0
#include<stdio.h>
int main(void)
{

 char heart[]="I Love Tillie"; /* using array notation */

 int i;
 for (i=0;i<6;i++)
 {
   printf("%c",&heart[i]);  /* %c expects the address of the character we want to print     */
 }

 return 0;

}

If heart[i] and &heart[i] mean the same thing , which is the address of heart[i], why is my program giving me this-??????, as output? Could someone please help me out here?

9
  • 1
    Where did you read heart[i] is same as &heart[i] ? Commented Dec 18, 2013 at 13:08
  • They don't mean the same thing. Commented Dec 18, 2013 at 13:08
  • The name of a character array, like any array name, yields the address of the first element of the array. Therefore, the following holds for the array m1: m1 == &m1[0] , *m1 == 'L', and *(m1+1) == m1[1] == 'i' from C Primer Plus- STephen Prata , confused me too. Commented Dec 18, 2013 at 13:10
  • 2
    What gave you the idea that a char is the same as the address of a char (char *)?! I've explained this in an answer to one of your other questions, didn't I? Commented Dec 18, 2013 at 13:13
  • 3
    You have misread the equivalences. Apparently, the array is char m1[] = "Like"; or something similar. Saying that m1 == &m1[0] is accurate. Saying that *m1 == 'L' is equivalent to saying m1[0] == 'L' and given the initialization I showed is accurate. Similarly, *(m1+1) == 'i' and m1[1] == 'i' are individually accurate, but the composite statement *(m1+1) == m1[1] == 'i' does not evaluate to true in C, though it makes sense speaking loosely. Commented Dec 18, 2013 at 13:16

2 Answers 2

4

First of all

should be

printf("%c",heart[i]); // if you want to print the charachter

or

printf("%p",&heart[i]); // if you want to print the charachter address in the memory

and not

printf("%c",&heart[i])

The heart is an array of charachters and the heart[i] is the charachter number i in the array

The &heart[i] is the memory address of the element number i in the heart array. and to print the memory address you have to use "%p"

Sign up to request clarification or add additional context in comments.

Comments

2

You are trying to print an address as a single character; this is bad news.

heart[i] is a single character; &heart[i] is the address of that character. They are not the same thing at all.

Try a loop like this:

for (i = 0; i < 6; i++)
{
     printf("%c", heart[i]);
     printf(": %s\n", &heart[i]);
}

See what a difference the different conversion specifications (and parameter types) make. If you wish, you can add a printf("%p ", (void *)&heart[i]); to the start of the loop to see how the address values change as you go through the loop.

2 Comments

Yes, %p would work; it would do a completely different job, of course. %p will print the address; %s will print the substring. Of the two, the substring is probably more interesting to the OP, but you could add a printf("%p\n", (void *)&heart[i]); to the list of informative printing operations.
@MarounMaroun: %p would also require a (void *) cast

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.