Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointer

Status
Not open for further replies.

cat5ive

MIS
Dec 3, 2004
184
US
Hi,

char *string = "A string!";

why is *string+8 has a value of '!'?
Why not one position after '!' (which is a null char.)?
Since *string contain value of A(index 0), doesn't *string+8 mean add 8 more index to it?

Thanks
 
> why is *string+8 has a value of '!'?
Because your string has 9 characters, and ! is the last one


--
 
????please explain. From what I see, we start counting from 0 (which is 'A'), then '!' would be at 8 not 9. Or may be I should ask, what's the different between *string+8 and string[9]? (string[9] contain NULL)
 
got it now. It's the counting that get me (very)confuse. Sometime I'm not sure if I sould count from 0 or 1.

Thanks Salem.
 
Indexing is zero based, size is simply howmany... So operations logically that would yeild a size start counting with 1, things that logically yeild a index start counting at 0.

Index:
Code:
012345678 9
A string![blue]NULL[/blue]
[code]
size = 9.

So...
char *string = "A string!"
string gets the address of the value A string.
string[0] gets the thing that is at the value of string (the address) + [0 (the offset in square braves) * size of datatype the pointer is pointing].
*(string+8)
8 plus that address is '!'.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top