string verse char *

Started by
15 comments, last by graveyard filla 20 years, 1 month ago
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]
FTA, my 2D futuristic action MMORPG
Advertisement
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.
quote:Original post by CGameProgrammer
quote:Original post by Beer Hunter
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)
}
An array is exactly identical to a pointer at runtime...
A common implementation, but not a requirement. Have you ever read the standard?
quote:Original post by Beer Hunter
quote:Original post by CGameProgrammer
An array is exactly identical to a pointer at runtime...
A common implementation, but not a requirement. Have you ever read the standard?

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( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
C++ did not have string either untill rescently when ISO made it standardised, just a few years ago.
Commander Keen
quote:Original post by CGameProgrammer
Does it matter?
Nope.
quote:Original post by Beer Hunter
quote:Original post by CGameProgrammer
Does it matter?
Nope.

It was a rhetorical question.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.

This topic is closed to new replies.

Advertisement