代码改变世界

char* []

2024-03-21 10:44  @学无止境  阅读(9)  评论(0编辑  收藏  举报
// strlen 不计'\0',sizeof 计'\0'(都查字节个数) 
    char x[] = "abcdefg中中";
    char y[] = { 'a','b','c','d','e','f','g','','\0' };
    cout << "length x-->" << strlen(x) << "--sizeof x-->" << sizeof(x) << endl;
    cout << "length y-->" << strlen(y) << "-- sizeof y-->" << sizeof(y) << endl;
    cout << "x-->" << x << endl;
    cout << "y-->" << y << endl;
    char a = '';
    cout << "a-->" << a << endl;

    cout << "---------------\n" << endl; 
    const char* str="abc";
    printf("sizeof str-->%d\n", sizeof(str));
    FILE* fp=fopen("a.txt", "w+");
    fwrite(str, sizeof(char), sizeof(str), fp);
    fclose(fp);
    fp = fopen("a.txt", "r+");
    char str2[8];
    fread(&str2, sizeof(char), sizeof(str), fp);
    printf("str2-->%s\n\n", str2);
    fclose(fp); 

    int arr[] = { 6,7,8 };
    cout << "arr length-->" << sizeof(arr) / sizeof(int) << "  sizeof-->" << sizeof(arr) << endl;

length x-->11--sizeof x-->12
length y-->8-- sizeof y-->9
x-->abcdefg中中
y-->abcdefg
a-->
---------------

sizeof str-->4
str2-->abc

arr length-->3 sizeof-->12

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

char txt[]="hello";
printf("sizeof txt-->%d\n",sizeof(txt));
const char* fname="a.txt";
int f= open(fname,O_RDWR);
write(f,txt,sizeof(txt));
close(f);      

char y[8]; 
f= open(fname,O_RDWR);
read(f,y,sizeof(txt));
printf("txt-->%s\n",y);
close(f);

 

sizeof txt-->6
txt-->hello