difference of char *s && char s[

2014-04-17  本文已影响0人  Roy_he
#include<stdio.h>
int main()
{
    char *s1 = "hello World";
    //s1[0] = 'H';//error
    char s2[] = "hello world";
    //s2[1] = 'H';//right
    printf("s1   = %p", s1);
    printf("s2   = %p", s2);
    printf("main = %p", main);
    return 0;
}

代码分析:
1.两个"hello World"均为常量,保存在代码区,而代码区是只读的
2.程序执行char s2[] = "hello World";时,是将代码区的"hello World"拷贝到堆

运行结果如下:
s1 = 0x4006b4
s2 = 0x7fffe9909ad0
main= 0x40059d

上一篇 下一篇

猜你喜欢

热点阅读