问题描述
请解释下面的指针初始化:
char * cptr =" test" ;; / *没有错误和警告* /
int * iptr = 10; / *只给出警告* /
float * fptr = 3.14; / *给出错误* /
为什么?
谢谢
Can you please explain the below pointer initializations:
char *cptr = "test"; /* gives no error and warnings */
int *iptr = 10; /* gives only warning */
float *fptr = 3.14; /* gives error */
Why?
Thanks
推荐答案
因为没有错误而且没有任何警告。
cptr现在指向(读取) -only)charachter数组的第一个字符
" test"
Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the charachter array
"test"
因为10可能是一个地址,但很可能不是,
最少没有来自您编写的地址空间由操作系统提供。
您可以使用10初始化一个int,但是应该初始化一个in * with
int的地址
Because 10 potentially could be an address, but most probably is not, at
least none from the address space you program has been given by the OS.
You can inititialize an int with 10, but should inititialize an in * with
the address of an int
因为地址不能是浮点值
你可以用3.14初始化一个浮点数,但不是浮动*,在这里你需要浮动的
地址。
Because an address can''t be a floating point value
You can initialite a float with 3.14, but not a float *, here you''d need the
address of a float.
再见,Jojo
Bye, Jojo
" test"是一个字符串文字,一个数组类型的表达式。在大多数
上下文中,包括这一个,数组类型的表达式是
隐式转换为指向数组的第一个元素的指针。
最好声明
const char * cptr =" test";
你不能修改a的内容字符串文字
(尝试这样做是未定义的行为),但对于历史
原因,字符串文字不是常量。
请参阅comp.lang.c常见问题解答中的第6部分,< http://www.c-faq.com>。
"test" is a string literal, an expression of array type. In most
contexts, including this one, an expression of array type is
implicitly converted to a pointer to the array''s first element.
It would be better to declare
const char *cptr = "test";
You''re not allowed to modify the contents of a string literal
(attempting to do so is undefined behavior), but for historical
reasons string literals are not const.
See section 6 of the comp.lang.c FAQ, <http://www.c-faq.com>.
这实际上是违反约束条件;编译器可以自由地拒绝它,而不仅仅是打印一个警告。这不会导致iptr
指向值为10的int对象;它试图使用
值10(类型为int)来初始化int *类型的对象。你的
编译器可能会让你把10作为一个
地址,从int隐式转换为int *。
如果你真的想这样做,你可以写:
int * iptr =(int *)10;
但它不太可能有意义。
如果你想让iptr指向一个值为10的对象,你必须创建这样一个对象:
:
int obj = 10;
int * iptr =& obj;
This is actually a constraint violation; the compiler is free to
reject it rather than just print a warning. This doesn''t cause iptr
to point to an int object with the value 10; it attempts to use the
value 10 (of type int) to initialize an object of type int*. Your
compiler is probably letting you get away with treating 10 as an
address, implicitly converted from int to int*.
If you really wanted to do that, you could write:
int *iptr = (int*)10;
but its not likely to be meaningful.
If you want iptr to point to an object with the value 10, you''re going
to have to create such an object:
int obj = 10;
int *iptr = &obj;
这会尝试将类型为double的值赋给类型为
float *的对象。你不能这样做。如果你想让ftpr指向一个值为3.14的浮动
对象,你将不得不创建这样一个对象,如上面的
。
-
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
诺基亚
我们必须做点什么。这是事情。因此,我们必须这样做。
- Antony Jay和Jonathan Lynn,是部长
This attempts to assign a value of type double to an object of type
float*. You can''t do that. If you want ftpr to point to a float
object with the value 3.14, you''ll have to create such an object, as
above.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
因为没有错误而且没有任何警告。
cptr现在指向(读取) -only)角色阵列的第一个字符
" test"
Because there no error and nothing to warn about.
cptr now points to the (read-only) first character of the character array
"test"
那么cptr的价值是多少?它会是一个地址类型的价值
还是横向字符串(test)本身?
因为当我尝试打印不同的值时,如:
printf(" \ n& cptr =%p",& cptr); / *打印cptr的地址* /
& cptr = 0x7fbffff938
printf(" \ ncptr =%p",cptr); / *打印cptr的值* /
cptr = 0x4005ec
但是当我尝试这样的时候:
printf(" \ ncptr =%s",cptr); / *打印cptr的值* /
cptr = test
printf(" \ ncptr =%s",* cptr); / *在cptr打印值* /
分段错误
cptr(名称)
------------ -------
|应该怎么回事?
|这里? | < - (这应该是某种地址的种类,
对吗?)
| |
-------------------
0x7fbffff938(cptr的地址)
So what will be the value of cptr? Will it be an address kind of value
or the lateral string ("test") itself?
Because when I try to print the different values like:
printf("\n&cptr = %p", &cptr); /* prints the address of cptr */
&cptr = 0x7fbffff938
printf("\ncptr = %p", cptr); /* prints value of cptr */
cptr = 0x4005ec
but when I try like this:
printf("\ncptr = %s", cptr); /* prints value of cptr */
cptr = test
printf("\ncptr = %s", *cptr); /* prints value at cptr */
Segmentation fault
cptr (name)
-------------------
| What should come |
| here? | <- (This should be some address kind of vale,
right?)
| |
-------------------
0x7fbffff938 (Address of cptr)
这篇关于了解指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!