C Primer Plus 14.17 复习题

1、

复制代码
struct qwe {
	
	char itable;
	
	int num[20];
	
	char *togs;
	
}; 

2、

3、

复制代码
struct year {
	
	char month[10];
	
	char MONTH[4];
	
	int days;
	
	int numbers;
	
}; 

4、

复制代码
struct year months[12] ={
	
	{"January", "Jan", 31, 1},
	
	{"February", "Feb", 28, 2},
	
	{"March", "Mar", 31, 3},
	
	{"April", "Apr", 30, 4},
	
	{"May", "May", 31, 5},
	
	{"June", "Jun", 30, 6},
	
	{"July", "Jul", 31, 7},
	
	{"August", "Aug", 31, 8},
	
	{"September", "Sep", 30, 9},
	
	{"October", "Oct", 31, 10},
	
	{"November", "Nov", 30, 11},
	
	{"December", "Dec", 31, 12}
	
}; 

5、

复制代码
int days(const struct year months[], int month)
{
	
	int i, total = 0;
	
	if(month < 1 || month > 12)
	
		return 1;
		
	else
	
	{
	
		for(i = 0; i < month; i++)
			
			total += months[i].days;
	
		return total;
				
	}
}

6、

a.

复制代码
#include <string.h>

typedef struct lens {
	
	float foclen;
	
	float fstop;
	
	char brand[30];	

} LENS;

LENS arry[10];

arry[2].foclen = 500;

arry[2].fstop = 2.0;

strcpy(arry[2].brand ,"Remarkata");

/*
	
	因为数组名在C语言中是个常量指针,表示数组首元素的地址。而字符串也是个常量指针,
	直接赋值数组的话会改变地址,故可以选择给数组一个字符一个字符赋值,
	或者用 strcpy() 函数把字符串拷贝到数组中 

*/

b.

复制代码
#include <string.h>

typedef struct lens {
	
	float foclen;
	
	float fstop;
	
	char brand[30];	

} LENS;

LENS arry[10];

LENS arry[10] = 
{
	
    [2] = {  

        .foclen = 500.0,

        .fstop = 2.0,

        .brand = "Remarkata"  // 注意:这里可以写字符串常量
    }


};

/*

①与其它初始化方式混合: 

LENS arry[10] = {

    [2].foclen = 500.0,  // 只为特定元素的特定成员赋值

    [2].fstop = 2.0,

    [2].brand = "Remarkata",

    [5].brand = "Nikon"  // 可以只初始化部分成员

};

*/ 


/*

 ②全部使用指定初始化器:
 
 LENS arry[10] = {
   
    [0] = {.foclen = 50.0, .fstop = 1.8, .brand = "Sigma"},
   
    [1] = {.foclen = 85.0, .fstop = 1.4, .brand = "Sony"},
  
    [2] = {.foclen = 500.0, .fstop = 2.0, .brand = "Remarkata"},
  
    [3] = {.foclen = 35.0, .fstop = 1.4, .brand = "Zeiss"}
  


};

*/ 


/*

③部分初始化 + 传统初始化:

LENS arry[10] = {
   
    [2] = {500.0, 2.0, "Remarkata"},  // 指定索引,传统顺序
   
    {100.0, 4.0, "Tamron"},  // 没有指定索引,就是[0]
    
	[4] = {200.0, 2.8, "Tokina"}

};

*/ 

7、

a.

b.

复制代码
①

deb.title.last

②
 
pb->title.last

c.

复制代码
#include <stdio.h>
#include "starfolk.h"

struct name {

    char first[20];

    char last[20];

};

struct bem {

    int limbs;

    struct name title;

    char type[30];

};

void show(const struct bem *ptr);

int main(void)
{
    struct bem *pb;
   
    struct bem deb = {
   
        6,
   
        {"Berbnazel", "Gwolkapwolk"},
   
        "Arcturan"
   
    };

    pb = &deb;
   
    show(pb);
   
    return 0;
}

void show(const struct bem *fp)
{
    
	printf("%s %s is a %d-limbed %s.\n",fp->title.first, 
			fp->title.last, fp->limbs, fp->type);
				
}

8、

复制代码
a.

willie.born

b.

pt->born

c.

scanf(%d, &willie.born);

d.

scanf(%d, &pt->born);

e.

scanf(%s, willie.name.lname);

f.

scanf(%s, pt->name.lname);

g.

willie.name.fname[2]; 

h.

strlen(willie.name.fname) + strlen(willie.name.lname);

9、

复制代码
struct car {

	char name[20];
	
	float horsepower;
	
	float EAPMPG;
	
	float wheelbase;

	int year;

};

10、

a.

复制代码
struct gas mpgs(struct gas *fp)
{
 
    if(fp.gals > 0)
 
        fp.mpg = fp.distance / fp.gals;
 
    else
 
        fp.mpg = -1.0;
 
    return fp;

}

b.

复制代码
void set_mpgs(struct gas *fp)
{
   
    if(fp->gals > 0)
   
        fp->mpg = fp->distance / fp->gals;
   
    else
   
        fp->mpg = -1.0;

}

11、

12、

13、

复制代码
double function1(double , double );

double function2(double , double );

double function3(double , double );

double function4(double , double );

double (*fp[4])(double , double)= {

	function1, 
	
	function2, 
	
	function3, 
	
	function4

};


function1(10.0, 2.5);

(*fp[2])(10.0, 2.5);
相关推荐
z落落11 小时前
C# ArrayList 动态集合(接口/区别/API/深浅拷贝)+ List<T> 泛型集合
开发语言·c#
Cx330❀11 小时前
【Linux网络】从零构建高性能UDP服务器:从Echo到英译汉业务级实现
大数据·linux·服务器·开发语言·网络·c++·udp
basketball61611 小时前
Golang:基础语法总结
开发语言·后端·golang
兰令水11 小时前
leecodecode【双指针题2】【2026.5.26打卡-java版本】
java·开发语言·算法
不吃土豆的马铃薯11 小时前
TCP 三次握手 / 四次挥手详解
服务器·开发语言·网络·c++·网络协议·tcp/ip
金刚狼8811 小时前
用atomic解决全局变量跨线程修改的问题
c语言
ch.ju11 小时前
Java程序设计(第3版)第四章——引用
java·开发语言
Huangjin007_11 小时前
【C++ STL篇(十三)】无序关联容器 unordered_set / unordered_map解析
开发语言·c++
白日与明月11 小时前
pip下载库指定操作系统及python版本
开发语言·python·pip
折哥的程序人生 · 物流技术专研11 小时前
Qoder 1.0 完全指南:从安装到Agents驱动开发实战
开发语言·人工智能·python·ai编程