943,998 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1729
  • C RSS
Nov 11th, 2005
0

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

Expand Post »
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<stdio.h>
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...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arun_kumar_c is offline Offline
5 posts
since Nov 2005
Nov 11th, 2005
0

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

Quote originally posted by arun_kumar_c ...
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.

Quote originally posted by arun_kumar_c ...
and can anyone expalin this program:
the out put was :
415
419
98
97
Well, 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 13th, 2005
0

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

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
arun_kumar_c is offline Offline
5 posts
since Nov 2005
Nov 13th, 2005
0

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

you should not use gets, instead use fgets
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Nov 13th, 2005
0

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

I should have given you the reason for that also...you can see that here
http://www.gidnetwork.com/b-56.html
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Converting without additional headers
Next Thread in C Forum Timeline: Counting occurrence of numbers in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC