一、关于变量长度

#include <stdio.h>

int main(){
    printf("sizeof(char   )=%d\n", sizeof(char   ));
    printf("sizeof(int    )=%d\n", sizeof(int    ));
    printf("sizeof(char  *)=%d\n", sizeof(char  *));
    printf("sizeof(char **)=%d\n", sizeof(char **));    
    printf("//=================\n");
    return 0;
}

正常64位系统下输出,单位为字节

sizeof(char   )=1//8位
sizeof(int    )=4//32位
sizeof(char  *)=8//64位
sizeof(char **)=8//64位

CFLAGS 加入 -m32选项,遇到错误解决办法如下

apt-get update
apt-get purge libc6-dev
apt-get install libc6-dev
apt-get install libc6-dev-i386

输出

sizeof(char   )=1
sizeof(int    )=4
sizeof(char  *)=4//32位
sizeof(char **)=4//32位
arm-linux-gcc变量长度
测试代码
int my_printf_test2(void){
    printf("sizeof(char)=%d\n\r", sizeof(char));
    printf("sizeof(unsigned char)=%d\n\r", sizeof(unsigned char));
    
    printf("sizeof(int)=%d\n\r", sizeof(int));
    printf("sizeof(unsigned int)=%d\n\r", sizeof(unsigned int));
    
    printf("sizeof(short)=%d\n\r", sizeof(short));
    printf("sizeof(unsigned short)=%d\n\r", sizeof(unsigned short));

    printf("sizeof(long)=%d\n\r", sizeof(long));
    printf("sizeof(unsigned long)=%d\n\r", sizeof(unsigned long));
    
    printf("sizeof(char  *)=%d\n\r", sizeof(char  *));
    
    printf("sizeof(int *)=%d\n\r", sizeof(int *));

    printf("sizeof(short *)=%d\n\r", sizeof(short *));
    
    printf("sizeof(long *)=%d\n\r", sizeof(long *));
    
    return 0;
}
输出

clipboard.png

二、指针变量代码例子

指针基本操作
void test0(){
    char c;
    char *pc;
    char *pb;

    /*第一步 : 所有变量都保存在内存中,我们打印一下变量的存储地址*/
    printf("&c  =%p\n", &c);
    printf("&pc =%p\n", &pc);
    printf("&pb =%p\n", &pb);

    /*第二步:所有变量都可以保存某些值,接着赋值并打印*/
    c  = 'A';
    pc = &c;
    printf("c  =%c\n",c);
    printf("pc =%p\n",pc);

    /*第三步:使用指针:1)取值  2)移动指针*/
    printf("*pc =%c\n",*pc);    
    printf("//=================\n");    
}
//======输出=======
&c  =0xffb36093
&pc =0xffb36094
&pb =0xffb36098
c  =A
pc =0xffb36093
*pc =A
  • 首先看pc和c的地址差1表示1个字节(8位),因为char类型在内存中占1个字节
  • pb和pc的地址差4个字节,因为char *类型在内存中占4个字节
  • c中保存字符A,ASCII码大小为1个字节
  • pc保存c的地址0xffb36093(4字节、32位),以下以0xfff59ea7为例,展示在内存中的排列

  • pc中保存的地址(c的地址)指向的值(c的值)所以输出A

指针移动操作
void test1(){
    int  ia;
    int  *pi;
    char *pc;

    /*第一步 : 所有变量都保存在内存中,我们打印一下变量的存储地址*/
    printf("&ia =%p\n",&ia);
    printf("&pi =%p\n",&pi);    
    printf("&pc =%p\n",&pc);        

    /*第二步:所有变量都可以保存某些值,接着赋值并打印*/    
    ia = 0x12345678;
    pi = &ia;
    pc = (char *)&ia;
    printf("ia =0x%x\n",ia);    
    printf("pi =%p\n",pi);        
    printf("pc =%p\n",pc);

    /*第三步:使用指针:1)取值  2)移动指针*/
    printf("*pi =0x%x\n",*pi);            
    printf("pc =%p\t",pc);    printf("*pc =0x%x\n",*pc); pc=pc+1;
    printf("pc =%p\t",pc);    printf("*pc =0x%x\n",*pc); pc=pc+1;
    printf("pc =%p\t",pc);    printf("*pc =0x%x\n",*pc); pc=pc+1;
    printf("pc =%p\t",pc);    printf("*pc =0x%x\n",*pc);    
    printf("//=================\n");
//======输出=======
&ia =0xffaab350
&pi =0xffaab354
&pc =0xffaab358
ia =0x12345678
pi =0xffaab350
pc =0xffaab350
*pi =0x12345678
pc =0xffaab350    *pc =0x78
pc =0xffaab351    *pc =0x56
pc =0xffaab352    *pc =0x34
pc =0xffaab353    *pc =0x12
}
  • 关键的点是pc = (char *)&ia;让pc指向了一个char类型变量(pc储存的地址0xffaab350是一个char变量的地址),所以*pc只取1个字节,pc+1指向下一个char变量地址字节加1

