简体   繁体   中英

Difference between char* and int*

I'm sorry for asking a basic question that has definitely been asked before, but I just couldn't find it! Why in C can you assign a string literal to a char*, but cannot assign an integer literal to an int* ?

Because a string literal is translated to an array of char ( char [] ) during translation. C does not have a distinct string type.

As every array, it is converted for most usages (except for sizeof , _Alignof and the address-operator & ), it is converted ("decays") to a pointer to its first element. As this is a char * , there is no problem assigning it to a char * variable or passing as a char * argument.

An int /integer constant (the C standard does not use the term "literal" here) OTOH can be converted to a pointer, but that is not what you normally want. Also the conversion is implementation defined. Thus the compiler emits a warning for such an assignment without explicit cast. To emphasise: there are very few cases you want to assign an integer constant to a pointer (eg embedded systems). None in typical PC programs.

To get the address of an int variable , use the & operator:

int i = 5;
int *p = &i;

If you need something similar to the string literal, qualify the variable const :

const int i = 5;

Note that this still is a single integer, so you must not add an offset to the pointer, eg p[1] is undefined behaviour.

A string in C isn't a primitive data type. It is actually a 0 terminated sequence of chars. So char *sp = "literal"; is actually working with a pointer to a sequence of chars.

An int on the otherhand is a primitive type, so when you use a literal int, it is an int with no tricks to make it look like something else.

In C, a string is an array of characters. When an array is used as a the value of an expression, it's usually automatically converted to a pointer to its first element (there are some exceptions, they're not important here), so you can assign this to a pointer variable.

Other literals are not automatically converted to pointers, so you can't assign them to pointers without casting (and even if you do cast, the result is not portable). So the reason you can't do:

int *intptr = 123;

is the same reason you can't do:

char *charptr = 'a';

Note that there's nothing special about char* . You can assign an int* from an int array, eg

int intarray[] = {1, 2, 3};
int *intptr = intarray;

AC string literal is an array of type char (composed of many chars). An integer is a single item of type int.

This is why the syntax for assigning each to a pointer differs.

Assigning an array of type char or int to a pointer would use the same syntax, as would assigning a single item of type char or int to a pointer.

When you say

char *str = "Hello";

what happens is just about exactly the same as if you had said

const char tmpstr[] = {'H', 'e', 'l', 'l', 'o', '\0'};
char *str = tmpstr;

That is, whenever you write a string constant like "Hello", the compiler automatically allocates a little static character array containing your string. And it's perfectly okay to use this array as the initializer for a character pointer. (There's actually a potential little discrepancy as to whether the string constant is an array of const char or not, but let's not worry about that for now.)

So if you said

int tmpi[] = {1, 2, 3, 4, 5};
int *ip = tmpi;

that would work perfectly fine, too.

But you can't assign an integer to an int * , because that doesn't make sense:

int *ip2 = 123;    /* WRONG */

On the right we have an integer 123, and on the left we have an int * ,and you can't assign an integer to a pointer.

(Now, if you really want to, you can try to assign a raw memory address to a pointer, but let's not worry about that for now, either.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM