This is how normal pointer works...
Code:int x = 5;
int *pX = &x;
But how come char doesn't work that way?
Why does this work even if I don't put & sign?Code:char y[] = "Hello!";
char *pY = y;
Printable View
This is how normal pointer works...
Code:int x = 5;
int *pX = &x;
But how come char doesn't work that way?
Why does this work even if I don't put & sign?Code:char y[] = "Hello!";
char *pY = y;
y in your example is not a char, is a (nul-terminated) array of char. In C, arrays are in many ways equivalent to a pointer to their 1st element. In your example, pY points to the address of 'H' (the 1st letter in your char array).
I have another question.
If pointer takes an address, how come the code above is executable? I mean in C++ I can't even compile this code...Code:int *x = 5;
You will get a warning have to type cast to integer pointer. I think you will also get a segmentation fault if you try to dereference x.
What you meant there was that your able to compile the above code, but not able to execute it. Because you wont be able to execute that code. Your trying to dereference a block of memory which dosnt belong to your process. That is, 5 is an arbitrary address as it is, you dont know if does even exists on your machine. The pointer can take any value. The problem only hits when you try to dereference it. In your example if your try to de-reference the x; your bound to get segfault. Does that make sense?
Sorry guys i'm holding this topic too long but I just wanted to make sure that i'm understanding this correctly.
[/code]Code:char *sentence = "Hello World!";
sentence is a pointer variable that is holding an address of the string(arrays of chars) "Hello World!". Am I correct?
So "Hello World!" is like a variable that has no name, like this,Code:char[] *blank* = "Hello World!";
And for some reason, we don't need to write. Why is this?Code:char *sentence = &"Hello World!"
Why can't I put & next to sentence?Code:char sentence[] = "Hello World!";
char *p_sentence = &sentence;
I'm sorry guys i'm just terrible at understanding pointer...
Close. The string literal "Hello World!" is an array of 13 chars. In most contexts, including here, an array is converted to a pointer to its first element. So, sentence is a pointer to char rather than a pointer to an array of 13 chars because it points to the first element of "Hello World!". Because string literals cannot be modified, we would normally use const char* instead:Quote:
Originally Posted by yj1214
Code:const char *sentence = "Hello World!";
This is one of those cases where the array is not converted to a pointer to its first element. &sentence results in a pointer to an array of 13 chars, i.e., a char (*)[13], but what you want is a char*.Quote:
Originally Posted by yj1214
Alright, correct me if i'm wrong.
sentence holds an address of a first element of the array which would be 'H' in this case.Code:char *sentence = "Hello";
p_sentence would store the address of value 'H' and it knows where the array ends because there is a nul-terminator at the end of the string literal.Code:char sentence[] = "Hello";
char *p_sentence = sentence;
Is there specific reason why I don't have to put &? Why can't compiler know that i'm talking about the first elemnt of "Hello" which appears to be 'H' (the address of 'H')?Code:char *x = &"Hello";
char y[] = "Hello";
char *z = &y;
It seems more weird that I don't have to put & because all other data type needs it except char,
Is it just whoever made C decided to do this way?
Is same asCode:char *x = "Hello";
The only difference is that second one can be modified...correct?Code:char x[] = "Hello";
char *y = x;
Last of all,
const is not neccecery but it's good idea to put it because we can't modify the value that x is pointing to.Code:const char *x = "Hello";
And we can't modify it because x is pointing to the first element of an array and it doesn't know other arrays like 'e' or 'l'...
The specific reason is that if you have an array x, &x results in a pointer to the array itself, not to a pointer to its first element. So, not only do you not have to put the &, but doing so would be wrong.Quote:
Originally Posted by yj1214
Because you would have explicitly told the compiler that you are talking about the entire array, not the first element thereof. It so happens that they have the same address, but the types are different.Quote:
Originally Posted by yj1214
That's not true. For example, consider:Quote:
Originally Posted by yj1214
The second line in the above code would likely result in a warning: &y is of type int(*)[3], but z is of type int*. The correct code would be:Code:int y[] = {123, 456, 789};
int *z = &y;
or equivalently:Code:int *z = y;
Code:int *z = &y[0];
No. In the former, x is a pointer to the first element of a string literal. In the latter, x is an array of char.Quote:
Originally Posted by yj1214
Yes.Quote:
Originally Posted by yj1214
No, you cannot modify it because it is part of a string literal, and modifying a string literal results in undefined behaviour. The implementation might place storage for the string literal in a location that cannot be modified, for example.Quote:
Originally Posted by yj1214