- 求 sizeof(name1)?(晟安信息)
struct name1{
char str;
short x;
int num;
};
答案:8
str 是1 字节 + x是2 字节+ num是4 字节 = 8 字节
2.(电工时代)
typedef struct _a
{
char c1;
long i;
char c2;
double f;
}a;
typedef struct _b
{
char c1;
char c2;
long i;
double f;
}b;
sizeof(a) = _______;
sizeof(b) = _______;
答案:32 24
3.给了一个结构体,求 sizeof(struct A) = ________。 (鲁科安全,软通动力)
struct A{
char a;
char b;
char c;
short d;
int e;
short f;
}
答案:16
4.有一个如下的结构体,请问在64位编译器下用 sizeof(struct A) 计算出的大小是多少?( ) (鲁科安全)
struct A{
long a1;
short a2;
int a3;
int *a4;
}
A. 24 B. 28 C. 16 D. 18
答案:A
5.有以下说明语句,则下列错误的引用 是( )。(山大华天)
struct stu
{
int no;
char name[21];
};
stu w, *p = &w;
A. w.no B*p.no C. p->no D. (*p).no
答案:B
6.写出下述程序结果: (中维世纪)
typedef struct test
{
char x1;
short x2;
float x3;
char x4;
}TEST_T;
printf("%d", sizeof(TEST_T));
答案:12
7.下面的代码输出是什么,为什么?(64位环境) (信雅达)
struct {
char *a1;
long a2;
short a3;
}A;
int main(void)
{
printf("%d", sizeof(A));
}
答案:24
8.设有如下结构定义: struct student { int num; char name[20]; char sex; int age; char addr[30];} stud; 若用printf("%s\n", .....)访问该结构中name值的正确方法是 ( ) (杭州快越科技)
A. stud -> name B. &stud.name
C. stud.&name D. stud.name
答案:D
9.struct
{
short a; char b; float c;
}cs;
则sizeof(cs)的值是( ) (苏州特点电子科技)
A.4 B.5 C.7 D.8
答案:D
10.如下函数的输出结果是:【 】
struct node
{
char a; short b; char c; int d;
};
struct node s = { 3, 5, 6, 99 };
struct node *pt = &s;
printf("%X\n", *(int*)pt);
答案:63
11.编程题:定义学生结构体,存储学生的学号、姓名、分数,定义长度为5的结构体数组,实现:
①输入学生信息
②输出学生信息
③计算学生的成绩总分、平均分
④按照学生的分数进行排序
⑤输出排序后的数组
答案:
#include <stdio.h> #include <string.h> typedef struct { int id; char name[50]; float score; } Student; void inputStudents(Student students[], int count); void printStudents(const Student students[], int count); float calculateTotalScore(const Student students[], int count); float calculateAverageScore(const Student students[], int count); void sortStudentsByScore(Student students[], int count); int main() { const int numStudents = 5; Student students[numStudents]; printf("输入学生信息:\n"); inputStudents(students, numStudents); printf("\n学生信息:\n"); printStudents(students, numStudents); float totalScore = calculateTotalScore(students, numStudents); float averageScore = calculateAverageScore(students, numStudents); printf("\n成绩总分: %.2f\n", totalScore); printf("平均分: %.2f\n", averageScore); sortStudentsByScore(students, numStudents); printf("\n按分数排序后的学生信息:\n"); printStudents(students, numStudents); return 0; } void inputStudents(Student students[], int count) { for (int i = 0; i < count; i++) { printf("输入第 %d 位学生的信息:\n", i + 1); printf("学号: "); scanf("%d", &students[i].id); printf("姓名: "); scanf("%s", students[i].name); printf("分数: "); scanf("%f", &students[i].score); } } void printStudents(const Student students[], int count) { for (int i = 0; i < count; i++) { printf("学号: %d, 姓名: %s, 分数: %.2f\n", students[i].id, students[i].name, students[i].score); } } float calculateTotalScore(const Student students[], int count) { float total = 0; for (int i = 0; i < count; i++) { total += students[i].score; } return total; } float calculateAverageScore(const Student students[], int count) { float total = calculateTotalScore(students, count); return total / count; } void sortStudentsByScore(Student students[], int count) { for (int i = 0; i < count - 1; i++) { for (int j = i + 1; j < count; j++) { if (students[i].score > students[j].score) { Student temp = students[i]; students[i] = students[j]; students[j] = temp; } } } }