C - Difference between “char var[]” and “char *var”?

后端 未结 4 1214
悲&欢浪女
悲&欢浪女 2020-11-27 06:49

I am expecting that both following vectors have the same representation in RAM:

char a_var[] = \"XXX\\x00\";
char *p_var  = \"XXX\";

But st

4条回答
  •  無奈伤痛
    2020-11-27 07:36

    Arrays can be treated (generally) as pointers but that doesn't mean that they are always interchangeable. As the other said, your p_var points to a literal, something static that cannot be changed. It can point to something else (e.g. p_var = &a_var[0]) but you can't change the original value that you specified by quotes....

    A similar problem is when you are define a variable as an array in one file, and then extern-use it as a pointer.

    Regards

提交回复
热议问题