S1E49:基础typedef 课后作业

测试题:
0. 以下哪个选项正确使用了 typedef 关键字来定义一个新的类型名?
A. typedef int newInt;
B. type int newInt;
C. typedef newInt int;'
D. int typedef newInt;:
答:A

答案:A,解析:typedef 的语法是 typedef <已存在类型名称> <新的类型名称>;

1. 考虑以下代码:

typedef struct Node {

    int data;

    struct Node* next;

} Node;

在此代码中,Node 是以下哪种类型?
A. 一个新的基本数据类型
B. 一个指向结构体的指针
C. 一个结构体类型
D. 一个函数类型

答:A(错误

答案:C

解析:这里定义了一个结构体类型,并使用 typedef 将其命名为 Node


2. 请问下面哪种写法比较好,为什么?
A. typedef char* pStr;
B. #define pStr char*

答:A,define是把pStr替换为 char*,typedef 是给char*取个别名

答案:A 的写法要更好一些。

请看下面例子:

typedef char* pStr1;

#define pStr2 char* 



pStr1 s1,s2;

pStr2 s3,s4;

在上述的变量定义中,s1s2s3 都被定义为 char *,而 s4 则定义成了 char
不是我们所预期的指针变量,根本原因就在于 #define 只是简单的字符串替换而 typedef 则是为一个类型起新名字。
因此,在上述例子中 #define 语句必须写成 pStr2 s3, *s4; 这样才能正常执行。


3. 下列关于 typedef 的描述哪一项是错误的?
A. typedef 可以用来定义结构体的新名称。
B. typedef 可以用来定义指针的新名称。
C. typedef 可以用来定义函数的新名称。
D. typedef 只能用于基本数据类型。

答:D

答案:D

解析:typedef 可以用于任何类型,包括结构体、指针、函数类型等。


4. 请问下面代码中,p1 和 p2 等价吗?

#include <stdio.h>


typedef char *pStr;


int main(void)

{

        const pStr p1 = "FishC.com";

        const char *p2 = "FishC.com";        


        return 0;

}

答:等价(错误

答案:不等价!
解析:因为 const pStr p1 中,const 修饰的是 pStr 类型的变量 p1 为只读,而 const char *p2 修饰的则是 p2 指向的字符指针为只读。所以,如果 p1++; 会报错,而执行 p2++; 则没有问题。

5. 请问下面代码会打印什么内容?

#include <stdio.h>



char array[] = {'F', 'i', 's', 'h', 'C'};



#define LEN (sizeof(array) / sizeof(array[zxsq-anti-bbcode-0]))



int main(void)

{

        int i;



        for (i = -1; i < LEN - 1; i++)

        {

                putchar(array[i+1]);

        }

        putchar('\n');



        return 0;

}

答:array[zxsq-anti-bbcode-0])这是什么意思?(错误

答案:会打印一个换行符(请不要告诉我会打印 FishC,这么简单的问题我不会拿来考你们的……)
分析:
for 循环体内的语句一次都不会被执行,因为 sizeof 运算符的返回值是 size_t 类型,而 size_t 通常被定义为 unsigned int
当计算 i < LEN - 1 条件时,由于 < 运算符左右必须是相同类型的操作数,如果出现不同,会内部自动进行调整(范围小自动转换为范围大的数)。
所以带符号数会转换为无符号数再进行比较,即 -1 == 4294967295(32 位 unsigned int),所以 i < LEN - 1 的条件是无论如何也不会成立的!

6. 请解释 typedef 与 #define 在定义类型别名时的区别。

答:我认为,#define 是完全的替换 ,而typedef 是封装了

答案:typedef 和 #define 都可以用来定义类型别名,但有以下区别。

  • typedef 是编译器处理的,遵循 C 语言的类型规则,能够处理复杂类型如指针、数组、结构体等。
  • #define 是预处理器处理的文本替换,不能检查类型,因此在处理复杂类型时容易出错。

动动手:
0. 请按要求编写代码。

  • 定义一个表示二维点的结构体 Point,结构体包含两个 int 类型的成员 x 和 y。
  • 使用 typedef 为该结构体创建一个名为 Point 的别名。
  • 然后,定义一个 Point 类型的变量 p,并初始化 x 和 y 的值为 10 和 20。
  • 最后打印该点的坐标。

答:

#include <stdio.h>

typedef struct point
{
	int x;
	int y;
	
}Point; 


int main()
{	
	Point p;
	p.x = 10;
	p.y = 20;
	
	printf("x = %d, y = %d\r\n",p.x,p.y);
}

答案:

#include <stdio.h>

    

typedef struct

{

        int x;

        int y;

} Point;

    

int main()

{

        // 定义并初始化 Point 类型的变量

        Point p = {10, 20};

    

        // 打印点的坐标

        printf("点坐标是:(%d, %d)\n", p.x, p.y);

    

        return 0;

}

1. 定义一个带有嵌套结构体的别名。

  • 定义两个结构体:Point 表示二维点,包含 int 类型的成员 x 和 y;Rectangle 表示矩形,包含两个 Point 类型的成员 topLeft 和 bottomRight。使用 typedef 为这两个结构体创建别名。
  • 定义一个 Rectangle 类型的变量 rect,并初始化 topLeft 和 bottomRight 的坐标值为 (0, 10) 和 (10, 0)。
  • 最后打印矩形的四个顶点坐标。

答:

#include <stdio.h>

typedef struct point
{
	int x;
	int y;
	
}Point; 

typedef struct rectangle
{
	Point topLeft;
	Point bottomRight;
	
}Rectangle;



int main()
{	
	Rectangle rect;
	
	rect.topLeft.x = 0;
	rect.topLeft.y = 10;
	
	rect.bottomRight.x = 10;
	rect.bottomRight.y = 0;
	
	
	printf("Top Left: (%d, %d)\n", rect.topLeft.x, rect.topLeft.y);

    printf("Bottom Right: (%d, %d)\n", rect.bottomRight.x, rect.bottomRight.y);
}


答案:

#include <stdio.h>

    

// 定义 Point 结构体

typedef struct

{

        int x;

        int y;

} Point;

    

// 定义 Rectangle 结构体

typedef struct

{

        Point topLeft;

        Point bottomRight;

} Rectangle;

    

int main()

{

        // 定义并初始化 Rectangle 类型的变量

        Rectangle rect = {{0, 10}, {10, 0}};

    

        // 打印矩形的四个顶点坐标

        printf("Top Left: (%d, %d)\n", rect.topLeft.x, rect.topLeft.y);

        printf("Bottom Right: (%d, %d)\n", rect.bottomRight.x, rect.bottomRight.y);

    

        return 0;

}

2. 定义一个带有动态数组的结构体别名。

  • 定义一个表示动态整数数组的结构体 DynamicArray,包含 int* 类型的成员 array 和 size 表示数组大小的 int 类型的成员。使用 typedef 为该结构体创建别名。
  • 编写初始化动态数组的函数 initArray,接受一个 DynamicArray* 类型的参数和一个指定大小的 int 参数。
  • 编写释放数组内存的函数 freeArray。
  • 在 main 函数中,初始化一个大小为 5 的动态数组,并为其赋值。
  • 最后打印数组的所有元素并释放内存。

答:main函数自己写,其他参考答案

#include <stdio.h>


typedef struct
{
	int *array;
	int size;
		
}DynamicArray;


//初始化动态数组的函数 
void intArray(DynamicArray *arr, int size)
{
	arr->array = (int *)malloc(size *sizeof(int));
	arr->size = size;		
}


//释放数组内存 
void freeArray(DynamicArray *arr)
{
		free(arr->array);
		arr->array = NULL;
		arr->size = 0; 
}


int main()
{
	DynamicArray Myarray;
	int i = 0;
	
	intArray(&Myarray,5);
	
	for(i=0;i<5;i++)
	{
		*(Myarray.array+i) = i;
	}
	
	for(i=0;i<5;i++)
	{
		printf("%d\r\n",*(Myarray.array+i));
		
	}
	
	free(&Myarray);

}

答案:

#include <stdio.h>

#include <stdlib.h>

    

// 定义 DynamicArray 结构体

typedef struct

{

        int* array;

        int size;

} DynamicArray;

    

// 初始化动态数组的函数

void initArray(DynamicArray* arr, int size)

{

        arr->array = (int*)malloc(size * sizeof(int));

        arr->size = size;

}

    

// 释放数组内存的函数

void freeArray(DynamicArray* arr)

{

        free(arr->array);

        arr->array = NULL;

        arr->size = 0;

}

    

int main()

{

        // 定义 DynamicArray 类型的变量

        DynamicArray myArray;

    

        // 初始化数组大小为 5 的动态数组

        initArray(&myArray, 5);

    

        // 为数组赋值

        for (int i = 0; i < myArray.size; i++)

        {

                myArray.array[zxsq-anti-bbcode-i] = i + 1;

        }

    

        // 打印数组的所有元素

        for (int i = 0; i < myArray.size; i++)

        {

                printf("%d ", myArray.array[zxsq-anti-bbcode-i]);

        }

        printf("\n");

    

        // 释放数组内存

        freeArray(&myArray);

    

        return 0;

}

u#Ez,*

  • 23
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值