shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Originally Posted by pradeep
LOL! I took shakthi's '&' in the post for a literal AND and not the address-of operator!
sakthi.eeswari's Avatar
Go4Expert Member

i meant the & as the address question s that for all other data type for reading the input we are using &(ampersan) in the scanf function,but while we are reading strings we are not using &.can i know the reason for that


pradeep's Avatar, Join Date: Apr 2005
Team Leader
n C, a string is stored as a null-terminated char array. This means that after the last truly usable char there is a null, hex 00, which is represented in C by '\0'. The subscripts used for the array start with zero (0). The following line declares a char array called str. C provides fifteen consecutive bytes of memory. N.B. Only the first fourteen bytes are usable for character storage, because one must be used for the string-terminating null.

char str[15];

The following is a representation of what would be in RAM, if the string "Hello, world!" is stored in this array.

Characters: H e l l o , w o r l d !
Hex values: 48 65 6C 6C 6F 2C 20 77 6F 71 6C 64 21 00
Subscripts: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

The name of the array is treated as a pointer to the array. The subscript serves as the offset into the array, i.e., the number of bytes from the starting memory location of the array. Thus, both of the following will save the address of the 0th character in the pointer variable ptr.

ptr = str;
ptr = &str[0];

As because the name of the array is treated as a pointer you do not need to use the address-of operator because the pointer is itself an address.
I hope I was able to help you out.
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Or, as mentioned in post #9,
The name of the array is treated as a pointer to its first element. Since it's already a pointer, the address-of operator is not required.
If you're not reading your responses, why should we write them?
sakthi.eeswari's Avatar
Go4Expert Member
hi dawei

i post the thread b4 3 days.i think pradeep asked me that what i m going to xplain.thats y i post the thread once again.after posting that i got the reply.sterday itself i read that.