字符串:字符串变量

2020-11-02  本文已影响0人  爱生活_更爱挺自己

1、字符串常量

​ char* s = “Hello,world”;

#inlcude<stdio.h>

int main(void)
{
    int i=0;
    char *s = "Hello world";
    char *s2 = "Hello world";
    
    printf("&i=%p\n", &i);
    printf("s =%p\n", s);
    printf("s2=%p\n",s2);
    printf("Here!s[0]=%c\n", s[0]);
    
    return 0;
}
&i=0xbff1fd6c
s =0xe1f82
s2=0xe1f82
Here!s[0]=H

【释】s和s2指向相同地方的地址,地址很小,位于程序的代码段而且是只读的,如果试图修改其中的值,操作系统会有保护机制会让程序崩溃掉

#inlcude<stdio.h>

int main(void)
{
    int i=0;
    char *s = "Hello world";
    char *s2 = "Hello world";
    char s3[] = "Hello world";
    
    printf("&i=%p\n", &i);
    printf("s =%p\n", s);
    printf("s2=%p\n",s2);
    printf("s3=%p\n", s3);
    s3[0]='B';
    printf("Here!s3[0]=%c\n", s3[0]);
    
    return 0;
}
&i=0xbff03d64
s =0xfdf7c
s2=0xfdf7c
s3=0xbff03d50
Here!s3[0]=B

指针还是数组?

char *是字符串?

上一篇 下一篇

猜你喜欢

热点阅读