473,671 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dinamically allocate array of array of structures

Hello all
I would like to dinamically allocate an array of array of structures.
To explain this:

struct file{ char* fileName,int inode) myfiles[];
struct file{ char* fileName,int inode) mydirs[][];

but I would like to do it dinamically with linked list.
I am able to do it for myfiles, but not with mydirs.
Pseudo-code is ok.
If this is not so simple I will use static arrays.
Thanks a lot

Valerio

Aug 31 '06 #1
3 2675
valerio posted:
Hello all
I would like to dinamically allocate an array of array of structures.

I.e. a two-dimensional array of structures.

To explain this:

struct file{ char* fileName,int inode) myfiles[];
struct file{ char* fileName,int inode) mydirs[][];

Is this your first day learning C? Get the syntax right.

The amount of files in each directory will have to be constant:

#define FILES_PER_DIR 5
#define QUANTITY_DIRS 2

#include <stdlib.h>

typedef struct File {
char const *name;
} File;

File dir1[] = {{"a.exe"},{"b. exe"},{"c.exe"} ,{"d.exe"},{"e. exe"}};
File dir2[] = {{"a.bat"},{"b. bat"},{"c.bat"} ,{"d.bat"},{"e. bat"}};

File mydirs[QUANTITY_DIRS][FILES_PER_DIR];

int main(void)
{
unsigned i,j;
memcpy(mydirs,d ir1,sizeof dir1);
memcpy(mydirs+1 ,dir2,sizeof dir2);

for(i=0; i!=QUANTITY_DIR S; ++i)
{
for(j=0; j!=FILES_PER_DI R; ++j)
{
puts(mydirs[i][j].name);
}
}

return 0;
}

--

Frederick Gotham
Aug 31 '06 #2
"valerio" <va************ @gmail.comwrite s:
I would like to dinamically allocate an array of array of structures.
To explain this:

struct file{ char* fileName,int inode) myfiles[];
struct file{ char* fileName,int inode) mydirs[][];

but I would like to do it dinamically with linked list.
I am able to do it for myfiles, but not with mydirs.
Pseudo-code is ok.
If this is not so simple I will use static arrays.
Linked lists and arrays are very different things. In a linked list,
each element is allocated separately, and the only way to get to an
element is to traverse all the elements leading up to it. It's a
useful technique if you don't know in advance how many elements you'll
need, but I suspect you really want an array.

