btn char *和char []有什么区别 [英] What's the difference btn char * and char[]
本文介绍了btn char *和char []有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
char * a和char a [20]有什么区别?
请给我看一个简单的例子进行解释.不要太复杂.
What is the difference between char *a and char a[20]?
Please show me a simple example for an explanation. Don''t make it too complex.
Thanks in advance!
推荐答案
char* a
声明一个指向char
的指针.您可以随时重新分配a
:
char* a
declares a pointer to achar
. You can re-assigna
anytime:
char* a = "a string";
a = "another string";
char a[20]
声明一个固定大小数组.您不能更改a
的值,但是可以修改数组的内容:
char a[20]
declares a fixed size array. You can''t change the value of a
, but you can chande the content of the array:
char a[20] = "a string";
//this will not compile
a = "another string";
//but this will
strcpy(a, "another string");
//be carefull though not to copy more than 20 characters
//or you will have a buffer overflow
如果您有兴趣,可以在这里进行更深入的讨论->
声明char Str [16]时, Str是指针吗? [
If you''re interested, there is a more in-depth discussion here ->
When declare char Str[16] then Str is a pointer?[^]
char * a只是一个指向字符的指针,除了引用变量的空间之外,还有没有足够的空间来存储任何东西.
char b [20]保留用于易于使用的20个字符的空间.
该页面有很好的描述: http://c-faq.com/aryptr/index.html [ ^ ]
干杯,
char *a is only a pointer to a character and besides the space for the reference variable there is no space alotted for storing anything.
char b[20] reserves space for 20 characters that can be readily used.
This page has an good description: http://c-faq.com/aryptr/index.html[^]
Cheers,
这篇关于btn char *和char []有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文