C language memory alignment judgment date struct knowledge, alignment

Source: Internet
Author: User

C language memory alignment judgment date struct knowledge, alignment

1. struct Declaration

2. typedef? ? Leve upgrade
Typedef actually creates a new name for this struct, test4;
Typedef struct test3 test4;
Test4 is equivalent to struct test3;
3.
Several special methods of typedef
Typedef int (* pfun) (int, int) // function pointer
Typedef int (* p) [10]? ? Array pointer

Remove typedef to see what type
Tyepedef int ARR [10]

ARR arr
4.
Def? // Const? Start alias? ? Character replacement ??

Const int * p1; // const modifier * the address and edge of the referenced content cannot be changed
Int * const? P2 ;? // Const modifier p2. Can the content be changed? ? The address cannot be changed.

5 .? ? "*" And the first combination


6. const? * P
? Int * const p


7 .? ?.? Operator? And? ->

If there is a pointer above, use->

8 .? * Unreference? ? Declare pointer

8. Usage of strcpy

9 .? ? Auto-Reference

11. Some code?
? 1. Student struct variable age name?
? 2. strcpy () in the array, the characters are saved one by one, so you need to use strcpy to change the string.

12. Memory alignment

? A. The preceding address must be an integer multiple of the following address.
? B. The length of the entire struct must be a positive multiple of the longest byte !~~
C. if the program has the # pragma pack (n) pre-compiled command, all members are aligned with n Bytes (that is, the offset is an integer multiple of n), and the current type and maximum schema type are not considered.
13. Why memory alignment?

? ? Byte alignment is mainly used to improve memory access efficiency, such as intel 32-bit cpu. Each Bus Cycle reads 32-bit memory data from an even address, if the data storage address does not start from an even number, it may take two bus cycles to read the desired data. Therefore, alignment is required when data is stored in the memory .?

 

Code Section:

/ / Define a structure variable (year, month and day) to calculate the day is the first day of the year
 
#include<stdio.h>
#include<string.h>
//#define PI 3
Struct MyList1
{
Int num ;
Int age ;
Char *name;
Char sex[8];
};
 
Typedef struct MyList2
 
{
Int num ;
Int age ;
Char name[15];
Char *sex;
}MyList2;
 
Typedef struct MyList3
{
Char *name;
Int num;
}list;
 
 
Void Show1(list *q4 , int len ) //for loop
 
{
Int i ;
For(i=0;i<len;i++)
{
Printf("%2s %2d \n",q4[i].name,q4[i].num);
 
}
}
 
Void Calculate1(int year,int month ,int day ) //Do not add structure variables
{
Int count;//
Int i ;
Count = day;
Int arr[12]={31,28,31,30,31,30,31,31,30,31,30,31};
For(i=0;i<month-1;i++) //The calculation does not include all dates of the month plus the current date
{
Count = count + arr[i];
}
If( ((year%4==0 && year%100!=0)||(year%400==0)) &&(month>2))
{
 
Printf("This year is Leap Year \n");
Printf ("And today is the %d year's %d day \n",year,count+1);
}
Else
{
Printf("This year is not Leap Year \n ");
Printf("And today is the %d year's %d day \n",year,count);
}
 
}
 
Struct Date
{
Int year;
Int month;
Int day;
}date;
Void Calculate2(int year,int month ,int day ) //Do not add structure variables
{
Int count;//
Int i ;
Count = day;
Int arr[12]={31,28,31,30,31,30,31,31,30,31,30,31};
For(i=0;i<month-1;i++) //The calculation does not include all dates of the month plus the current date
{
Count = count + arr[i];
}
If( ((year%4==0 && year%100!=0)||(year%400==0)) &&(month>2))
{
 
Printf("This year is Leap Year \n");
Printf ("And today is the %d year's %d day \n",date.year,count+1);
}
Else
{
Printf("This year is not Leap Year \n ");
Printf("And toda is the %d year's %d day \n",date.year,count);
}
 
}
 
////switch error
//void Calculate3(int year,int month ,int day ) //switch statement
//{
// switch (date.month)
// {case 1: day=date.day; break;
// case 2: day=date.day+3; break;
// case 3: day=date.day+59; break;
// case 4: day=date.day+90; break;
// case 5: day=date.day+120; break;
// case 6:day =date.day+151; break;
// case 7: day=date.day+181; break;
// case 8: day=date.day+212; break;
// case 9: day=date.day+243; break;
// case 10: day=date.day+273; break;
// case 11: day=date.day+304; break;
// case 12: day=date.day+334; break;
// break;
// }
// if ((date.year %4== 0 && date.year % 100 != 0 ||date.year % 400 == 0) && date.month >=3)
// {
// day+=1;
// printf("This year is Leap Year \n");
// printf ("And today is the %d year's %d day \n",date.year,date.day);
// }
// else
// {
// printf("This year is not Leap Year \n ");
// printf("And toda is the %d year's %d day \n",date.year,date.day);
// }
//}
 
 
//Use the . operator and the -> operator
Typedef struct A
{
Char a;
Int b;
 
Struct A *pe;
}A;
 
Typedef struct B
{
A a;
Int b;
Struct A * pc;
}B;
 
Struct C // memory alignment
{
Int c;
Char a; / / the previous address must be an integer multiple of the following address
Short b;

};
Int main()
{
//1. Luban No. 7 general struct output
Struct MyList1 q1= {1,6,"LUBAN","nan"};
Q1.name = "LUBAN7HAO";
Printf("%d,%d,%s,%s\n",q1.num,q1.age,q1.name,q1.sex);
 
//2. Luban No. 7 typedef output strcpy Cao Mengde
MyList2 q2={2,9,"LUBAN7","NAN"};
Q2.num = 4;
Strcpy(q2.name,"caomengde");
Printf("%d %d %s %s\n",q2.num,q2.age,q2.name,q2.sex);
List q4[4]= {{"luban1",100},{"luban2",99},{"luban3",94},{"luban6",92}};
 
//3.show function output king glory for loop printing
Int len = sizeof(q4)/sizeof(q4[0]);
Show1(q4,len);
 
// 4 define replace
//printf ("%d\n", PI);
 
//5 Calculate date without structure variable
Calculate1 (2008, 3, 15);
 
//6 Calculate date plus structure variable
Calculate1(2019,4,15);
////error 7 Calculate the date switch statement
//Calculate3(2016,4,5);
 
//8 use . operator and -> pointer
B b;
B.a.a; //Access a in A through B
B.pc; / / pc no pointer in front of.
B.a.pe;//pe has no pointer in front.
B.pc->pe; // pe has a pointer in front of pc so use ->
 
Printf ("the len is is %d ",sizeof(C));
 
Getchar();
Return 0;
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.