的char []和char *区别? [英] Difference between char[] and char*?
问题描述
可能重复:结果
Ç - 之间与ldquo差; VAR的char []”的和“字符* VAR和rdquo ;? 结果
<一href=\"http://stackoverflow.com/questions/2938895/difference-between-char-a-string-char-p-string\">Difference字符之间的[] =&ldquo;串rdquo ;;的char * p =&ldquo;串rdquo ;;
块引用>有人会解释究竟是什么的char []和char *之间的区别是什么?
例如差异
对于之间的字符名称[] =土;
和
的char * name =土;
感谢
解决方案字符NAMEA [] =土;
字符* PNAME =土;一个是一个数组(名称
NAMEA
指的块的字符)。另一种是指向一个字符(名称
PNAME
指一个指针,这恰好指向字符块的第一个字符)。虽然前者经常会衰变为后者,这并非总是如此。试着做一个
的sizeof
对他们既要明白我的意思。数组的大小,好了,数组的大小(六个字符,包括终端为空)。
指针的大小取决于你的指针宽度(4或8,或其他)。什么
PNAME
的指向的大小不是数组,但第一个字符。因此这将是1您也可以的移动的像
PNAME ++
的事情(除非他们宣布不变,像指针字符* const的PNAME = ...;
当然)。不能移动数组名指向它的第二个字符(NAMEA ++;
)。Possible Duplicate:
C - Difference between “char var[]” and “char *var”?
Difference between char a[]=“string”; char *p=“string”;would someone explain what exactly the difference between char[] and char* is? for example difference between
char name[] = "earth";
and
char *name = "earth";
thanks
解决方案char namea[] = "earth"; char *pname = "earth";
One is an array (the name
namea
refers to a block of characters).The other is a pointer to a single character (the name
pname
refers to a pointer, which just happens to point to the first character of a block of characters).Although the former will often decay into the latter, that's not always the case. Try doing a
sizeof
on them both to see what I mean.The size of the array is, well, the size of the array (six characters, including the terminal null).
The size of the pointer is dependent on your pointer width (4 or 8, or whatever). The size of what
pname
points to is not the array, but the first character. It will therefore be 1.You can also move pointers with things like
pname++
(unless they're declared constant, with something likechar *const pname = ...;
of course). You can't move an array name to point to it's second character (namea++;
).这篇关于的char []和char *区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!