1

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!

1
  • in the second printf you have %s which actually takes a pointer --- should be printf("%s", ptr) Commented Nov 11, 2013 at 3:49

4 Answers 4

2
#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
Sign up to request clarification or add additional context in comments.

Comments

1

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).

Comments

0

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];

1 Comment

Two things: char and int are two different types, and C knows this. Also, if he uses *ptr = arrayI[1], then it'll set arrayI[0] to array[1], which is clearly not intended.
0

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.

1 Comment

Using arrayI + 1 overcomplicates arrayI[1]. Also, arrayI is NOT a pointer. arrayI is an array. c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=aryptr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.