so, an array IS NOT a pointer which points to the first element in the array? its only set to this by default, but in reality it could point to ANY element in the array? i read in my text book that arrays were const pointers, therefore could not change there value and ALWAYS pointed to the first element in the array. so this is bullcrap? an array IS NOT a const pointer? also, if im pointing to an element in a char array, and cout << chararray, it will print out each element following the one that the pointer is pointing to? thanks for clearing things up guys, i appreciate it
[edited by - graveyard filla on March 27, 2004 2:22:39 PM]
string verse char *
You''re confused to all hell. An array IS a const pointer (but not necessarily a pointer to const, unless you declare it that way). Example:
char letters[] = "abc";
You can''t do the following:
letters++;
because it''s a const pointer. If you were to declare letters like the following:
char* letters = new char[4];
strcpy(letters, "abc");
You can do this now:
letters++;
in which case it will point to letter ''b'' and if you were to send it to cout, you would get "bc" printed out.
This is covered in any introductory C++ text. RTFM.
char letters[] = "abc";
You can''t do the following:
letters++;
because it''s a const pointer. If you were to declare letters like the following:
char* letters = new char[4];
strcpy(letters, "abc");
You can do this now:
letters++;
in which case it will point to letter ''b'' and if you were to send it to cout, you would get "bc" printed out.
This is covered in any introductory C++ text. RTFM.
quote:Original post by CGameProgrammerA common implementation, but not a requirement. Have you ever read the standard?quote:Original post by Beer HunterAn array is exactly identical to a pointer at runtime...
int main()
{
char myArray[50];
char* pointerToFirstElement = myArray;
std::cout << sizeof(myArray) << std::endl; // Prints "50"
std::cout << sizeof(pointerToFirstElement) << std::endl; // Prints "4" (usually)
}
quote:Original post by Beer Hunterquote:Original post by CGameProgrammerA common implementation, but not a requirement. Have you ever read the standard?
An array is exactly identical to a pointer at runtime...
No. Does it matter? If VC++ and GCC produce code that treats arrays and pointers identically, then any code people here write will treat arrays and pointers identically, regardless of what the standard says.
quote:so, an array IS NOT a pointer which points to the first element in the array? its only set to this by default, but in reality it could point to ANY element in the array?
No, you have it backwards. When you do this:
char* mystring = "Hello World";
mystring is NOT an array; it''s a pointer that points to the first node in an array. "Hello World" is a null-terminated string which is an array of characters. ''mystring'' is a pointer to a char.
char* mystring = new char [12];
strcpy( mystring, "Hello World" );
mystring is, again, a pointer to a char. It points to an array of 32 characters created at runtime.
~CGameProgrammer( );
-- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
C++ did not have string either untill rescently when ISO made it standardised, just a few years ago.
quote:Original post by Beer Hunterquote:Original post by CGameProgrammerNope.
Does it matter?
It was a rhetorical question.
~CGameProgrammer( );
-- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement