#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?
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"
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.