Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference in pointers when defining char and int?

Tags:

c++

pointers

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?

like image 535
J. Doe Avatar asked Mar 13 '16 23:03

J. Doe


People also ask

What is difference between char pointer and int pointer?

Type char is a byte wide type. This means, it can store 8 bits of information. So type char can store numbers from -127 to 126. Type int is a 4-byte wide type, so it can store 32 bits of information.

What is the difference between char and int?

Size of an int is 4 bytes on most architectures, while the size of a char is 1 byte. Note that sizeof(char) is always 1 — even when CHAR_BIT == 16 or more .

Why pointer to different data types are different?

Different types of pointers take different amounts of memory. So, in the case of advancing a pointer one needs to take the type's size into the account.

What is the difference between char a 5 and int A 5?

Answer. char a[5] is an array of char data type that can hold 5 characters whereas int a[5] is an array of int data type that can hold 5 integer values.


3 Answers

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.

like image 33
John Difool Avatar answered Oct 28 '22 10:10

John Difool


"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)

like image 133
user253751 Avatar answered Oct 28 '22 10:10

user253751


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

like image 41
Ivan Gritsenko Avatar answered Oct 28 '22 12:10

Ivan Gritsenko