C primer Plus_part6

第十章  数组和指针

1.const :保护变量不受改变,特别是在作为入参传入函数

             对于变量:const 不能修改值

             对于指针: const 可以修改值,但是不能修改指向对象

    

#include<stdio.h>

int main()
{
  int arrays [10] = {0};
  const int *p  = arrays;
  int i =0 ;
  *(p+3) = 4;//error 不能通过编译 const 并不智能

  for(;i < 10;i ++)
  {
    printf(" %d 
",arrays[i]);


  }
}

#include<stdio.h>

int main()
{
  int arrays [10] = {0};
  const int *p  = arrays;
  int i =0 ;
 // *(p+3) = 4;//error 不能通过编译 const 并不智能
  *(arrays+3) = 4;
  for(;i < 10;i ++)
  {
    printf(" %d 
",*(p+i));

  }
}


 

关于结构体的大小计算

   

#include<stdio.h>

typedef struct A_s
{
   int a[10];


}A;

 typedef struct data_s
{
      int a ;
        double dou;
        char b[4];
        //char ch;
        A  d;
       // int e;
        char c;
}data;

int main()
{
   data data_1={0};
       printf("%d  %d
",sizeof(data) ,sizeof(data_1.d));
}
~  

多次尝试后对于补位理解补齐一个最大的基本类型大小最大补为8。

#include<stdio.h>

typedef struct A_s
{
   int a[10];


}A;

 typedef struct data_s
{
      short a ;
       // double dou;
        //char b[4];
        //char ch;
       // A  d;
       // int e;
        char c;
}data;

int main()
{
   data data_1={0};
       printf("%d  %d
",sizeof(data) ,sizeof(A));
}

像这个补齐为4.

原文地址:https://www.cnblogs.com/liuchuanwu/p/7112986.html