c - 如何重置函数中的结构?

标签 c function struct

我声明了一个结构并在主结构中将其初始化。 然后通过一个函数我想重置这个结构,但我似乎不能。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NOME 20
#define MAX_COGNOME 20
#define MAX_PASSAPORTO 9
#define MAX_LUOGO_DI_NASCITA 15

#define MAX_GIORNO 2
#define MAX_MESE 2
#define MAX_ANNO 4

typedef struct {
    char giorno[MAX_GIORNO+1];
    char mese[MAX_MESE+1];
    char anno[MAX_ANNO+1];
} data; //struct data/*

typedef struct {
    char nome[MAX_NOME+1];
    char cognome[MAX_COGNOME+1];
    data datadinascita;
    char luogodinascita[MAX_LUOGO_DI_NASCITA+1];
    char numeropassaporto[MAX_PASSAPORTO+1];
    int id;
} passeggero; //struct passeggero

void resetUtente();

int main() {

    passeggero utenti;

    utenti.id = 1;
    strcpy(utenti.nome, "John");
    strcpy(utenti.cognome, "McCabe");
    strcpy(utenti.datadinascita.giorno, "12");
    strcpy(utenti.datadinascita.mese, "02");
    strcpy(utenti.datadinascita.anno, "1996");
    strcpy(utenti.luogodinascita, "London");
    strcpy(utenti.numeropassaporto, "AA1234567");

    printf("USER BEFORE RESET:");
    printf("\n%d ------> %s %s - Born %s-%s-%s in %s | PASSPORT NUMBER: %s\n",utenti.id, utenti.nome,
            utenti.cognome, utenti.datadinascita.giorno, utenti.datadinascita.mese, utenti.datadinascita.anno,
            utenti.luogodinascita, utenti.numeropassaporto);

    resetUtente();

    printf("USER AFTER RESET:");
    printf("\n%d ------> %s %s - Born %s-%s-%s in %s | PASSPORT NUMBER: %s\n",utenti.id, utenti.nome,
            utenti.cognome, utenti.datadinascita.giorno, utenti.datadinascita.mese, utenti.datadinascita.anno,
            utenti.luogodinascita, utenti.numeropassaporto);

    return 0;
}

void resetUtente() {

    passeggero utenti;
    int i = 0;

    utenti.id = 0;
    utenti.nome[0] = '\0';
    utenti.cognome[0] = '\0';
    utenti.datadinascita.giorno[0] = '\0';
    utenti.datadinascita.mese[0] = '\0';
    utenti.datadinascita.anno[0] = '\0';
    utenti.luogodinascita[0] = '\0';
    utenti.numeropassaporto[0] = '\0';

}

resetUtente() 函数应将结构体的所有字段设置为无,但第二个 printf 始终打印“John McCabe - Born 12-02-1996 in London | PASSPORT NUMBER: AA1234567

最佳答案

void resetUtente(passeggero *u)
{
   memset(u, 0, sizeof(*u));
}

主要

 resetUtente(&utenti);

关于c - 如何重置函数中的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56313211/

相关文章:

Python - 将字节解包为 ascii 字符但带有重音符号

c++ - 在 C/C++ 中将月/日/年/时间转换为时间值

C HTTP 服务器/连接重置

c - 在C代码中,自由功能无法将内存清除掉

function - 试图在 Lua 中随机选择

c - 错误 : storage size of 'name' isn't known

c - C中如何进行就地计算

c - 我什么时候应该使用地址为 '&' & 符号键的 scanf ?

javascript - 获取 nouislider 函数之外的值

swift - 如何制作具有写时复制语义的容器? ( swift )