char pointer question
-
Hi All, I was playing around with char pointers just trying to learn things and noticed that if I malloc the size of a char and set what the returned poitner points at to be 'AB', when I cout what the pointer points to it writes out 'B'. This is the code: char *mainBuf = NULL; mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB'; cout << "mainBuf=" << mainBuf << endl; The output is: mainBuf=B I really didn't know what this would do as like I said I was experimenting. In a way I was surprised it did not crash because I thought cout looked for the terminating null character. Can anyone help me understand why this does what it does? Thanks.
-
Hi All, I was playing around with char pointers just trying to learn things and noticed that if I malloc the size of a char and set what the returned poitner points at to be 'AB', when I cout what the pointer points to it writes out 'B'. This is the code: char *mainBuf = NULL; mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB'; cout << "mainBuf=" << mainBuf << endl; The output is: mainBuf=B I really didn't know what this would do as like I said I was experimenting. In a way I was surprised it did not crash because I thought cout looked for the terminating null character. Can anyone help me understand why this does what it does? Thanks.
yadrif wrote:
mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB';
What exactly are you expecting to do with this? :confused: You are passing
1
tomalloc()
, yet you are trying to stuff, albeit incorrectly, 2 bytes intomainBuf
. Why? Why are you usingmalloc()
with a C++ program. Usenew
instead:char *mainBuf = new char[3];
strcpy(mainBuf, "AB");
delete [] mainBuf;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
yadrif wrote:
mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB';
What exactly are you expecting to do with this? :confused: You are passing
1
tomalloc()
, yet you are trying to stuff, albeit incorrectly, 2 bytes intomainBuf
. Why? Why are you usingmalloc()
with a C++ program. Usenew
instead:char *mainBuf = new char[3];
strcpy(mainBuf, "AB");
delete [] mainBuf;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
DavidCrow wrote:
yet you are trying to stuff, albeit incorrectly, 2 bytes into mainBuf
Nope, 'AB' is one byte (it is different than "AB"), this will be truncated to one byte. But I think the compiler should give an error there instead of a warning...
Cédric Moonen Software developer
Charting control [v1.2] -
DavidCrow wrote:
yet you are trying to stuff, albeit incorrectly, 2 bytes into mainBuf
Nope, 'AB' is one byte (it is different than "AB"), this will be truncated to one byte. But I think the compiler should give an error there instead of a warning...
Cédric Moonen Software developer
Charting control [v1.2]Point taken, but it's wrong nonetheless.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
yadrif wrote:
mainBuf = (char*)malloc(sizeof(char)); *mainBuf = 'AB';
What exactly are you expecting to do with this? :confused: You are passing
1
tomalloc()
, yet you are trying to stuff, albeit incorrectly, 2 bytes intomainBuf
. Why? Why are you usingmalloc()
with a C++ program. Usenew
instead:char *mainBuf = new char[3];
strcpy(mainBuf, "AB");
delete [] mainBuf;
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
I was just playing around with the code, no real application here. Just learning. I actually started by writing a sample to use a pointer to a pointer. Just to learn how it worked and actually use what I read. I then for some reason decided to stuff 'AB' into what the pointer points to just to see what would happen. I then removed the pointer to pointer part to simplify it before posting the question. Maybe this just gets into the realm of undefined behavior. I thought perhaps there would be an explanation as to why it does what it does. Thanks.
-
DavidCrow wrote:
yet you are trying to stuff, albeit incorrectly, 2 bytes into mainBuf
Nope, 'AB' is one byte (it is different than "AB"), this will be truncated to one byte. But I think the compiler should give an error there instead of a warning...
Cédric Moonen Software developer
Charting control [v1.2]Nope, 'AB' is a short int that will truncated to one byte ;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I was just playing around with the code, no real application here. Just learning. I actually started by writing a sample to use a pointer to a pointer. Just to learn how it worked and actually use what I read. I then for some reason decided to stuff 'AB' into what the pointer points to just to see what would happen. I then removed the pointer to pointer part to simplify it before posting the question. Maybe this just gets into the realm of undefined behavior. I thought perhaps there would be an explanation as to why it does what it does. Thanks.
I'm sorry no one here has taken the effort to give you a good reply. I also would like to know the answer. I've never used syntax like 'AB' since I've always dealt with ASCII strings, and the single 'apostophe' returns the character (integer) value of a single character between the marks. I don't know what it does in your context, but appears that perhaps it's playing "little endian" and returning the least-significant byte of a 16-bit value??? Did you try declaring the pointer as a wchar_t* instead? This might work. Maybe some real coders will see your question in a while and respond with a real answer. David
-
Nope, 'AB' is a short int that will truncated to one byte ;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
:-D
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I'm sorry no one here has taken the effort to give you a good reply. I also would like to know the answer. I've never used syntax like 'AB' since I've always dealt with ASCII strings, and the single 'apostophe' returns the character (integer) value of a single character between the marks. I don't know what it does in your context, but appears that perhaps it's playing "little endian" and returning the least-significant byte of a 16-bit value??? Did you try declaring the pointer as a wchar_t* instead? This might work. Maybe some real coders will see your question in a while and respond with a real answer. David
DQNOK wrote:
but appears that perhaps it's playing "little endian" and returning the least-significant byte of a 16-bit value???
This is correct.