Skip to main content
gamedev.net
🔒 Locked

pointers

Started by slightlywhacked 2 replies 400+ views
Original Post
slightlywhacked
slightlywhacked
does.. char *pointer; point to a character..or a whole string like if i pointed it to char moo[50] = "why do cows moo?"; would the pointer return why do cows moo? thanx in advance
BAHcows go moomoos go cow
Sly
Sly
A character is simply a single element of a string. A character could be the entire string. A string is simply a sequence of characters.

As the variable declaration shows, char *pointer is a pointer to a character. This could point to the first character in a string or a character in the middle of the string.

char moo[50] sets aside 50 sequential characters and initializes it to "why do cows moo?" (with null terminator). The thing to remember here is moo by itself is a really a char *. That is why you can use moo in strcpy().

pointer = moo means that pointer will now point to the same memory space that moo points to. And now you can use pointer[1] to get ''h''.

Have I confused you yet?



Steve ''Sly'' Williams
Tools Developer
Krome Studios
ByteBlaster256
ByteBlaster256
The pointer is actually just a number refering to the first character in your string. But yes, the *pointer should return the entire string.

-----------------------------
C++ is great, but when is B-
coming out?
-----------------------------C++ is great, but when is B-coming out?
Gladiator
Gladiator
moo is simply the address to ''w'' in "why do cows moo?"

For the same reason *moo is actually ''w'' itself (meaning the value at the location that moo is pointing at).

Now, it is YOUR responsibility to check for the ''\0'' (read NULL) terminator at the end of your string. NULL indicates the end of string.

You can have void *str; and still think of it as a string, but you would need typecasting to get the varies operations to work correctly on the string.

That''s how all the functions work on strings. They check for that NULL terminator at the end.

To answer your question, the pointer will return the address of the first character in the string. That''s it!

Anyways, if you have any other questions, please ask!

-------------------------------
"Mind your own damn business!!!" - Gladiator

..-=gLaDiAtOr=-..

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.