Sign in to follow this  
mckoo

stuck at char ptr*

Recommended Posts

mckoo    122
excuse me, why asterisk(*) sign is not required to be attached behind "s" i read the tutorial that was quoted but i don't understand why the char ptr does not need to dereference the string thx in advance. quote from http://computer.howstuffworks.com/c36.htm Fragment 1 { char *s; s="hello"; printf("%s\n",s); } "These two fragments produce the same output, but their internal behavior is quite different. In fragment 2, you cannot say s="hello";. To understand the differences, you have to understand how the string constant table works in C. When your program is compiled, the compiler forms the object code file, which contains your machine code and a table of all the string constants declared in the program. In fragment 1, the statement s="hello"; causes s to point to the address of the string hello in the string constant table. Since this string is in the string constant table, and therefore technically a part of the executable code, you cannot modify it. You can only point to it and use it in a read-only manner. [Edited by - mckoo on August 4, 2004 2:37:36 AM]

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by mckoo
excuse me,
why asterisk(*) sign is not required to be attached behind "string"
i read the tutorial that was quoted but i don't understand why the char ptr does not need to dereference the string


Because a string literal is not a pointer, you can't dereference a string literal because it's not a pointer. I think you need to read up on pointers & arrays.

A string literal is a statically allocated constant array of characters meaning it's of type
const char a[n];
. So when you assign a string literal to pointer to char, the pointer gets copy of the address of the first element into the array. So it would be like saying:


char a[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
char* ptr = a;

or

char* ptr = &a[0];


Except string literals in C cannot be modified otherwise it will cause undefined behaviour.

Share this post


Link to post
Share on other sites
mckoo    122
sorry i made a typing mistake....
it should be why * sign cannot be added behind "s" so can be assigned with the string "hello"

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by mckoo
sorry i made a typing mistake....
it should be why * sign cannot be added behind "s" so can be assigned with the string "hello"


Because you have already declared it as a pointer to char before, when you use the operator "*" outside a declaration of a new variable it means to dereference a pointer (to get the value it points to) so if you did that it's gonna crash because when you declared that pointer you didn't assign it to any value but thats ok because you assign it a copy of the address of first element into that string literal later on. Like i said before if you read up about pointers & arrays you'll come across a section about how the name of an array is simillar to a constant pointer to the first element in the array. Then things will start to make sense.

Share this post


Link to post
Share on other sites
mckoo    122
so since the starting address is assigned to ptr....
ptr = string;
[ptr + 1] = string[1];?

----------------------------------------------
so that "ptr" could print out the content stored in the element of string?

seems making sense rite?

thx in advance~~

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by mckoo
so since the starting address is assigned to ptr....
ptr = string;
[ptr + 1] = string[1];?


You mean is (ptr + 1) equivalent to string[1] yes thats correct.

Share this post


Link to post
Share on other sites
mckoo    122
thx so much....
but how could i print out a ptr with string
since the address of first element of the array is only assigned to ptr

printf(" %s", ptr);
%s means that it will look up the next element of the ptr of the array?

excuse me ignorance

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by mckoo
thx so much....
but how could i print out a ptr with string
since the address of first element of the array is only assigned to ptr

printf(" %s", ptr);
%s means that it will look up the next element of the ptr of the array?

excuse me ignorance


the conversion specifier s means a null-terminated strings (C style strings) so it knows that it's either a pointer points to the first element of an array of characters, or it's an array of characters because you can use an array name just like a pointer to the first element of an array.

So inside printf you can imagine a loop through the array to print the string to the console.

Share this post


Link to post
Share on other sites
snk_kid    1312
This is a good example of how a pointer to an array & how the name of an array can be used like each other, take the library function strcpy (string copy) you could do it like this:

first with array subcript version

void strcpy(char* source, char* dest) {
int i = 0;
while((source[i] = dest[i]) != '\0')
++i;
}







second version pointer arithmetic version


void strcpy(char* source, char* dest) {
while((*source = *dest) != '\0') {
++source;
++dest;
}
}







You can see how the pointer version can reduce to the common C idolism:


void strcpy(char* source, char* dest) {
while((*source++ = *dest++) != '\0')
;
}







What is the advantage of using a pointer as parameter instead of an array? well the pointer doesn't have to point to the first element of an array, it could be in the middle.

[Edited by - snk_kid on August 4, 2004 4:17:43 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this