473,320 Members | 2,117 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,320 software developers and data experts.

Dynamically malloc'd array of structs

What would be the correct syntax for setting up
a dynamic array of structs?

Suppose you have a struct declared:

struct relation {
FILE * binFile;
unsigned int numAttrs;
struct attrList * relAttrs; /* definition shown at end of post */
};
typedef struct relation relHeader;

The array only has to be sized once. The size is read from a text
file.

Currently I have something like this:

#include <stdio.h>
int main(void){

unsigned int relCount = 2;
relHeader Headers[relCount-1];

return 0;
}

I would prefer to be able to define Headers as:
relHeader * Headers;

And then malloc space for whatever size I need.
When I try:

Headers = malloc(sizeof(*Headers) * (relCount-1))

I get a warning and a syntax error as follows:

p3.c:26: warning: assignment makes pointer from integer without a cast
p3.c:26: syntax error before "Headers"

What I have will work well enough for my purposes. If it's something
simple I'm missing I'd like to fix it.

Regards,
Grant
struct attribute {
struct attribute * next;
unsigned int attrLen;
char type;
char * attrName;
};
typedef struct attribute attrList;

Nov 14 '05 #1
5 2040
Grant Austin <ga*****@foo.foo.bar.net> wrote in
news:pa****************************@foo.foo.bar.ne t:
I would prefer to be able to define Headers as:
relHeader * Headers;

And then malloc space for whatever size I need.
When I try:

Headers = malloc(sizeof(*Headers) * (relCount-1))

I get a warning and a syntax error as follows:

p3.c:26: warning: assignment makes pointer from integer without a cast
p3.c:26: syntax error before "Headers"


Because there is no prototype for malloc() and functions without
prototypes default to returning an 'int' type. Since Headers is a pointer
this warning is very helpful. This is *precisely* why we don't cast
malloc's return in C. Simply #include <stdlib.h> and the warning will go
away along with the bug you would have had regarding this.
--
- Mark ->
--
Nov 14 '05 #2
On Fri, 05 Mar 2004 16:15:14 GMT, Grant Austin <ga*****@foo.foo.bar.net>
wrote:
What would be the correct syntax for setting up
a dynamic array of structs?
You're quite close.

Suppose you have a struct declared:

struct relation {
FILE * binFile;
unsigned int numAttrs;
struct attrList * relAttrs; /* definition shown at end of post */
Because attrList is already a typedef for a struct (well, it will be), this
isn't quite right. See my re-organized code below.
};
typedef struct relation relHeader;

The array only has to be sized once. The size is read from a text
file.

Currently I have something like this:

#include <stdio.h>
int main(void){

unsigned int relCount = 2;
Does "2" really mean 2 structs you'll be reading? If so, why are you
subtracting 1 below? Are you possibly confusing "size" with subscripting
expressions?
relHeader Headers[relCount-1];

return 0;
}

I would prefer to be able to define Headers as:
relHeader * Headers;
No prob.

And then malloc space for whatever size I need.
When I try:

Headers = malloc(sizeof(*Headers) * (relCount-1))
If you're malloc-ing relHeader objects, you just need to specify the size
of a relHeader object (see code below). And again, I suspect you don't want
to be subtracting that 1.

I get a warning and a syntax error as follows:

p3.c:26: warning: assignment makes pointer from integer without a cast
p3.c:26: syntax error before "Headers" Looks like you didn't include a required header file. See my code below.
What I have will work well enough for my purposes. If it's something
simple I'm missing I'd like to fix it.

Regards,
Grant
struct attribute {
struct attribute * next;
unsigned int attrLen;
char type;
char * attrName;
};
typedef struct attribute attrList;


Here's the rolled-up version:

#include <stdio.h>
#include <stdlib.h> /* for malloc */

struct attribute {
struct attribute * next;
unsigned int attrLen;
char type;
char * attrName;
};

typedef struct attribute attrList;

struct relation {
FILE * binFile;
unsigned int numAttrs;
/* struct attrList * relAttrs; /* definition shown at end of post */
attrList * relAttrs; /* note: no 'struct' */
};
typedef struct relation relHeader;

int main(void){

unsigned int relCount = 2;

relHeader * Headers;

/* Headers = malloc(sizeof(*Headers) * (relCount-1)) */
Headers = malloc(sizeof(relHeader) * relCount);
return 0;
}

HTH,
-leor
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3

Thanks,

Lesson learned.

-Grant
Nov 14 '05 #4
On Fri, 05 Mar 2004 16:38:37 GMT, Leor Zolman <le**@bdsoft.com> wrote:

Headers = malloc(sizeof(*Headers) * (relCount-1))


If you're malloc-ing relHeader objects, you just need to specify the size
of a relHeader object (see code below). And again, I suspect you don't want
to be subtracting that 1.


Sorry, I went brain-dead there. For some reason the * before Headers didn't
register to me. I'd usually put a space before that *, anyway...
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #5
Sorry, I went brain-dead there.


No worries, I am brain-dead most of the time.

You're right about the -1. I was thinking subscripts.

-g
Nov 14 '05 #6

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

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
7
by: Fabian Wauthier | last post by:
Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: ...
10
by: junky_fellow | last post by:
What is the correct way of dynamically allocating a 2d array ? I am doing it the following way. Is this correct ? #include <stdlib.h> int main(void) { int (*arr)(3); arr =...
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
7
by: Serpent | last post by:
The C-FAQ describes some techniques here: http://c-faq.com/aryptr/dynmuldimary.html I was using something slightly different from the C-FAQ and I was wondering if it was legal. Say I want a...
2
by: hal | last post by:
Hi, I'm trying to make an array of pointers to 'TwoCounts' structs, where the size of the array is arraySize. Right now I'm just mallocing enough space for all the pointers to the structs, and...
8
by: kiser89 | last post by:
I'm having a problem with my array of structs and segmentation faults. I have this struct that represents one line of a source file: struct threeTokens { int lineNumber; char* cmd; char* param;...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.