使用具有结构的C创建地址簿

  • 本文关键字:创建 地址簿 结构 c
  • 更新时间 :
  • 英文 :


我的意图是使用C的结构创建一个包含10个联系人的地址簿。用户必须一个接一个地插入联系人,我的程序应该打印整个通讯簿。编译是成功的,但当我运行程序时,它只打印第一个联系人,然后出现分段错误。这是我的代码:

文件Ex7.h:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define DIM 10
struct person {
    char name[40];
    char surname[40];
    char date[11];
    char number[11];
};
typedef struct person Person;
void dataEntry(Person *person);
void printPerson(Person person);
void printAddressBook(Person *addbook[10]);

文件Ex7.c:

#include "ex7.h"
void dataEntry(Person *person) {
    printf("Insert name: ");
    gets(person -> name);
    printf("Insert surname: ");
    gets(person -> surname);
    printf("Insert date of birth [mm/dd/yyyy]: ");
    gets(person -> date);
    printf("Insert phone number: ");
    gets(person -> number);
}

void printPerson(Person person) {
    printf("Name: %sn", person.name);
    printf("Surname: %sn", person.surname);
    printf("Date of birth: %sn", person.date);
    printf("Number: %snn", person.number);
}

void printAddressBook(Person *addbook[10]) {
    for (int i = 0; i < DIM; i++) {
        printPerson(*addbook[i]);
    }
}

file-main.c:

#include "ex7.h"
int main(void) {
    Person* addbook = (Person*) malloc(DIM*sizeof(Person));
    printf("DATA ENTRYnn");
    for (int i = 0; i < DIM; i++) {
        printf("Person %d:n", i);
        dataEntry(&addbook[i]);
    }
    printf("nnPRINTING ADDRESS BOOK...nn");
    printAddressBook(&addbook);
    return 0;
}

然后我编译了所有键入的内容:gcc-o main main.c ex7.c-std=c99

问题出在这部分:

void printAddressBook(Person *addbook[10]) {
    for (int i = 0; i < DIM; i++) {
        printPerson(*addbook[i]);
    }
}

该方法需要一个person指针数组,其中主要传递指向person数组的指针,而不是您分配的person数组。因此,将方法更改为:

void printAddressBook(Person *addbook) {
    for (int i = 0; i < DIM; i++) {
        printPerson(addbook[i]);
    }
}

主要是这样称呼的:

printAddressBook(addbook);

首先不要使用gets。它已被弃用,不再是当前标准的一部分。fgets运行良好。在向每个函数传递指向结构集合的指针时,必须保持一致。另外,以这种方式使用malloc会引起Conditional jump or move depends on uninitialised value(s)valgrind的投诉。您可以使用calloc进行分配,以确保所有数据都初始化为NULL。纠正这些问题的一种方法如下。提供了一个新的输入例程,使用getline来获得输入,而不是"获取":

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define DIM 10
struct person {
    char name[40];
    char surname[40];
    char date[11];
    char number[11];
};
typedef struct person Person;
void dataEntry(Person *person);
void printPerson(Person *person);
void printAddressBook(Person *addbook);
int main(void) {
    Person* addbook = calloc (DIM, sizeof(Person)); /* calloc initializes all storage */
    printf("DATA ENTRYnn");
    for (int i = 0; i < DIM; i++) {
        printf("Person %d:n", i);
        dataEntry(&addbook[i]);
    }
    printf("nnPRINTING ADDRESS BOOK...nn");
    printAddressBook(addbook);
    if (addbook) free (addbook);
    return 0;
}
/* get string input using getline and write to 'string' */
void getinput (char *string)
{
    ssize_t read = 0;                   /* values to use with getline       */
    char *line = NULL;                  /* forces getline to allocate mem   */
    size_t n = 0;                       /* bytes to read, 0 no-limit        */
    read = getline (&line, &n, stdin);  /* read from stdin with getline     */
    if (line[read - 1] == 'n')         /* if newline at end of line        */
        { line[read - 1] = 0; read--; } /* strip newline                    */
    strncpy (string, line, read);       /* copy line w/o linefeed to string */
    if (line) free (line);              /* free memory allocated by getline */
}
void dataEntry(Person *person) {
    printf("Insert name: ");
    getinput (person -> name);
    printf("Insert surname: ");
    getinput (person -> surname);
    printf("Insert date of birth [mm/dd/yyyy]: ");
    getinput (person -> date);
    printf("Insert phone number: ");
    getinput (person -> number);
}
void printPerson(Person *person) {
    printf("Name    : %sn", person -> name);
    printf("Surname : %sn", person -> surname);
    printf("D.O.B.  : %sn", person -> date);
    printf("Number  : %snn", person -> number);
}
void printAddressBook(Person *addbook) {
    for (int i = 0; i < DIM; i++) {
        printPerson(&addbook[i]);
    }
}

另外,不要忘记释放最后分配的内存。

输出:

$ ./bin/abook
DATA ENTRY
Person 0:
Insert name: dog
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 12/12/1912
Insert phone number: 555-1212
Person 1:
Insert name: cat
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 11/11/1911
Insert phone number: 555-1212
Person 2:
Insert name: rat
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 10/10/1910
Insert phone number: 555-1212
Person 3:
Insert name: flat
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 09/09/1909
Insert phone number: 555-1212
Person 4:
Insert name: fat
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 08/08/1908
Insert phone number: 555-1212
Person 5:
Insert name: bat
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 07/07/1907
Insert phone number: 555-1212
Person 6:
Insert name: hat
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 06/06/1906
Insert phone number: 555-1212
Person 7:
Insert name: Matt
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 05/05/1905
Insert phone number: 555-1212
Person 8:
Insert name: george
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 04/04/1904
Insert phone number: 555-1212
Person 9:
Insert name: harry
Insert surname: fish
Insert date of birth [mm/dd/yyyy]: 03/03/1903
Insert phone number: 555-1212

PRINTING ADDRESS BOOK...
Name: dog
Surname: fish
Date of birth: 12/12/1912
Number: 555-1212
Name: cat
Surname: fish
Date of birth: 11/11/1911
Number: 555-1212
Name: rat
Surname: fish
Date of birth: 10/10/1910
Number: 555-1212
Name: flat
Surname: fish
Date of birth: 09/09/1909
Number: 555-1212
Name: fat
Surname: fish
Date of birth: 08/08/1908
Number: 555-1212
Name: bat
Surname: fish
Date of birth: 07/07/1907
Number: 555-1212
Name: hat
Surname: fish
Date of birth: 06/06/1906
Number: 555-1212
Name: Matt
Surname: fish
Date of birth: 05/05/1905
Number: 555-1212
Name: george
Surname: fish
Date of birth: 04/04/1904
Number: 555-1212
Name: harry
Surname: fish
Date of birth: 03/03/1903
Number: 555-1212

最新更新