But when you create an array of char p[4], don''t you actually create an array from p[0]->p[3] ?
The road to success is always under construction
I/O freaks me out !
char p[4] will create an array[0..3], and you could not
store the string "test" because the compiler adds the
''\0'' char automatically to any string constant in your
code. So you''ll require an char[5] array to store it
as an null terminated string.
store the string "test" because the compiler adds the
''\0'' char automatically to any string constant in your
code. So you''ll require an char[5] array to store it
as an null terminated string.
July 30, 2000 12:11 PM
quote: Original post by AlekM
Ford,
char* p;
p = "Text";
may be legal , but it is dangerous. You are writting into memory address space that does not exist. Where exactly is the memory for the pointer being initialized. It''s not on the stack.
char* p = "text"; /*this is fine*/
char p[] = "test"; /*this is fine*/
char p[4] = "test"; /* this will work, but is dangerous, especially if you will be using a lot of strcpy. Just be aware of how this is implemented. There is no room for a null character. So it''s not a proper string literal. */
quote: Original post by AlekM
Ford,
char* p;
p = "Text";
may be legal , but it is dangerous. You are writting into memory address space that does not exist. Where exactly is the memory for the pointer being initialized. It''s not on the stack.
No, the string literal "Text" does exist in memory. It is stored in memory as an array of char, and in an expression context, arrays are converted into pointers. p in this example points to that location.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement