MST

星途 面试题库

面试题:C语言结构体成员访问与操作(高级难度)

给定以下结构体层次定义: ```c struct address { char city[20]; char street[30]; int zip; }; struct employee { char name[20]; int id; struct address emp_address; }; ``` 编写一个函数,该函数接收一个`struct employee`类型的数组指针和数组长度作为参数,查找出所有居住在特定城市(函数参数传入城市名)的员工信息并输出。要求使用指针操作来访问结构体成员。
27.9万 热度难度
编程语言C

知识考点

AI 面试

面试题答案

一键面试
#include <stdio.h>
#include <string.h>

void findEmployeesByCity(struct employee *employees, int length, const char *city) {
    for (int i = 0; i < length; i++) {
        if (strcmp((employees + i)->emp_address.city, city) == 0) {
            printf("Name: %s, ID: %d, City: %s, Street: %s, Zip: %d\n", 
                   (employees + i)->name, (employees + i)->id, 
                   (employees + i)->emp_address.city, (employees + i)->emp_address.street, 
                   (employees + i)->emp_address.zip);
        }
    }
}