Difference between char* and char[]

前端 未结 7 1239
清歌不尽
清歌不尽 2020-11-27 03:32

I know this is a very basic question. I am confused as to why and how are the following different.

char str[] = \"Test\";
char *str = \"Test\";
7条回答
  •  無奈伤痛
    2020-11-27 03:39

    The diference is the STACK memory used.

    For example when programming for microcontrollers where very little memory for the stack is allocated, makes a big difference.

    char a[] = "string"; // the compiler puts {'s','t','r','i','n','g', 0} onto STACK 
    
    char *a = "string"; // the compiler puts just the pointer onto STACK 
                        // and {'s','t','r','i','n','g',0} in static memory area.
    

提交回复
热议问题