数据结构day2

1.结构体字节对齐64位,32位,指定2字节对齐

struct data{
char t1;
char t2;
unsigned short t3;
unsigned long t4;
};

64位	16
//1
//1
//2	//4
//8
32位	8
//1
//1
//2
//4

struct data{
char t1;
int t2;
short t3;
};

64位	12
//1	//3
//4
//2	//2
32位	12
//1	//3
//4
//2	//2

struct s1
{
char c1;
int i;
char c2;
};
struct s2
{
char c1;
char c2;
int i;
};

s1
64位	12
//1	//3
//4
//1	//3
32位	12
//1	//3
//4
//1	//3

s2
64位	8
//1
//1	//2
//4
32位	8
//1
//1	//2
//4

typedef struct Test
{
short a;
struct
{
int b;
double c;
char d;
}p;
int e;
}Test;

64位	40
//2	//6
{//4	//4
//8
//1	//7


}//24
//4	//4
32位	24
//2	//2
{//4
//8
//1	//3
}//16
//4

typedef struct Test
{
short a;
struct
{
int b;
double c[10];
char d;
}p;
int e;
}Test;

64位	112
//2	//6
{//4	//4
//8*10
//1	//7
}//96
//4	//4
32位	96
//2	//2
{//4
//8*10	
//1	//3
}//88
//4

struct C{
char b;
int a;
short c;
};

64位	12
//1	//3
//4
//2	//2
32位	12
//1	//3
//4
//2	//2

struct C {
char a;
char b[3];
char c;
};

64位	5
//1
//1*3
//1
32位	5
//1
//1*3
//1

typedef struct
{
int b;
char a;
long e;
char c;
float d;
double t;
}node;

64位	32
//4
//1	//3
//8
//1	//3
//4
//8	
32位	28
//4
//1	//3
//4
//1	//3
//4
//8

//指定2字节对齐
#pragma pack(2)
typedef struct
{
	char c;
	struct A
	{
		short a;
		int *b;
		char c;
	}p;
	long b;
	int a;
}T;
#pragma pack()

64位	26
//1	//1
{//2
//8
//1	//1
}//12
//8
//4
32位	18
//1	//1
{//2
//4
//1	//1
}//8
//4
//4

2.两种验证大小端对齐的代码写一遍

//用union判断
#include <stdio.h>
union A
{
	char t1;
	int t2;
};
int main(int argc, const char *argv[])
{
	union A a1;   //栈区
	a1.t2 = 0x12345678;   //0x12数据高位  0x78数据低位
	printf("%#X\n",a1.t1);
	if (a1.t1==0x78)
	{
		printf("小端\n");
	}
	else
	{
		printf("大端\n");
	}
	return 0;
}
//用指针判断
#include <stdio.h>
int main(int argc, const char *argv[])
{
    int a = 0x12345678;
    char *p = &a; //为了只取a的低地址的数据

    if(*p==0x78)
    {
        printf("小端\n");
    }
    else
    {
        printf("大端\n");
    }

    return 0;
}

3.完善顺序表已写出的功能

//main.c

#include "seq_list.h"
int main()
{
	seq_p L = create_seq_list();
	printf("%d\n",seq_empty(L));
	printf("%d\n",seq_full(L));
	return 0;
}

//seq_list.c

#include "seq_list.h"

seq_p create_seq_list()
{
	seq_p L = (seq_p)malloc(sizeof(seq_list));
	if(L==NULL)
	{
		printf("空间申请失败\n");
		return;
	}
	L->len = 0;
	bzero(L,sizeof(L->data));
	return L;
}

int seq_empty(seq_p L)
{
	if(L==NULL)
		return -1;
	return L->len==0?1:0;
}
int seq_full(seq_p L)
{
	if(L==NULL)
		return -1;

	return L->len==MAX?1:0;
}

//seq_list.h

#ifndef _SEQ_LIST_H
#define _SEQ_LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 7
typedef int datatype;
typedef struct seq_list
{
	datatype data[MAX];
	int len;
}seq_list,*seq_p;
seq_p create_seq_list();
int seq_full(seq_p L);

int seq_empty(seq_p L);


#endif

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值