Simple pointer question...
-
whats the difference between
char test[] = "Hello";
andchar* test = "Hello";
in both cases is "Hello" allocated on the stack?? :confused: -
whats the difference between
char test[] = "Hello";
andchar* test = "Hello";
in both cases is "Hello" allocated on the stack?? :confused:Haroon Sarwar wrote:
char test[] = "Hello";
This is an array of characters. This is not a pointer.
Haroon Sarwar wrote:
char* test = "Hello";
This is a
pointer
to an array of characters. Note that test will contain the address of the given array and not the real data. For proof enter the following in the debugger &test test ptest &ptest You will see that the first two are same, but the last two are not.
Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com
-
whats the difference between
char test[] = "Hello";
andchar* test = "Hello";
in both cases is "Hello" allocated on the stack?? :confused:There are notable differences between:
Haroon Sarwar wrote:
char test[] = "Hello";
Here
test
is a character array(i.e. aconst
pointer
to a memory area containing six characters -remember the'\0'
string terminator-), initialized with the string literal"Hello"
.Haroon Sarwar wrote:
char* test = "Hello";
Here
test
is apointer
to the string literal"Hello"
, that is a read-only memory area. See the following sample code:char test1[] = "Hello";
char * test2 = "Hello";test1[2]='p'; // (1) legal, you can change the value of an element of the array
// test1 = test2; (2) ILLEGAL, compiler error, test1 is a const pointer//test2[2]='p'; (3) ILLEGAL, runtime error, you cannot change read-only memory area
test2 = test1;// (4) legal, you can change the pointer value
test2[2]='p'; // (5) legal, since now, test2 points to the same writable memory area of test1Hope that helps :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
whats the difference between
char test[] = "Hello";
andchar* test = "Hello";
in both cases is "Hello" allocated on the stack?? :confused:If you do these within a function they will both get allocated on the Stack, one array and one pointer. The actual
"Hello
" text in each case will end up as static data within the executable image.char text[] = "Hello";
will give you an on stack copy of this data at runtime.const char* test = "Hello";
will give you a pointer to the string stored in the image memory.char* test = "Hello";
will almost certainly be the same. If you do these at file scope, i.e. in a .c or .cpp file but not in a function then the "Hello" text is still stored in the image and in this case so is test[] or test. With a reasonable optimising compiler with string mergingchar* test;
will probably end up pointing to the same memory refered to bychar test[]
which will be the one and only original"Hello"
text in the loaded image. Have a look in the debugger at runtime and see what the address values are in each case. You'll soon spot the pointers to stack memory as opposed to pointers into the loaded image, the ranges will be quite different.:)Nothing is exactly what it seems but everything with seems can be unpicked.
-
whats the difference between
char test[] = "Hello";
andchar* test = "Hello";
in both cases is "Hello" allocated on the stack?? :confused:See here.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne