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) - #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
5 4779
there is nothing wrong ..you are correct
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
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?
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. -
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
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
{
|
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>
|
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.
|
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
|
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;
| |
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;
|
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
{
|
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
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |