Jump to content
  • Advertisement
Sign in to follow this  
jagguy

pointer to char

This topic is 4581 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

this works char *p ; p="xat"; cout << p <<endl; i thought you need something like this char *p=new char[10];

Share this post


Link to post
Share on other sites
Advertisement
"xat" is a string that gets compiled as an array of chars: [x, a, t, \0]
when you say char* p="xat";, thet p is the address of the char x
the cout function prints a string that is located at address p. Note that if you compile a string "xat" that a null character (\0) automaticly is added. This means that the string ends here. If there was no null character the program would crash.

Share this post


Link to post
Share on other sites
hi

i was doing some win32 stuff and it doesn't like string


if it is a constant then this shouldn't work but it does

char *p ;

p="xbbat";

cout << p <<endl;

p="xbbat";

cout << p <<endl;

Share this post


Link to post
Share on other sites
Win32 is a C-API. To use std::string with a C-API you need to invoke the c_str() member function whenever you want to pass the string to a function which takes a const char * argument, i.e.:
std::string className = "MyWindowClass";
std::string windowName = "My Window";
CreateWindow(className.c_str(), windowName.c_str(), /*remaining parameters*/);

Use std::string.

"xat" is a contant string. It has type const char[4]. When you compile your program the executable will contain the bytes 'x', 'a', 't', and '\0', somewhere within the executable image. When you do char * p = "xat"; you are making p point to the first of those bytes within the executable image. You are also invoking a special clause in the C++ Standard which allows the conversion of a const char[] to a char *. Normally a const char[] would be converted to a const char *, but for legacy reasons C++ allows the more lenient conversion. The bytes that p points are still const however, so attempting to write to them (i.e. *p = 'c';) is undefined behaviour.

Is is only the characters themselves which are constant though, not the pointer, which is why the pointer can then be made to point to other things. If the pointer itself was const then your latest code would fail to compile:
const char * p const = "xbbat"; // both the pointer and what it points to are const
cout << p << endl;
p = "xbbat"; // error - a constant pointer cannot be re-bound.
cout << p << endl;
Σnigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!