0

I'm trying to write a data structure with two elements, and then defining a variable of that type struct. However, after initializing the variable in the main function, I'm getting segmentation fault and I don't know why.

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

struct AnimalSizes {
    char stringName[50]; 
    double sizeLength; 
    } animalSizes[2]; 

int main()
{

    struct AnimalSizes *snakes; 
    strcpy(snakes[0].stringName,"Anaconda"); 
    snakes[0].sizeLength=3.7; 
    strcpy(snakes[1].stringName,"Python"); 
    snakes[1].sizeLength= 2.4; 
    printf("%c", *snakes[0].stringName); 
    printf("%lf", snakes[0].sizeLength); 
    printf("%c", *snakes[1].stringName); 
    printf("%lf", snakes[1].sizeLength);
    return 0;
}

3 Answers 3

2

You try to strcpy to destination where is no allocated memory. That is undefined behavior. You should first allocate enough memory to hold two AnimalSizes instances:

struct AnimalSizes *snakes;
snakes = malloc(2 * sizeof(struct AnimalSizes));

Also, here printf("%c", snakes[0].stringName); you are trying to output the first character of stringName. I assume, what you rather want to do is to output whole string with %s.

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

1 Comment

Hi, Thank you for your answer! I was wondering why we had to use dynamic memory allocation. I'm kind of lost in that topic and any help would be appreciated!
2

You've declared a pointer to a struct AnimalSizes, and you have declared an array struct AnimalSizes[2], but you have not made the pointer point to this array:

int main()
{

    struct AnimalSizes *snakes = &animalSizes[0]; 
...
}

Alternatively, you may choose to not declare a global variable, rather choosing to allocate memory in main:

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

struct AnimalSizes {
    char stringName[50]; 
    double sizeLength; 
    }; 

int main()
{

    struct AnimalSizes *snakes = (struct AnimalSizes*) malloc(2*sizeof(struct AnimalSizes)); 
    strcpy(snakes[0].stringName,"Anaconda"); 
    snakes[0].sizeLength=3.7; 
    strcpy(snakes[1].stringName,"Python"); 
    snakes[1].sizeLength= 2.4; 
    printf("%c", *snakes[0].stringName); 
    printf("%lf", snakes[0].sizeLength); 
    printf("%c", *snakes[1].stringName); 
    printf("%lf", snakes[1].sizeLength);
    free(snakes);
    return 0;
}

Comments

0

the following proposed code:

  1. eliminates any need for malloc() and free()
  2. performs the desired functionality
  3. separates the definition of the struct from any instance of the struct.
  4. inserts some spacing between the first letter of the snake name and the 'size' of the snake, for readability
  5. applies certain other changes to the code for 'human' readability

and now the proposed code:

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

struct AnimalSizes 
{
    char stringName[50]; 
    double sizeLength; 
};


int main( void )
{
    struct AnimalSizes snakes[2]; 

    strcpy(snakes[0].stringName,"Anaconda"); 
    snakes[0].sizeLength=3.7; 

    strcpy(snakes[1].stringName,"Python"); 
    snakes[1].sizeLength= 2.4; 

    printf("%c  ",  snakes[0].stringName[0]); 
    printf("%lf\n", snakes[0].sizeLength); 

    printf("%c  ",  snakes[1].stringName[0]); 
    printf("%lf\n", snakes[1].sizeLength);
    return 0;
}

a run of the proposed code outputs:

A  3.700000
P  2.400000

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.