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)
Expand|Select|Wrap|Line Numbers
- #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",&insert);
- horse = (ANIMAL*) malloc(insert*sizeof(ANIMAL));
- 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);
- }
- printf("The Horse number %d has the name %s",count(???),horse[count].name); // print information about the 1++ horses inserted
- }
modify as you please!
thanks in advance, peace