void test2(){
    char ca[3]={'A','B','C'};
    char *pc;

    /*第一步 : 所有变量都保存在内存中,我们打印一下变量的存储地址*/
    printf("ca  =%p\n",ca);
    printf("&pc =%p\n",&pc);

    /*第二步:所有变量都可以保存某些值,接着赋值并打印*/
    //前面已经有ca[3]={'A','B','C'};
    pc = ca;
    printf("pc =%p\n",pc);

    /*第三步:使用指针:1)取值  2)移动指针*/
    printf("pc =%p\t",pc);    printf("*pc =0x%x\n",*pc); pc=pc+1;
    printf("pc =%p\t",pc);    printf("*pc =0x%x\n",*pc); pc=pc+1;
    printf("pc =%p\t",pc);    printf("*pc =0x%x\n",*pc);    
    printf("//=================\n");
//======输出=======
ca  =0xffa516a9
&pc =0xffa516a4
pc =0xffa516a9
pc =0xffa516a9    *pc =0x41
pc =0xffa516aa    *pc =0x42
pc =0xffa516ab    *pc =0x43    
}
  • ca是数组的首地址,在这里是3个char变量连续排列,ca为第一个char变量的地址
  • pc指向的是char变量所以+1移动1个字节

void test3(){
    int ia[3]={0x12345678,0x87654321,0x13572468};
    int *pi;

    /*第一步 : 所有变量都保存在内存中,我们打印一下变量的存储地址*/
    printf("ia  =%p\n",ia);
    printf("&pi =%p\n",&pi);

    /*第二步:所有变量都可以保存某些值,接着赋值并打印*/
    //前面已经有ia[3]={0x12345678,0x87654321,0x13572468};
    pi = ia;
    printf("pi =%p\n",pi);

    /*第三步:使用指针:1)取值  2)移动指针*/
    printf("pi =%p\t",pi);    printf("*pi =0x%x\n",*pi); pi=pi+1;
    printf("pi =%p\t",pi);    printf("*pi =0x%x\n",*pi); pi=pi+1;
    printf("pi =%p\t",pi);    printf("*pi =0x%x\n",*pi); 
    printf("//=================\n");    
}

//输出
ia  =0xff9f1e80
&pi =0xff9f1e7c
pi =0xff9f1e80
pi =0xff9f1e80    *pi =0x12345678
pi =0xff9f1e84    *pi =0x87654321
pi =0xff9f1e88    *pi =0x13572468
  • 分析过程同上,pi指向int 每次加1 加4个字节到下一个int

void test4(){
     char *pc="abc";
    /*第一步 : 所有变量都保存在内存中,我们打印一下变量的存储地址*/
    printf("&pc =%p\n",&pc);

    /*第二步:所有变量都可以保存某些值,接着赋值并打印*/
    //前面已经有pc="abc";
    
    /*第三步:使用指针:1)取值  2)移动指针*/
    printf("pc    =%p\n", pc);
    printf("pc+1    =%p\n", pc+1);
    printf("pc+2    =%p\n", pc+2);
    printf("*pc   =%c\n",*pc);
    printf("*(pc+1)   =%c\n",*(pc+1));
    printf("*(pc+2)   =%c\n",*(pc+2));
    printf("pc str=%s\n", pc);
}    
//输出
&pc =0xffd3c278
pc    =0x8048bd4
pc+1    =0x8048bd5
pc+2    =0x8048bd6
*pc   =a
*(pc+1)   =b
*(pc+2)   =c
pc str=abc
  • 字符串可以看做一个char类型的数组,char *pc="abc"相当于把保存字符a的内存地址(0x8048b64)赋值给pc
  • pc也是一个在内存中4字节的变量,地址为0xff8fd018保存"abc"的首地址

Kyseng
1 声望3 粉丝

电子爱好者一枚,利用工作空余时间记录一下学习过程