Smith Will Suffice
Smith Will Suffice

Reputation: 99

Cannot determine why pointer variable will not address elements in a string in this program?

I am attempting to utilize a pointer variable to access elements of a string and there are issues with my code generating a compilation error:

#include <stdio.h>
#define MAX 29

char arrayI[250];
char *ptr;

int main(void)
{
ptr = arrayI;

puts("Enter string to arrayI: up to 29 chars:\n");
fgets(arrayI, MAX, stdin);

printf("\n Now printing array by pointer:\n");
printf("%s", *ptr);


ptr = arrayI[1];     //(I set the pointer to the second array char element)
printf("%c", *ptr);  //Here is where I was wanting to use my pointer to 
                     //point to individual array elements.
return 0;
}

My compiler crieth:

[Warning] assignment makes pointer from integer without a cast [enabled by default]

I do not see where my pointer was ever assigned to the integer data type? Could someone please explain why my attempt to implement a pointer variable is failing? Thanks all!

Upvotes: 1

Views: 143

Answers (4)

Amarnath Krishnan
Amarnath Krishnan

Reputation: 1263

#include <stdio.h>
#define MAX 29
// An array declaration that can hold upto 250 characters 
char arrayI[250];
// A pointer variable that can hold the address of a character
char *ptr;
int main(void)
{
   // Assigning the array's base address to the pointer variable
   ptr = arrayI;
   puts("Enter string to arrayI: up to 29 chars:\n");
   fgets(arrayI, MAX, stdin);

   printf("\n Now printing array by pointer:\n");
   // Printing the entire array
   printf("%s", ptr);

   // Now modifying the pointer variable to point to the second character
   ptr = &arrayI[1];     
   // Printing the second character from left in the array
   printf("%c", *ptr);  
}

Suppose that the memory is allocated as follows:

Assume the base address starts from 4000.

      +----------+
4000  |          |    arrayI[0]  
      +----------+
4001  |          |    arrayI[1]
      +----------+
4002  |          |    arrayI[2]
      +----------+
           .
           .
           .
      +----------+
4249  |          |    arrayI[249]
      +----------+

Now ptr = arrayI => 4000

printf("%s",ptr);  //Print the entire string starting from address 4000

Now ptr = &arrayI[1] => 4001

printf("%c", *ptr); // Print valueAt(ptr) => valueAt(4001) => second character of arrayI

Upvotes: 2

BrainSteel
BrainSteel

Reputation: 669

This code:

ptr = arrayI[1];     //(I set the pointer to the second array char element)
printf("%c", *ptr);

is broken. ptr is a variable of type char*, and you're trying to assign a char to it. You want to assign the address of arrayI[1] to ptr, and you should thus use ptr = &arrayI[1];.

EDIT -- As Catalyst noted, your second printf should read as follows: printf("%s", ptr).

Upvotes: 1

rjv
rjv

Reputation: 6766

I guess you will be needing this

ptr = arrayI;

puts("Enter string to arrayI: up to 29 chars:\n");
fgets(arrayI, MAX, stdin);

printf("\n Now printing array by pointer:\n");
printf("%s", ptr);


ptr = arrayI+1;     //(Where 1 is the sizeof char)
printf("%c", *ptr)

ptr is a pointer,arrayI is also a pointer,but arrayI[1] is a character aka integer.

Upvotes: 0

Zach Halle
Zach Halle

Reputation: 321

I believe the problem is the line "ptr = arrayI[1]". ptr is of type char*, while arrayI[1] is of type char (which in C is the same as an int, hence the compiler message). The fix should be changing this line to *ptr = arrayI[1];

Upvotes: 0

Related Questions