Creating a struct array in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GARiMTO
    New Member
    • Oct 2007
    • 2

    Creating a struct array in C

    Hi there
    I'm introducting myself in structures in C
    What i'm trying to do, is create a structure with 3 types like name, age, weight, etc, so i typedef a struct
    in order to save information for more than one "guy", i must create a struct array so that data could be saved.. and that's where my problem is!
    i'll send my code here and hope anyone helps me with solving this!
    the real question is, how to make the array, and in the end, print the data that the user entered (like "his name is john and he has 20 years old" / etc etc)

    [CODE=c]#include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>

    typedef struct {
    char name[20];
    int age;
    int weight;
    }ANIMAL;


    void main(){


    ANIMAL * horse;
    ANIMAL * horses_array[2]; // i believe this is wrong
    int count = 0;
    int insert;
    printf("How many Horses do you want to insert?\n");
    scanf("%d",&ins ert);

    horse = (ANIMAL*) malloc(insert*s izeof(ANIMAL));

    for (count=0; count<insert; count++){

    printf("Enter the name of the horse number %d: \n",count);
    scanf("%s",hors e[count].name);
    printf("Enter the age of horse number %d: \n",count);
    scanf("%d",&hor se[count].age);
    printf("Enter the weight of horse number %d: \n",count);
    scanf("%d",&hor se[count].weight);

    }
    printf("The Horse number %d has the name %s",count(???), horse[count].name); // print information about the 1++ horses inserted

    }[/CODE]




    modify as you please!
    thanks in advance, peace
    Last edited by Ganon11; Oct 2 '07, 01:25 AM. Reason: Please use the [CODE] tags provided.
  • shabinesh
    New Member
    • Jan 2007
    • 61

    #2
    there is nothing wrong ..you are correct

    Comment

    • GARiMTO
      New Member
      • Oct 2007
      • 2

      #3
      Originally posted by shabinesh
      there is nothing wrong ..you are correct
      but how do i creat an struct array?
      with int's i make like this:
      int a[10];

      but with struct ANIMAL, how do i make?
      ANIMAL horseArray[2] ? it gives me an error!
      help

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by GARiMTO
        but with struct ANIMAL, how do i make?
        ANIMAL horseArray[2] ? it gives me an error!
        No it doesn't. At least it won't after you hadd the semi-colon at the end of the statement.

        This is an array of 2 ANIMAL variables.

        However this is not the code in your original post:
        Originally posted by GARiMTO
        ANIMAL * horses_array[2]; // i believe this is wrong
        Nothing wrong here either. Except this is an array of two pointers to ANIMAL variables.

        Are you confusing a pointer to ANIMAL with the ANIMAL itself?

        Comment

        • Studlyami
          Recognized Expert Contributor
          • Sep 2007
          • 464

          #5
          First let me say that I'm not real familiar with malloc, but i know you are allocating memory for the size of the array. The problem i see is you specified the array size as 2 ( Animal Horse_Array[2];) Then allocate memory for a user specified the size then you access elements outside of the original declaration. i.e. that loop would access Horse_Array[8] or Horse_Array[10]. That just seem wrong to me, but it could be different styles.

          Heres another way of handle this while using heap memory.

          Code:
          ANIMAL * horse;
          	int count = 0;
          	int insert;
          	printf("How many Horses do you want to insert?\n");
          	scanf("%d",&insert);
          
              horse = new ANIMAL[insert];
          
          	for (count=0; count<insert; count++){
          
          	printf("Enter the name of the horse number %d: \n",count);
          	scanf("%s",horse[count].name);
          	printf("Enter the age of horse number %d: \n",count);
          	scanf("%d",&horse[count].age);
          	printf("Enter the weight of horse number %d: \n",count);
          	scanf("%d",&horse[count].weight);
          	
                  }
          
          delete [] horse; //must delete the array manually now in order to prevent memory leak

          Comment

          • Studlyami
            Recognized Expert Contributor
            • Sep 2007
            • 464

            #6
            Sorry about that post up above I didn't read that you were using C. I guess thats what i get for not being observant.

            Comment

            Working...