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

struct Directory {
    char Name;
    long int Number;
    int HouseNumber;
    char street;
};

int main(void)
{
    struct Directory contact;
    struct Directory *answer1, *answer2, *answer3, *answer4;
    char answer;

    printf("Welcome to Telephone Directory.\n\nPlease enter the name of the contact.\n");
    scanf("%s", &contact.Name);

    printf("Please enter the number of the contact.\n");
    scanf("%ld", &contact.Number);

    printf("Please enter the address of the contact.\n");
    scanf("%d %s", &contact.HouseNumber, &contact.street);

    answer1 = &contact;
    answer2 = &contact;
    answer3 = &contact;
    answer4 = &contact;

    printf("Would you like to obtain information on your contact? Enter 'yes' or 'no'.\n");
    scanf("%s", &answer);


    if (strcmp(&answer, "yes")==0) {
        printf("%s\n", &answer1->Name);
        printf("%ld\n", answer2->Number);
        printf("%d", answer3->HouseNumber);
        printf("%s", &answer4->street);
    }

    if (strcmp(&answer, "no")==0) {
        printf("Thank you for using Telephone Directory.\n");
    }

}

我正在尝试在 C 中创建一个联系人程序。我希望该程序打印用户的家庭地址。我在结构中有“HouseNumber”变量,在结构中有“street”变量,将“answer3”设置为 int 变量以显示“HouseNumber”,将“answer4”设置为 char 变量以显示“street”。一起,我希望他们将用户的地址打印在一个字符串中,但是当我运行程序并输入“是”以在输入门牌号和街道后显示联系信息时,程序崩溃,并显示 lldb 错误线程错误。似乎一切都是正确的,因为我的编译器说代码没有问题,但它崩溃了。有人可以帮我吗?

4

3 回答 3

2

当你这样做scanf("%s", &answer);

您正在将用户的输入写入char answer;scanf 的参数需要有足够的空间来容纳整个字符串加上一个空字节。尝试类似:

char answer[256];

这假设输入永远不会超过 256 个字符,但在像这样的简单程序中似乎是合理的。请注意,gcc 支持非标准%a,如果您传入一个char*.

当您尝试存储字符串时,您需要确保有足够的空间来存放您放入的数据,例如在 Dictionary 中仅保存一个字符,否则您将溢出并踩到您稍后可能需要的其他内存。

于 2013-07-15T16:55:55.430 回答
1

我在您的代码中观察到的错误是,您已将名称、街道和答案变量创建为 char,但尝试使用%s将字符串存储在其中导致崩溃。在这些地方,您必须使用 char * 或字符数组。

    char *name //(or) char name[15]

修改后的代码如下

      #include <stdio.h> 
      struct Directory { 
      char Name[20]; 
      long int Number; 
      int HouseNumber; 
      char street[20]; 
      }; 


    int main(int argc, char *argv[]) 
    { 
        struct Directory contact; 

        struct Directory *answer1, *answer2, *answer3, *answer4; 

        char answer[4]; 

        printf("Welcome to Telephone Directory.\n\nPlease enter the name of the                                          contact.\n"); 

        scanf("%s", contact.Name); 

        printf("Please enter the number of the contact.\n"); 

        scanf("%ld", &contact.Number); 

        printf("Please enter the address and street of the contact.\n"); 

       scanf("%d %s", &contact.HouseNumber, contact.street); 

       answer1 = &contact; 

      answer2 = &contact; 

      answer3 = &contact; 

      answer4 = &contact; 

      printf("Enter yes r no"); 

      scanf("%s",answer); 

      if (strcmp(answer,"yes")==0) { 

        printf("%s\n", answer1->Name); 

        printf("%ld\n", answer2->Number); 

        printf("%d\n", answer3->HouseNumber); 

        printf("%s", answer4->street); 

     } 


    else { 

        printf("Thank you for using Telephone Directory.\n"); 
    } 

   return 0; 
    }
于 2013-07-15T18:12:12.610 回答
0
struct Directory {
    char * Name;
    long int Number;
    int HouseNumber;
    char * street;
};

内部主要:

struct Directory Contact;
Contact.Name = malloc(sizeof(char * sizeOfName));

 scanf("%s", contact.Name);

然后你可以复制你的名字/街道等。
由于名字是一个字符*,你不会使用地址,但你只需将指针传递给 scanf。这将用用户输入的字符串填充 malloc 的内存。

于 2013-07-15T16:54:10.563 回答