char * myString和myString []之间的区别

时间:2012-05-16 05:52:03

标签: c

  

可能重复:
  What is the difference between char s[] and char *s in C?
  What is the difference between char a[] = “string”; and char *p = “string”;

我想知道C语言下面两个字符串声明之间的区别是什么

char *myString = "This is a C character string";

char myString[] = "This is a C character array";

我知道第一个是指针,第二个是数组,但我无法区别。

1 个答案:

答案 0 :(得分:1)

除了Binyamin Sharet的回答,在第一种情况下你可以改变myString指针,但在第二种情况下你不能。所以,这段代码将编译并运行并打印“ello”:

#include <stdio.h>

int main(int argc, char **argv)
{
    char *myString = "Hello\n";

    myString++;

    printf("%s", myString);
}

但是这段代码不会编译:

#include <stdio.h>

int main(int argc, char **argv)
{
    char myString[] = "Hello\n";

    myString++;

    printf("%s", myString);
}