char pointer

Roses are red
Violets are blue
Pointer stores an address of a variable
Then how come code below is working?

1
2
char *x = "Hello"; //OK
char *x[] = "Hello"; //ERROR 


 
int *x = 5;  //ERROR; 

Last edited on
char *x = "Hello"; //OK
NOT OK actually. conversion of string literal to non-const char pointer is prohibited by current C++ standard. Many compilers still allows that to ensure backwrd compatibility though.

char *x[] = "Hello"; //ERROR
It sure is, because "Hello" is not an array of char pointers. You can do that though: const char* x[] = { "Hello" };. Here you are declaring an array of 1 pointer to the const char.

int *x = 5; //ERROR;
Why should it work? 5 is not an address, so you cannot store it in pointer.
I'm using C11. Is there differences when it comes to pointer? so far I only noticed that in C, there is no 'new' function. Instead they have 'malloc' function to allocate memories in heap.

 
int *x = 5;

Funny thing in C, above code is executable.
It has to be valid in C, that's how you work with hardware in freestanding environments.
C is not C++. C++ strives to be more type safe so it imoses restriction on most undesireable behavior. In fact most of unsafe or dangerous language rules are there to ensure backward compatibility (first with C, then with older C++ standards).

Is there differences when it comes to pointer?
I do not know well C11 standard, but C's "razor" is sharper than C++ one. You can do stuff like implicit conversion from void pointer (prohibited in C++ for obvious reasons). So, there probably is.

I only noticed that in C, there is no 'new' function. Instead they have 'malloc' function to allocate memories in heap.
This stems from C's lack of classes and constructors. new allocates memory and calls constructor. malloc only allocates memory. As C structs do not have constructors, new is not going to do any more for them. So no need for new in C.

It has to be valid in C, that's how you work with hardware in freestanding environments.
Implying that you cannot do the same in C++ with slight elaborating on your intentions to the compiler.
1
2
3
int *x = reinterpret_cast<int*>(5); //Yes this is what I want to do.
//I ready for consequences and willing to take the blame 
//For any problems which might arise because of that operation 
Last edited on
Er,

const char * s = "Hello";
This is fine, since s is a pointer to the first character in the immutable character array "Hello".



const char * x[] = "Hello";
It is an error, because the "[]" syntax means you are trying to create a mutable array of (const char*) with size given by the initializer.

However, your initializer is the element's element type.

const char * x[] = { "Hello" };
All better.



int * x = 5;
The only reason this won't compile is because assigning an address directly is probably not what you meant to do. If you really did mean to try and access memory location 5, use a cast:

int * x = (int*)5;
Again, this compiles, but it is the wrong thing -- x now is a pointer to an integer at address 5 in memory, and the OS will probably disagree with you about trying to access it.

If what you really want is a pointer to an integer, anywhere in memory that you are allowed to touch, and that integer has the value five, then:

1
2
int * x = new int;
*x = 5;

Don't forget to delete x; before your program terminates.

Hope this helps.
MiiNiPaa wrote:
Implying that you cannot do the same in C++ with slight elaborating on your intentions to the compiler.
I didn't realize my post had implications toward C++, sorry for the confusion.
And here I thought I was pretty quick on the draw with that one...
Er,

const char * s = "Hello";
This is fine, since s is a pointer to the first character in the immutable character array "Hello".


I still don't get it.

int *x = 5;
This code is invalid.

char *y = "Hello";
This code is valid.


Shouldn't char * needs an address? like this...
char *x = &y;

So why does char able to assign its value that isn't an address?
Last edited on
yj1214 wrote:
int *x = 5;
This code is invalid.
Only without the cast.

Computer memory is like a giant array of bytes.
A pointer is an index into that array. So int *x = (int*)5; says that 'x' is a pointer to an integer starting at byte 5. If an integer is four bytes long, then the integer occupies bytes 5,6,7,and 8.

Like I said, people who write compilers know that any time someone writes something like int *x = 5 it was likely a mistake. So the compiler complains.


yj1214 wrote:
char *y = "Hello";
This code is valid.
Duoas wrote:
const char * s = "Hello"; This is fine, since s is a pointer to the first character in the immutable character array "Hello".


yj1214 wrote:
Shouldn't char * needs an address? like this...
char *x = &y;
Yes, it needs an address; No, not that address.

It helps to pay attention to the type of thing you are looking at.

char *y --> y is a (char*)
y --> y is a (char*)
&y --> the address of y --> ((char*)*)
(char*) != ((char*)*)

It works the same for other things:

1
2
int x = 5;
int *y = &x;  // valid? 

&x --> pointer to (int) --> (int*)
y --> (int*)
(int*) == (int*)

So yes, int *y = &x is valid.

Hope this helps.
So why does char able to assign its value that isn't an address?


C and C++ make a special case for string literals because there is no other practical way to handle them.

A string literal... that is... something inside of quotes, like "Hello" effectively does 2 things:

1) It ensures that the literal string data of "Hello" will appear in the resulting compiled binary
2) It will resolve the literal to a pointer to the start of that string data (usually, anyway).

So, effectively:
1
2
3
4
5
6
7
8
9
// in this line of code:
const char* y = "Hello";

// "Hello" data gets put in memory somewhere:
//       memory[some_address] = "Hello";
//
// And then the "Hello" literal effectively gets replaced with the pointer to
//     wherever that data was placed:
const char* y = some_address; // <- conceptual 


Again, this is a special case that only exists for double-quoted string literals. And it only exists because strings are a very commonly used language construct, and there isn't really any better way of doing it.
Topic archived. No new replies allowed.