473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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)

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h> 
  2. #include <stdlib.h> 
  3. #include <stdio.h>
  4.  
  5. typedef struct {
  6.     char name[20];
  7.     int age;
  8.     int weight;
  9. }ANIMAL;
  10.  
  11.  
  12. void main(){
  13.  
  14.  
  15.     ANIMAL * horse;
  16.     ANIMAL * horses_array[2];    // i believe this is wrong
  17.     int count = 0;
  18.     int insert;
  19.     printf("How many Horses do you want to insert?\n");
  20.     scanf("%d",&insert);
  21.  
  22.     horse = (ANIMAL*) malloc(insert*sizeof(ANIMAL));
  23.  
  24.     for (count=0; count<insert; count++){
  25.  
  26.     printf("Enter the name of the horse number %d: \n",count);
  27.     scanf("%s",horse[count].name);
  28.     printf("Enter the age of horse number %d: \n",count);
  29.     scanf("%d",&horse[count].age);
  30.     printf("Enter the weight of horse number %d: \n",count);
  31.     scanf("%d",&horse[count].weight);
  32.  
  33.     }
  34.     printf("The Horse number %d has the name %s",count(???),horse[count].name); // print information about the 1++ horses inserted
  35.  
  36. }



modify as you please!
thanks in advance, peace
Oct 2 '07 #1
5 4758
there is nothing wrong ..you are correct
Oct 2 '07 #2
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
Oct 2 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
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:
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?
Oct 2 '07 #4
Studlyami
464 Expert 256MB
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.

Expand|Select|Wrap|Line Numbers
  1. ANIMAL * horse;
  2.     int count = 0;
  3.     int insert;
  4.     printf("How many Horses do you want to insert?\n");
  5.     scanf("%d",&insert);
  6.  
  7.     horse = new ANIMAL[insert];
  8.  
  9.     for (count=0; count<insert; count++){
  10.  
  11.     printf("Enter the name of the horse number %d: \n",count);
  12.     scanf("%s",horse[count].name);
  13.     printf("Enter the age of horse number %d: \n",count);
  14.     scanf("%d",&horse[count].age);
  15.     printf("Enter the weight of horse number %d: \n",count);
  16.     scanf("%d",&horse[count].weight);
  17.  
  18.         }
  19.  
  20. delete [] horse; //must delete the array manually now in order to prevent memory leak
  21.  
Oct 2 '07 #5
Studlyami
464 Expert 256MB
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.
Oct 2 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
7
by: mike | last post by:
Hi, I am having difficulty in creating a thread using pthread_create. It seems that pthread_create does not execute 'program', and returns -1; I have checked the API but I am not sure why this...
5
by: Cybertof | last post by:
Hello, Is it possible to convert a VB6 Array of Struct to a C# Array Of Struct ? The test context is a C# application calling a VB6 ActiveX DLL Function using UDT (User Defined Type) and...
9
by: Patrick.O.Ige | last post by:
I have a code below and its a PIE & BAR CHART. The values now are all static but I want to be able to pull the values from a database. Can you guys give me some ideas to do this? Thanks ...
1
by: osamutoglu | last post by:
Platform is VS.NET 2003 and i am trying to design an application with Forms What is wrong here i didnt get it.. is there something i am missing? .. .. ../** STRUCTURE PROTOTYPE **/ struct...
5
by: Stick | last post by:
Hi, I normally program in C++, and I'm trying to write this little tool in C#, but I quickly realized that I can't use pointers, so instead I need to create an array of 3 Color 's private...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.