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");
    }

}

I'm trying to make a contacts program in C. I want the program to print the user's house address. I have the "HouseNumber" variable in the structure and the "street" variable in the structure, setting "answer3" as an int variable to display the "HouseNumber" and "answer4" as a char variable to display "street". Together, I was hoping they will print the user's address in a single string, but when I run the program and enter "yes" to display the contact information after entering the house number and street, the program crashes, and displays lldb with a bad thread error. It seems like everything is right, because my compiler says that there are no issues with the code, but it crashes. Can somebody please help me with this?

2
  • 2
    Your name has to be longer than 1 character. Set your "strings" to char *, or use a string library. Then inside each struct, you'll have to malloc size for your string and use the pointer in the struct to point to this allocated memory. Commented Jul 15, 2013 at 16:52
  • Your structure is wrong. It holds a char for Name and street instead of a string. You should either make it a character array (i.e. char Name[256]) or make it a char * pointer, and then malloc when it is assigned. Commented Jul 15, 2013 at 16:53

3 Answers 3

2

When you do scanf("%s", &answer);

You're writing the user's input into a char answer; The argument to scanf needs to have enough space to hold the entire string plus one null byte. Try something like:

char answer[256];

This assumes that input will never be more than 256 chars, but seems reasonable in a simple program like this. Note that gcc supports non-standard %a that will allocate a string for you if you pass in a char*.

When you try to store strings you need to ensure there's enough space for the data you're putting there, like in Dictionary only holding a char, or else you're going to overflow and stomp on some other memory you may need later.

Sign up to request clarification or add additional context in comments.

Comments

1

The mistake i have observed in your code is, you have created name,street and answer variables as char but tried to store strings in them by using %s resulting into crashing. In those places either you have to use char * or character array.

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

The modified code looks like this

      #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; 
    }

Comments

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

inside main:

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

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

Then you can copy in your Name/street etc.
Since Name is a char*, you won't use the address, but you'll just pass the pointer to scanf. This will fill that malloc'd memory with the string the user enters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.