Reputation: 915
I understand the basics of how pointers work, but the following example confuses me.
int *myNum = 10; // Produces an error
char *myChar = "Something"; // Works fine
Why does assigning char work but integer doesn't (Maybe cause char is treated as an array)?
As well what confuses me when directly assigning a pointer variable, does it automatically get an address?
char *myChar = "Something";
and
char myChar = "Something";
char *charAddr = &myChar;
What would be the difference here, or equals?
Upvotes: 14
Views: 3012
Reputation: 58858
"Something"
is essentially short for:
static const char some_hidden_array[] = {'S', 'o', 'm', 'e', 't', 'h', 'i', 'n', 'g', '\0'};
some_hidden_array
That is, when you write "Something"
, the compiler generates an array behind the scenes, and gives you a pointer to the start of that array. Since this is already a pointer to a char, you'll have no problem assigning it to a variable of type "pointer to a char" (written as char*
).
10
is not short for anything similar. It's just the number 10 - it's not a pointer to an array containing the number 10, or anything like that.
Note that a char
is a single character, not a string, which is why the string syntax is unusual compared to most other types - a string is several chars, not just one. If you try to use a plain old char
, you'll see the same thing:
char *myChar = 'a'; // error
or for any other type:
float *myFloat = 42.1f; // error
In other words, it's not strange that 10
gives an error - if anything, it's strange that "Something"
doesn't. (At least, it's strange until you know how string literals work)
Upvotes: 22
Reputation: 4236
Why does assigning char work but integer doesn't (Maybe cause char is treated as an array)?
You are right, "Something"
is a string literal and can be treated as char array. After char *myChar = "Something";
the following thing happen: it is allocated length+1 bytes of memory where "Something"
will be stored, myChar
is pointed to the starting address of this memory. String literals are somewhat special.
Here is a general way of initializing array with constant values:
// valid initializations;
char s2[] = { 'a', 'b', 'c' };
int a[] = { 1, 2, 3 };
char s1[] = "123";
As well what confuses me when directly assigning a pointer variable, does it automatically get an address?
Yes.
Take a look at 8.5.2 Character arrays of c++ docs
Upvotes: 3
Reputation: 885
When you do char *myChar = "Something";
, you create a read-only string literal somewhere in the memory, which ends in a null character. Now this is something special with the compiler, that it interprets a chunk of 'char' variables stored continuously and ending with a null character as string. So basically you created an array of characters, and when you do *myChar*
, it returns the string.
In case of integers or any other data types, it differentiates between int *ptr
as a pointer to an integer, and int ptr
as an integer. You are getting an error probably because the address you entered may not be valid/available to you.
Also, doing
char myChar = "Something"; //this is an error, since char can hold one character
char *charAddr = &myChar;
Note that myChar
and &myChar
are the same, since myChar
is a pointer!
Edit: Refer here about string literals: Is it possible to modify a string of char in C?
Upvotes: 2
Reputation: 57774
While theoretically the first int *myNum = 10
makes sense—especially if you know there is a useful int
at address ten—in general it is rarely useful and potentially quite dangerous.
However, there are certain pointer assignments which are widely used and quite safe:
int *myNum = 0;
On 99.9+% of modern CPU architectures, this is the same as
int *myNum = NULL;
See the definition of NULL in <stddef.h>
here.
As a general rule, assignment of pointer variables is best done by setting to the address of something else.
int k, *p = &k;
Upvotes: 1
Reputation: 5702
It's the same thing (no magic from the compiler is happening). By default, literals like 10 are int values, not int*.
You need to cast:
int *myNum = (int*)10; // Need to cast
char *myChar = "Something"; // No need to cast "..." is already a char*
Note that it's dangerous to reference a pointer to absolute value like this because you will end up with the address 10 in CPU memory.
Regarding your second question, "..." is treated as a contiguous sequence of char in memory similar to array and equivalent to char*.
For a thoughtful understanding of C, pointers and differences between arrays and pointers, you should read this: Expert C Programming: Deep C Secrets by Peter van der Linden.
Upvotes: 10