Greetings all, I had another post on here about a Dynamic array of ints problem, and with your help, I managed to solve that problem. Now, I've another issue. I need to dyanically allocate an array of structs.
The struct is defined as: - struct song{
-
-
char name[50];
-
char title[50];
-
-
};
So, for work, they want me to sort through the songs, but due to memory constraints on the tiny machine they want it to run on, I have to use arrays instead of Linked Lists. :( I tried this method, the way it worked with ints: - int * list;
-
-
list = (int *)malloc(sizeof(int) * length);
And that would actually give me an array of ints. But if I try that with the song struct, it just gives me one.
Thanks in Advance.
5 2689
Please post code that fails, i.e. the attempt to allocate an array of structs.
RedSon 5,000
Recognized Expert Expert
Greetings all, I had another post on here about a Dynamic array of ints problem, and with your help, I managed to solve that problem. Now, I've another issue. I need to dyanically allocate an array of structs.
The struct is defined as: - struct song{
-
-
char name[50];
-
char title[50];
-
-
};
So, for work, they want me to sort through the songs, but due to memory constraints on the tiny machine they want it to run on, I have to use arrays instead of Linked Lists. :( I tried this method, the way it worked with ints: - int * list;
-
-
list = (int *)malloc(sizeof(int) * length);
And that would actually give me an array of ints. But if I try that with the song struct, it just gives me one.
Thanks in Advance.
First, using a linked list is more memory efficient then using an array. Every time you need to add to the array you will have to create an array that is one element larger and copy your existing array into the new array then delete your previous array. When this occurs you will then have a array of length N and an array of length N+1 which means that you are using virtually double the memory.
If you use a linked list, anytime you add something all you need to do is create the element then iterate to the last element in the list and reset the pointer to your new element.
The problem you may be experiencing is that the sizeof call may not work for your struct. I am not too clear on how it works exactly since I have never used it in this way. Consider trying to create your list using the sizeof (char[50]) to see if that will allocate properly, then moving on from there.
This works: -
-
struct song{
-
char name[50];
-
char title[50];
-
-
};
-
-
int length = 10;
-
song * list = (song *)malloc(sizeof(song) * length);
-
-
char buf[20];
-
for( int k=0; k < 10; k++ ) {
-
sprintf(buf,"song #%d", k );
-
strcpy( list[ k ].name, buf );
-
}
-
-
for( int k=0; k < 10; k++ ) {
-
printf( "%s\n", list[ k ].name );
-
}
-
Excellent, this worked! Thanks to ravenspoint, RedSon, and mac111!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Roman Hartmann |
last post by:
hello,
I do have a question regarding structs. I have a struct (profil) which
has a pointer to another struct (point). The struct profil stores the
coordinates of points.
The problem is that I...
|
by: Kieran Simkin |
last post by:
Hi,
I wonder if anyone can help me, I've been headscratching for a few hours
over this.
Basically, I've defined a struct called cache_object:
struct cache_object {
char hostname;
char ipaddr;...
|
by: mrhicks |
last post by:
Hello all,
I need some advice/help on a particular problem I am having. I have
a basic struct called "indv_rpt_rply" that holds information for a
particular device in our system which I will...
|
by: Paminu |
last post by:
Why make an array of pointers to structs, when it is possible to just make
an array of structs?
I have this struct:
struct test {
int a;
int b;
|
by: xinsir |
last post by:
dynamic array as a byref parameter by used in function and
have a Marshal error ,what is the matter?thanks
source like as this .
------------------------------------------------------
Declare...
| |
by: hobbes992 |
last post by:
Howdy folks,
I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
|
by: Cliff Martin |
last post by:
Hi,
I am reading a fairly large file a line at a time, doing some
processing, and filtering out bits of the line. I am storing the
interesting information in a struct and then printing it out....
|
by: johndale |
last post by:
hi,
I am pretty new to C#, but have some experience with other lang.(delphi,php,asp...). I wanted to create dynamic array of structs type, but it wont work. So I google it, and found that C#.NET...
|
by: superdad |
last post by:
Hello;
I have 2 questions:
1- I know you can allocate memory to an array dinamically in C and C++. But, can this be done if the array is part of a structure?
2- Can the size of a dynamic array...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
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...
|
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...
|
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: 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...
|
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...
|
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...
| |