Archived

This topic is now archived and is closed to further replies.

string verse char *

This topic is 5007 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

high, in reading some source code, i see people passing a const char to a function when they want to send a string a text... ie Function(const char*); why though? i have an Open() function that opens a map in my pacman game and i have it recieve a string. it looks like Open(string MAP) and then i will call it by doing Open("map1.txt"); etc. also to use this string with a filestream i just use c_str() . this works fine, so why would someone use a char *? sorry if this sounds stupid, i dont know that much about pointers, so i might be missing something obvious.

Share this post


Link to post
Share on other sites
1) C does not have std::string.

2) A const char * can be passed in a register, while a passing a string by value requires making a copy of the whole string (although a const string reference would resolve this problem).

Share this post


Link to post
Share on other sites
Compatibility is another reason, const char* is the LCD (least common denominator).

Calling c_str() on a std::string is less work than turning a char[] into a (temporary) std::string.

I usually overload the function and provide both functions (and have the std::string invoke the const char* using .c_str()).

Now, if I need to return a string, it''s std::string without question.

Share this post


Link to post
Share on other sites
passing char*''s around is the old C way of pushing "strings". You''ll find it in APIs of various libs that are C/C++ compatable (as opposed to just C++). They are a bit messy for my tastes.

IMHO std::strings are much better, but you''ll have to learn to work with either/both if you start working with some other libs.

Share this post


Link to post
Share on other sites
ok, another stupid question, what does it look like when these people pass char * 's? heres an example with strings:

void Funct(string mystring);
{
string another = mystring;

cout << another << endl;
}

what would it look like with char pointers? i mean, do they make an array of characters, then pass the entire array into a function? what does this all look like? (ive never had to pass an ENTIRE array into a function, im wondering what this looks like, i have however passed elements of an array to a function, but this looked "normal". do you just make a character array, then pass this as a pointer (since an array is a pointer that points to the first element of the array?). please just give me an example or 2. thanks guys!

[edited by - graveyard filla on March 26, 2004 11:23:28 PM]

[edited by - graveyard filla on March 26, 2004 11:23:39 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
ok, another stupid question, what does it look like when these people pass char * 's? heres an example with strings:

void Funct(string mystring);
{
string another = mystring;

cout << another << endl;
}


There are two ways to do this. To create a totally new string identical to mystring, you'd do this:

void Funct(char* mystring);
{
char* another = new char [1 + strlen(mystring)];
strcpy( another, mystring );

cout << another << endl;

delete [] another; // EDIT: Forgot to add this line.
}

If you wanted 'another' to just point to the same string that 'mystring' points to, you'd do this (much faster):

void Funct(char* mystring);
{
char* another = mystring;
cout << another << endl;
}

Anyway you'd never pass an entire array to a function. Instead you pass a pointer to the first element. mystring is a pointer to the first character of it. If you say:

char* mystring = "Hello World";
cout << *mystring;
mystring ++;
cout << *mystring;

The output would be "He".

~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..

[edited by - CGameProgrammer on March 26, 2004 11:57:06 PM]

Share this post


Link to post
Share on other sites
hey Cgame, thanks for your reply. it both confused and helped me. well first of all, i did NOT know that you could assign a value to a pointer like that. you said

char* hello = "hello world";

well, how could you assign a value to a pointer? does this only work with characters? wouldnt you want to do

char hello[] = "hello world"; instead?

also, that incrementation of the pointer confused me even more.
you write:




char* mystring = "Hello World";
cout << *mystring;
mystring ++; //what the crap is this???
cout << *mystring;

how could you increment a pointer? and what is going off behind the scenes? when you increment, are you saying "point to the second element in mystring, which is the letter e"? in which case, how could a pointer have elements? i thought arrays did. argh, so confused now also, IF you truly are incrementing to the next element in the pointer, wouldnt that print out ''e'', because that is the next element, and NOT ''He'' ?

more questions: how would you send a value to this function you jsut showed me?

void Funct(char* mystring);
{
char* another = mystring;
cout << another << endl;
}

would you do

char* high = "hello world";
Funct(high);

or would it be
Funct(&high); ??

i understand that & means memory address, but i dont understand what context this is in with pointers. if we say
Fuct(&high);

does this send the memory address of the pointer itself? or does it send the memory address thats stored in the pointer?

thanks for any help!!! sorry for all the questions

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
char* hello = "hello world";

well, how could you assign a value to a pointer? does this only work with characters? wouldnt you want to do

char hello[] = "hello world"; instead?



char *hello = "hello world";

This drops a string "hello world" in your data segment (it sits in your exe file when you compile it) and assigns hello to the address of the string. You cannot write to this string (well, you can on some compilers but never should).

char hello[] = "hello world";

This copies the string "hello world" into an array of characters. The difference between the two is subtle but very important. Yes, you can only do the first one with chars.

quote:

char* mystring = "Hello World";
cout << *mystring;
mystring ++; //what the crap is this???
cout << *mystring;



The code is a little off, you don't want to dereference mystring really.

char* mystring = "Hello World";
cout << mystring; // Outputs "Hello World"
mystring++; // increment the address
cout << mystring; // Outputs "ello World"


quote:

more questions: how would you send a value to this function you jsut showed me?

would you do

char* high = "hello world";
Funct(high);



Yes.
quote:

or would it be
Funct(&high); ??


No.

Here is a simple rule of thumb. If you have something that is declared as a pointer, you will almost never want to take the address of it using the & operator. Take the address of the address? That's not too useful, unless you're making a pointer to a pointer, but don't worry about this for now.


[edited by - bobstevens on March 26, 2004 12:54:49 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
...since an array is a pointer that points to the first element of the array?
It actually isn''t. It''s easy to get confused since an array can be implicitly cast to a pointer to its first element.

#include <iostream>

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)

}

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
char* mystring = "Hello World";
cout << *mystring;
mystring ++; //what the crap is this???
cout << *mystring;

A pointer is an integer that specifies a location in memory. By incrementing the pointer, you increment the address by 1 (at least for char and void pointers). So initially mystring points to the ''H'' in "Hello World", but if you increment it, it then points to the ''e''. And saying ''*mystring'' is identical to ''mystring[0]'' so dereferencing it returns the character it points to.

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; it''s only at compile-time that they''re treated differently. sizeof is static; sizeof(myArray) is replaced with "50" before the code is compiled into assembly.

~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..

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites