954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

i am a to c programing..and a doubt about pointers

hi guys ,
I am a first year student!
can anyone help ..
the code. is .
main()
{
char *b="abc";
printf("%s",b);
}
the output is :abc.
my doubt is :
how can we assign a string constant to a pointer.And when we assign like this is a character array with the name b[] is initialised to "abc"?
and can anyone expalin this program:
#include
main()
{
char *j="abc";
printf("%u\n%u\n","abc",&"abc");
printf("%u\n%u",j[1],j[0]);
}
and the out put was :
415
419
98
97
please help... :confused:

arun_kumar_c
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 
my doubt is : how can we assign a string constant to a pointer.And when we assign like this is a character array with the name b[] is initialised to "abc"?

The pointer is assigned the address of an unnamed array which contains the string literal.and can anyone expalin this program:
the out put was :
415
419
98
97Well, first of all the behavior is undefined so what you see does not necessarily mean anything. At best you might be seeing the address of a string literal presented as an integer for the first two. The second two merely show that your system uses ASCII for characters.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

thank you for your reply...hey i have another doubt too..i declared a character pointer....
when i assign a value i.e .. a string to a character pointer using gets()...i get the warning possible use of that 'character pointer'before declaration..but the program works :rolleyes: ...do you know why?

arun_kumar_c
Newbie Poster
5 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

you should not use gets, instead use fgets

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

I should have given you the reason for that also...you can see that here
http://www.gidnetwork.com/b-56.html

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You