0

I created a program that uses arrays and array pointers to store a user's animal name, category of animal, and age. But when I run it, there is a segmentation fault after I type the name age and category. I was wondering if someone could guide me in how to fix it, I am very new to C programming.

This is the task I am supposed to complete:

Write a program that defines an animal data type, with an animal name, age, and category (cat, dog, etc.), as well as an animal array type that stores an array of animal pointers.  Your program will prompt the user to enter the data for as many animals as they wish.  It will initialize a dynamically allocated animal structure for each animal, and it will store each animal in an instance of the animal array structure.  Your program will then print all the animal information to the screen.  You will upload your C program as one file.

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

typedef struct {
char *name;
char *category;
int age;
} AnimalType;

typedef struct {
AnimalType basicInfo;
AnimalType *arr[];
} AnimalArrayType;

void initAnimal(char *n, char *c, int a, AnimalArrayType *ar);
void printAnimal(const AnimalArrayType *stuPtr);

int main() {    

  AnimalArrayType *array;
  int numAn;
  char name[20];
  char category[20];
  int  age;

  printf("Please enter the number of animals you want to input: ");
  scanf("%d", &numAn);

  array = calloc(numAn, sizeof(AnimalArrayType));

  for (int i=0; i<numAn; ++i) {

    printf("Enter animal name: ");
    scanf("%s", name);
    printf("Enter their category: ");
    scanf("%s", category);
    printf("Enter age: ");
    scanf("%d", &age);


initAnimal(name, category, age, array + I);

  }

  printf("\n LIST:\n");
  for (int i=0; i<numAn; ++i) {
    printAnimal(array + I);
  }

  return 0;
}


void initAnimal(char *n, char *c, int a, AnimalArrayType *ar)
{
  strcpy(ar->basicInfo.name, n);
  strcpy(ar->basicInfo.category, c);
  ar->basicInfo.age = a;
}


void printAnimal(const AnimalArrayType *stuPtr)
{
      printf("Animal:  %s, Category: %s age %d\n",
      stuPtr->basicInfo.name, stuPtr->basicInfo.category,
      stuPtr->basicInfo.age);
}
8
  • 4
    Please don't tag c questions with c++, they're different languages. Commented Oct 14, 2020 at 23:31
  • 2
    The first thing the program prompts you for is a number, so why are you typing in a name instead? You are ignoring the return value of scanf() to know if it successfully read a value or not. You can't read the input value Bella into the numAn variable using scanf("%d") Commented Oct 14, 2020 at 23:33
  • 1
    Single step through your program using a debugger. It's an essential skill for a programmer to learn. If you can't run it under a debugger, use printf statements strategically placed in you code to help you figure out where the execution differs from what you expect. Commented Oct 14, 2020 at 23:36
  • @RemyLebeau I just edited my question, silly mistake of mine sorry. Commented Oct 14, 2020 at 23:36
  • Please don't post text as images or links to images- reasoning. Post it as text directly into the question. Commented Oct 14, 2020 at 23:37

1 Answer 1

2

The char *name and char *category fields in the AnimalType struct are being initialized to NULL (from the call to calloc()), but you don't allocate any char[] memory for them to point at afterwards, so you end up crashing in initAnimal() when it tries to copy data into those fields using strcpy().

You need to either:

  • change those fields into char[] arrays instead of char* pointers:
typedef struct {
    char name[20];
    char category[20];
    int age;
} AnimalType;
  • allocate memory for them to point at, such as from strdup():
typedef struct {
    char *name;
    char *category;
    int age;
} AnimalType;

...

void initAnimal(char *n, char *c, int a, AnimalArrayType *ar)
{
    ar->basicInfo.name = strdup(n);
    ar->basicInfo.category = strdup(c);
    ar->basicInfo.age = a;
}

Don't forget to free() anything you dynamically allocate when you are done using it.

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

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.