structure {
char itable;
int num[20];
char * togs
}
structure
应为struct
;- 左花括号前应该有一个标记,或者在右花括号前应该有一个结构变量名(匿名结构模板方式);
- togs后缺少分号;
- 模板结尾缺少分号。
#include <stdio.h>
struct house {
float sqft;
int rooms;
int stories;
char address[40];
};
int main(void)
{
struct house fruzt = {1560.0, 6, 1, "22 Spiffo Road"};
struct house *sign;
sign = &fruzt;
printf("%d %d\n", fruzt.rooms, sign->stories);
printf("%s \n", fruzt.address);
printf("%c %c\n", sign->address[3], fruzt.address[4]);
return 0;
}
输出内容如下:
6 1
22 Spiffo Road
S p
struct month {
char name[10];
char abbr[4];
int days;
int monnumb;
};
struct month 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 }
};
extern struct month months [];
int days(int month)
{
int i, count;
if (month < 1 || month > 12)
return -1;
else
{
for (i = 0, count = 0; i < month; i++)
count += months[i].days;
return count;
}
}
- a.假设有下面的 typedef,声明一个内含 10 个指定结构的数组。然后,单独给成员赋值(或等价字符串),使第3个元素表示一个焦距长度有500mm,孔径为f/2.0的Remarkata镜头。
typedef struct lens { /* 描述镜头 */
float foclen; /* 焦距长度,单位为mm */
float fstop; /* 孔径 */
char brand[30]; /* 品牌名称 */
} LENS;
LENS bigEye[10];
bigEye[2].foclen = 500;
bigEye[2].fstop = 2.0;
strcpy(bigEye[2].brand, "Remarkatar");
- b.重写a,在声明中使用一个待指定初始化器的初始化列表,而不是对 每个成员单独赋值。
LENS bigEye[10] {[2] = { 500.0, 2.0, "Remarkatar" }};
struct name {
char first[20];
char last[20];
};
struct bem {
int limbs;
struct name title;
char type[30];
};
struct bem * pb;
struct bem deb = {
6,
{ "Berbnazel", "Gwolkapwolk" },
"Arcturan"
};
pb = &deb;
- a.下面的语句分别打印什么?
printf("%d\n", deb.limbs);
printf("%s\n", pb->type);
printf("%s\n", pb->type + 2);
打印内容如下:
6
Arcturan
cturan
- b.如何用结构表示法(两种方法)表示"Gwolkapwolk"?
deb.title.last
pb->title.last
- c.编写一个函数,以bem结构的地址作为参数,并以下面的形式输出结构的内容(假定结构模板在一个名为starfolk.h的头文件中):
Berbnazel Gwolkapwolk is a 6-limbed Arcturan.
#include <stdio.h>
#include <starfolk.h>
struct bem * pb;
struct bem deb = {
6,
{ "Berbnazel", "Gwolkapwolk" },
"Arcturan"
};
pb = &deb;
void show(const struct bem * pb);
int main(void)
{
show(pb);
return 0;
}
void show(const struct bem * pb)
{
printf("%s %s is a %d-limbed %s\n",
pb->title.first, pb->title.last, pb->limbs, pb->type);
}
struct fullname {
char fname[20];
char lname[20];
};
struct bard {
struct fullname name;
int born;
int died;
};
struct bard willie;
struct bard *pt = &willie;
- a.用willie标识符标识willie结构的born成员。
willie.born
- b.用pt标识符标识willie结构的born成员。
pt->born
- c.调用scanf()读入一个用willie标识符标识的born成员的值。
scanf("%d", &willie.born);
- d.调用scanf()读入一个用pt标识符标识的born成员的值。
scanf("%d", &pb->born);
- e.调用scanf()读入一个用willie标识符标识的name成员中lname成员的 值。
scanf("%s", &willie.name.lname);
- f.调用scanf()读入一个用pt标识符标识的name成员中lname成员的值。
scanf("%s", &pb->name.lname);
- g.构造一个标识符,标识willie结构变量所表示的姓名中名的第3个字母(英文的名在前)。
willie.name.fname[2]
- h.构造一个表达式,表示willie结构变量所表示的名和姓中的字母总数。
strlen(willie.name.fname) + strlen(willie.name.lname)
struct car {
char name[20];
float hp;
fload rate;
float wbase;
int year;
};
struct gas {
float distance;
float gals;
float mpg;
};
- a. 设计一个函数,接受
struct gas
类型的参数。假设传入的结构包含 distance和gals信息。该函数为mpg成员计算正确的值,并把值返回该结构。
struct gas getmpg(struct gas info)
{
if (info.gals > 0)
info.mpg = info.distance / info.gals;
else
info.mpg = -1.0;
return info;
}
- b.设计一个函数,接受
struct gas *
类型的参数。假设传入的结构包含 distance和gals信息。该函数为mpg成员计算正确的值,并把该值赋给合适的 成员。
void getmpg(struct gas * pt)
{
if (pt->gals > 0)
pt->mpg = pt->distance / pt->gals;
else
pt->mpg = -1.0;
}
enum choices = { no, yes, maybe };
char * (* fp)(char *, char);
double fn1(double x, double y);
double fn2(double x, double y);
double fn3(double x, double y);
double fn4(double x, double y);
double (*fp[4])(double x, double y) = { fn1, fn2, fn3, fn4 };
fp[1](10.0, 2.5);
(*fp[1])(10.0, 2.5);