了解C中的指针 [英] Understanding pointers in C
问题描述
我试图学习C语言中的指针,但是却与以下概念混淆了:
I am trying to learn pointers in C but is getting mixed up with the following concepts:
char *string = "hello"
char *string2;
两者之间的主要区别是什么
What is the main difference between:
A.) *string2 = string;
然后
B.) string2 = "bye";
推荐答案
某些图片可能会有所帮助.
Some pictures may help.
假定以下内存映射(地址完全是任意的,并且不反映任何已知的体系结构):
Assume the following memory map (addresses are completely arbitrary and don't reflect any known architecture):
Item Address 0x00 0x01 0x02 0x03
---- ------- ---- ---- ---- ----
"hello" 0x00501234 'h' 'e' 'l' 'l'
0x00501238 'o' 0x00
"bye" 0x0050123A 'b' 'y'
0x0050123C 'e' 0x00 0x?? 0x??
...
string 0x80FF0000 0x00 0x50 0x12 0x34
string2 0x80FF0004 0x?? 0x?? 0x?? 0x??
这显示了声明后的情况. "hello"
和"bye"
是字符串文字,它们作为char
某处"的数组存储在内存中,以便在程序的生存期内可用.请注意,尝试修改字符串文字的内容会调用未定义的行为.您不想将字符串文字(或指针表达式如string
求值为字符串文字的地址)作为函数的参数传递给scanf
,strtok
,fgets
等.
This shows the situation after the declarations. "hello"
and "bye"
are string literals, stored as arrays of char
"somewhere" in memory, such that they are available over the lifetime of the program. Note that attempting to modify the contents of string literals invokes undefined behavior; you don't want to pass string literals (or pointer expressions like string
that evaluate to the addresses of string literals) as arguments to functions like scanf
, strtok
, fgets
, etc.
string
是指向char
的指针,其中包含字符串文字"hello"
的地址. string2
也是char
的指针,其值是不确定的(0x??
表示未知的字节值).
string
is a pointer to char
, containing the address of the string literal "hello"
. string2
is also a pointer to char
, and its value is indeterminate (0x??
represents an unknown byte value).
写作时
string2 = "bye";
您将"bye"
(0x0050123A)的地址分配给string2
,所以我们的内存映射现在看起来像这样:
you assign the address of "bye"
(0x0050123A) to string2
, so our memory map now looks like this:
Item Address 0x00 0x01 0x02 0x03
---- ------- ---- ---- ---- ----
"hello" 0x00501234 'h' 'e' 'l' 'l'
0x00501238 'o' 0x00
"bye" 0x0050123A 'b' 'y'
0x0050123C 'e' 0x00 0x?? 0x??
...
string 0x80FF0000 0x00 0x50 0x12 0x34
string2 0x80FF0004 0x00 0x50 0x12 0x3A
看起来很简单,对吧?
现在让我们来看一下声明
Now let's look at the statement
*string2 = string;
这里有两个问题.
首先,题外话-C中的声明围绕着表达式的类型而不是对象. string2
是指向字符的指针;要访问字符值,必须使用一元*
运算符 dereference string2
:
First, a digression - declarations in C are centered around the types of expressions, not objects. string2
is a pointer to a character; to access the character value, we must dereference string2
with the unary *
operator:
char x = *string2;
表达式 *string2
的类型为char
,因此声明变为
The type of the expression *string2
is char
, so the declaration becomes
char *string2;
通过扩展名,表达式 string2
的类型为char *
或指向char
的指针.
By extension, the type of the expression string2
is char *
, or pointer to char
.
所以当你写
*string2 = string;
您正在尝试将类型char *
(string
)的值分配给类型char
(*string2
)的表达式.这是行不通的,因为char *
和char
是不兼容的类型.在翻译(编译)时会显示此错误.如果你写过
you're attempting to assign a value of type char *
(string
) to an expression of type char
(*string2
). That's not going to work, because char *
and char
are not compatible types. This error shows up at translation (compile) time. If you had written
*string2 = *string;
这两个表达式的类型均为char
,并且分配是合法的.
then both expressions have type char
, and the assignment is legal.
但是,如果尚未将任何内容分配给string2
,则其值不确定.它包含一个随机位字符串,该字符串可能与有效的可写地址相对应,也可能不相对应.试图尊重随机的,可能无效的指针值会导致未定义的行为;它可能看起来工作正常,可能会完全崩溃,也可能在这之间做任何事情.直到运行时才会出现此问题.更好的是,如果将字符串文字"bye"
分配给string2
,则会遇到上述问题.您正在尝试修改字符串文字的内容.同样,这是一个问题,直到运行时才会显示出来.
However, if you haven't assigned anything to string2
yet, its value is indeterminate; it contains a random bit string that may or may not correspond to a valid, writable address. Attempting to deference a random, potentially invalid pointer value invokes undefined behavior; it may appear to work fine, it may crash outright, it may do anything in between. This problem won't show up until runtime. Even better, if you assigned the string literal "bye"
to string2
, then you run into the problem described above; you're trying to modify the contents of a string literal. Again, that's a problem that's not going to show up until runtime.
这篇关于了解C中的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!