473,800 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a struct array in C

2 New Member
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 4779
shabinesh
61 New Member
there is nothing wrong ..you are correct
Oct 2 '07 #2
GARiMTO
2 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Contributor
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 Recognized Expert Contributor
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
3489
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 non-zero index is returned. Below is my attempt, which generates compiler errors: namespace Reference {
7
7857
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 isn't working. #include <stdio.h> #include <pthread.h>
5
6316
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 array of UDT.
9
3096
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 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Declare your object variables
1
1897
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 task_node { int task_id;
5
14546
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 Color cDay; private Color cNight; private Color cLuminous;
9
6535
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 structure is defined in the header(.h file) as shown below struct validationResult {
3
2286
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 these structs. A typical usage case will be as ff (note the code below is Pseudocode and WILL NOT compile) //example structs (I have left out the ctors/dtors etc for brevity sake) struct MyStructA
15
8450
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 a={int,float,char,int*..............................}, so that a should return me int and a should return me
0
10279
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.