Problem dereferencing char pointer in C?

Thanks. Yeah that was the problem – Tony Mar 6 at 18:11 9 @Tony Don't forget to mark the answer as 'Accepted' by clicking the check mark then – zneak Mar 6 at 18:14.

Sadly, from its origins of the C programming language, char * (i.e. Pointers to characters) are not treated as just a pointer to a type like int * (pointer to int) or float * (pointer to a float), but many library functions treat these as NUL-character terminated strings. By "string" we mean text.

Therefore, there are lots of library functions that treat char* as special case and treats them as a "nul-terminated string", meaning it's gonna treat that pointer as an ARRAY of (i.e. Multiple contiguous) charactes, until it reaches a NUL character (i.e. Numeric value 0, not the character '0' which has a numeric byte value of 48 or 0x30).

So, if in your code you have char *sz = "123"; this will allocate 4 characters, the 3 you see plus the NUL character Addr:0x00050014 0x31 // ASCII char '1' Addr:0x00050015 0x32 // ASCII char '2' Addr:0x00050016 0x33 // ASCII char '3' Addr:0x00050017 0x00 // ASCII char nul sz (a pointer to a character, representing an address) will have the value 0x00050014 (note that the starting address value is determined by your computer via multiple steps via the compiler, then the linker, then the loader, then possibly the progrma itself). When you do cout If 0x00050015 is not a NUL, it dumps IT out. Then the value at 0x00050016, sees its not a NUL, dumps it out, then it gets to 0x00050017 and sees that it IS NUL, and does NOT dump it out, and stops looking at addresses and returns to YOUR code just after where you did the output of sz, specifically in this code it will return to the point where it will dump the endl (end of line) to cout; You will eventually learn better ways to represent text than the old C-style char* mechanism, but for now, you just need to understand the legacy behavior that exists.

C++ (or, more specificaly, ostream in this case), will automatically treat a char* as a (C style) string. You need to explicitly cast it to the type you want, such as: cout.

Sadly, from its origins of the C programming language, char * (i.e. Pointers to characters) are not treated as just a pointer to a type like int * (pointer to int) or float * (pointer to a float), but many library functions treat these as NUL-character terminated strings. By "string" we mean text.

Therefore, there are lots of library functions that treat char* as special case and treats them as a "nul-terminated string", meaning it's gonna treat that pointer as an ARRAY of (i.e. Multiple contiguous) charactes, until it reaches a NUL character (i.e. Numeric value 0, not the character '0' which has a numeric byte value of 48 or 0x30).

Sz (a pointer to a character, representing an address) will have the value 0x00050014 (note that the starting address value is determined by your computer via multiple steps via the compiler, then the linker, then the loader, then possibly the progrma itself). To the output stream cout. It looks at the value at address 0x00050014 and sees if its NUL (i.e.

0), if not, it dumps out the character, then looks at the next address. If 0x00050015 is not a NUL, it dumps IT out. You will eventually learn better ways to represent text than the old C-style char* mechanism, but for now, you just need to understand the legacy behavior that exists.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions