美文网首页
difference of char *s && char s[

difference of char *s && char s[

作者: Roy_he | 来源:发表于2014-04-17 14:49 被阅读0次
#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

相关文章

网友评论

      本文标题:difference of char *s && char s[

      本文链接:https://www.haomeiwen.com/subject/vcvltttx.html