简体   繁体   English

char *和int *之间的区别

[英]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* ? 为什么在C中可以将字符串常量分配给char *,而不能将整数常量分配给int *?

Because a string literal is translated to an array of char ( char [] ) during translation. 因为在转换过程中字符串文字会转换为charchar [] )数组。 C does not have a distinct string type. C没有独特的字符串类型。

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. 作为每个数组,它都会转换为大多数用法( sizeof_Alignof和地址运算符&除外),它会转换(“衰减”)为指向其第一个元素的指针。 As this is a char * , there is no problem assigning it to a char * variable or passing as a char * argument. 由于这是一个char * ,将其分配给char *变量或作为char *参数传递没有问题。

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. 可以将一个int / integer常量(C标准在这里不使用术语“文字”)OTOH转换为指针,但这不是您通常想要的。 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. 在典型的PC程序中没有。

To get the address of an int variable , use the & operator: 要获取int 变量的地址,请使用&运算符:

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

If you need something similar to the string literal, qualify the variable const : 如果您需要类似于字符串文字的内容,请限定变量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. 请注意,这仍然是单个整数,因此您不得向指针添加偏移量,例如p[1]是未定义的行为。

A string in C isn't a primitive data type. C中的字符串不是原始数据类型。 It is actually a 0 terminated sequence of chars. 它实际上是一个以0终止的字符序列。 So char *sp = "literal"; 所以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. 另一方面,int是原始类型,因此当您使用文字int时,它是一个没有技巧的int使其看起来像其他东西。

In C, a string is an array of characters. 在C语言中,字符串是字符数组。 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* . 注意, char*没有什么特别的。 You can assign an int* from an int array, eg 您可以从一个int数组中分配一个int* ,例如

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

AC string literal is an array of type char (composed of many chars). AC字符串文字是char类型的数组(由许多char组成)。 An integer is a single item of type int. 整数是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. 将char或int类型的数组分配给指针将使用与将char或int类型的单个项分配给指针相同的语法。

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. 也就是说,每当您编写一个字符串常量(如“ Hello”)时,编译器就会自动分配一个包含您的字符串的静态字符串数组。 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.) (实际上,在字符串常量是否为const char数组方面可能存在一点差异,但暂时不要担心。)

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 * ,因为这没有意义:

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. 右边有一个整数123,左边有一个int * ,您不能为指针分配一个整数。

(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.) (现在,如果您确实愿意,可以尝试为指针分配原始内存地址,但是现在也不要担心。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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