By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,960 Members | 1,312 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,960 IT Pros & Developers. It's quick & easy.

Pointers

P: 12
char * a ="dd";
if we print a, it shows dd as output. But a is a pointer , how can it show the value.
Sep 25 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
I very much doubt you are printing a then but since you haven't posted the code you used it is hard to say.

Most forms of output accept a pointer to a string in order to output the characters of that string, for example

printf("%s\n", a);

the %s tells the function printf that the first parameter following the format string is a pointer to a string and that it should output the characters of that string.
Sep 25 '06 #2

P: 12
#include<stdio.h>
main()
{
char *p = "world";
printf("%s ",p);

}
here p is pointer and it points to world.
The output is world , but we are printing p which is a pointer, how can it have the values 'world'
Sep 25 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
#include<stdio.h>
main()
{
char *p = "world";
printf("%s ",p);

}
here p is pointer and it points to world.
The output is world , but we are printing p which is a pointer, how can it have the values 'world'
No you are not printing p, printing p would look like

main()
{
char *p = "world";
printf("%p ",p);
}

By putting %s you are telling printf that the next thing to appear on the parameter list is a pointer to a string and that it should output the string that the pointer points to.

p does not have the value 'world' it has value of the location in memory of the string literal 'world' and it provides this to printf.
Sep 25 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.