The comp.lang.c FAQ is at <http://c-faq.com/>. Question 6.16 is
"How can I dynamically allocate a multidimensiona l array?". (I highly
recommend reading the rest of the FAQ as well, though perhaps not all
at one sitting.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 31 '06 #3
valerio wrote:
>
Hello all
I would like to dinamically allocate an array of array of structures.
To explain this:

struct file{ char* fileName,int inode) myfiles[];
struct file{ char* fileName,int inode) mydirs[][];

but I would like to do it dinamically with linked list.
/* BEGIN list_of_lists.c output */

Person number 1
Initials: O.
Age : 32
Phone numbers:
163-4945
434-9890
785-6541
432-9672

Person number 2
Initials: B.L.
Age : 36
Phone numbers:
945-4349
890-7856
541-4329
672-3216

Person number 3
Initials: J.
Age : 24
Phone numbers:
349-8907

Person number 4
Initials: S.F.G.
Age : 24
Phone numbers:
907-8565
414-3296
723-2165
238-3036
381-0527

Person number 5
Initials: F.G.
Age : 20
Phone numbers:
565-4143
296-7232
165-2383

/* END list_of_lists.c output */

/* BEGIN list_of_lists.c */

#include <stdio.h>
#include <stdlib.h>

#define PEOPLE_MAX 5
#define NUMBERS_MAX 3
#define INITIALS 3
#define UPPER "ABCDEFGHIJKLMN OPQRSTUVWXYZ"
#define LU_RAND_SEED 123456789LU
#define LU_RAND(S) ((S) * 69069LU + 362437 & 0xffffffff)

struct list_node {
struct list_node *next;
void *data;
};

typedef struct list_node list_type;

struct person {
char name[2 * INITIALS + 1];
int age;
list_type *phone_numbers;
};

list_type *list_make(long unsigned count);
list_type *person_init(li st_type *node, long unsigned seed);
list_type *number_init(li st_type *node, long unsigned seed);
void make_name(char *name, size_t length, long unsigned seed);
void person_print(li st_type *node);
void number_print(li st_type *node);
void list_free(list_ type *node, void (*free_data)(vo id *));
void person_free(voi d *data);

int main(void)
{
list_type *head;
long unsigned seed = LU_RAND_SEED;

puts("/* BEGIN list_of_lists.c output */");
head = list_make(1 + seed % PEOPLE_MAX);
if (head == NULL) {
fputs("No list\n", stderr);
}
seed = LU_RAND(seed);
if (person_init(he ad, seed) != NULL) {
person_print(he ad);
}
list_free(head, person_free);
puts("\n/* END list_of_lists.c output */");
return 0;
}

list_type *list_make(long unsigned count)
{
list_type *node, *list;

list = count != 0 ? malloc(sizeof *list) : NULL;
if (list != NULL) {
node = list;
while (--count != 0) {
node -data = NULL;
node -next = malloc(sizeof *node -next);
if (node -next == NULL) {
list_free(list, free);
return NULL;
} else {
node = node -next;
}
}
node -data = NULL;
node -next = NULL;
}
return list;
}

list_type *person_init(li st_type *node, long unsigned seed)
{
list_type *head;
struct person person;

head = node;
while (node != NULL) {
node -data = malloc(sizeof person);
if (node -data == NULL) {
fputs("person_i nit malloc problem\n", stderr);
head = NULL;
break;
}
seed = LU_RAND(seed);
make_name(perso n.name, INITIALS, seed);
seed = LU_RAND(seed);
person.age = 20 + seed % 20;
seed = LU_RAND(seed);
person.phone_nu mbers = list_make(1 + seed % PEOPLE_MAX);
if (person.phone_n umbers == NULL) {
fputs("person.p hone_numbers == NULL\n", stderr);
head = NULL;
break;
}
seed = LU_RAND(seed);
if (number_init(pe rson.phone_numb ers, seed) == NULL) {
fputs("person_i nit(person.phon e_numbers, seed) == NULL\n",
stderr);
head = NULL;
break;
}
*(struct person *)node -data = person;
node = node -next;
}
return head;
}

list_type *number_init(li st_type *node, long unsigned seed)
{
list_type *head;
size_t count;
char *string;

head = node;
while (node != NULL) {
node -data = malloc(sizeof "xxx-xxxx");
if (node -data == NULL) {
fputs("number_i nit malloc problem\n", stderr);
head = NULL;
break;
}
string = node -data;
count = sizeof "xxx-xxxx" - 1;
while (count-- != sizeof "-xxxx" - 1) {
seed = LU_RAND(seed);
*string++ = (char)('0' + seed % 10);
}
*string++ = '-';
while (count-- != 0) {
seed = LU_RAND(seed);
*string++ = (char)('0' + seed % 10);
}
*string = '\0';
node = node -next;
}
return head;
}

void person_print(li st_type *node)
{
long unsigned count;
struct person *person;

for (count = 1; node != NULL; ++count) {
person = node -data;
printf(
"\nPerson number %lu\n"
"Initials: %s\n"
"Age : %d\nPhone numbers:\n",
count, person -name, person -age
);
number_print(pe rson -phone_numbers);
node = node -next;
}
}

void number_print(li st_type *node)
{
while (node != NULL) {
printf(" %s\n", (char *)node -data);
node = node -next;
}
}

void make_name(char *name, size_t length, long unsigned seed)
{
length -= seed % length ;
while (length-- != 0) {
seed = LU_RAND(seed);
*name++ = UPPER[seed % sizeof UPPER];
*name++ = '.';
}
*name = '\0';
}

void list_free(list_ type *node, void (*free_data)(vo id *))
{
list_type *next_node;

while (node != NULL) {
next_node = node -next;
free_data(node -data);
free(node);
node = next_node;
}
}

void person_free(voi d *data)
{
list_free(((str uct person *)data) -phone_numbers, free);
free(data);
}

/* END list_of_lists.c */
--
pete
Sep 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3265
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before opening to the file. I fail to write a function that allocates memory for a 2d array and returns it to main. I was trying to pass a pointer to the array back to main, but main cannot access the data. The simplified code (no opening of file, but...
4
7286
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving from an interface and only implementing some of it means something is wrong: either the interface specification is wrong e.g. not minimal or the derivation is wrong e.g. the type can't actually honour this contract.
104
16934
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from that array Could you show me a little example how to do this? Thanks.
32
2658
by: Mateo | last post by:
I have char *array and it is dinamically alocated.... When I pass it to other function, I need to determine size of this array.... Problem: sizeof operator does not work with dinamically alocated arrays!?!?! How can I determine size of dinamically alocated array.... ? Please advice... Thx!
2
3443
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You can not use the row 0, and the column 0.
12
3874
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
4
3253
by: Pawel_Iks | last post by:
Let's consider one dimensional array, which is defined like below: int N=200; int *tab=new int; // do something with tab delete tab; I'd like to know why can I do the same for two dimensional array. Please help me.
17
2313
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
5
3788
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);" I thought that it is very hard to memory map structure array. I need both read and write memory mapped file at both side of C# and C++.
0
8483
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8402
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8927
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8825
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
8605
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
8676
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
6237
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...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.