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

Allocating Memory for a variable-sized array of Structs

Greetings:
I'm trying to get the ASCI C syntax correct here. I want to create memory space for a variable-size array of pointers to structs.

This is what I found in theScripts' archive:

> The following code:
>
> #include <stdio.h>
>
> // const int const_asize = 10;
> #define define_asize = 10;
>
> int array[define_asize] = {1,2,3,4,5,6,7,8,9,0};[/color]

You don't need the size. The compiler can count the initializers and
size the array for you :

int array[] = {1,2,3,4,5,6,7,8,9,0};

You can retreive the size by applying the definition of an array
(sequence of elements of the same size), hence :

size_t n = sizeof array / sizeof array[0];

or

size_t n = sizeof array / sizeof *array;
This is what I'm toying with:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef struct {
  5.     char* name;  
  6.     int   number;
  7. } SomeSeq;
  8.  
  9. int main () {
  10.     printf("Hello, World!\n");
  11.     const unsigned n = 5;
  12.     SomeSeq *myStructures[] = calloc(n,sizeof(SomeSeq));
  13.     // ...
  14.     free(myStructures);
  15.     // ...
  16.     return 0;
  17. }
  18.  
I'm getting the following compiler errors:
Expand|Select|Wrap|Line Numbers
  1. main.c:12: warning: incompatible implicit declaration of built-in function 'calloc'
  2. main.c:12: error: invalid initializer
  3.  
What's wrong with this syntax?
... I even tried to do a (SomeSeq *) cast of calloc, but doesn't correct the problems.

Regards,

Ric.
Aug 31 '07 #1
3 9256
RRick
463 Expert 256MB
Your problem is on line 12, where you have defined too many levels of indirections for your pointer (i.e. loose the [ ] and it should work).

With C arrays, each * and [ ] defines another dimension (or level of indirection). You have defined an array of pointers where each thing they point to is an array of SomeSeq. You really only want a single array of SomeSeq defined. That is what calloc is expecting.
Aug 31 '07 #2
Your problem is on line 12, where you have defined too many levels of indirections for your pointer (i.e. loose the [ ] and it should work).

With C arrays, each * and [ ] defines another dimension (or level of indirection). You have defined an array of pointers where each thing they point to is an array of SomeSeq. You really only want a single array of SomeSeq defined. That is what calloc is expecting.
Thanks for the clarity. So the following should be okay:
Expand|Select|Wrap|Line Numbers
  1. const unsigned n = 5;
  2. SomeSeq *myStructures = calloc(n,sizeof(SomeSeq));
  3.  
or...

Expand|Select|Wrap|Line Numbers
  1. const unsigned n = 5;
  2. SomeSeq *myStructures = malloc(sizeof(SomeSeq)*n);
  3.  
This would give me an array of pointers to 5 structures on the heap.
...I think.

Now...

To access each of the elements, should I do something like:
Expand|Select|Wrap|Line Numbers
  1. const unsigned n = 5;
  2. SomeSeq *myStructures = malloc(sizeof(SomeSeq)*n);
  3. myStuctures[0]->name='Ric';
  4. ...
  5. ...
  6. free(myStructures);
  7. ...
  8.  
I believe line #3 above is incorrect. What would be the correct syntax or accessing a Struct member via an array of struct pointers?

... my shared Wi-Fi signal is waning... hence in a rush.

Thanks for your help!!

Regards,
Ric.
Sep 1 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You may be making this too hard.

For an array of pointers, malloc() or calloc() will return the address of the array. That means you need the address of a pointer to receive the return from malloc() or calloc():

Expand|Select|Wrap|Line Numbers
  1. SomeSeq** ptr = (SomeSeq**) malloc(5 * sizeof(SomeSeq*));
  2.  
Sep 2 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: hall | last post by:
Hi. I have some problems with a class i've written that allocates memory dynamicaly. I want to put these objects into a std::vector, but it does not work. My class looks (simplified) like this: ...
10
by: Jakob Bieling | last post by:
Hi, Whenever allocating memory using operator new or operator new I feel like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all...
4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
7
by: boss_bhat | last post by:
Hi all , I am beginner to C programming. I have a defined astructure like the following, and i am using aliases for the different data types in the structure, typedef struct _NAME_INFO {...
3
by: Tod Birdsall | last post by:
Hi All, The organization I am working for has created a new corporate website that used Microsoft's Pet Shop website as their coding model, and dynamically served up content, but cached each...
19
by: allanallansson | last post by:
Hi i would like some guidelines for a experiment of mine. I want to experiment with the swap and ctrl-z in linux. And for this i want to create a c program that allocates almost all the free memory...
3
by: Kane | last post by:
When you create node 1 you allocate memory and link it Again when you create node 2 you would allocate memory for that node in a different section of the code. Is there more efficient way where I...
12
by: filia&sofia | last post by:
For example I would like to dynamically allocate memory for linked list (or complex struct etc.) that has not maximum number of elements. All that I can remember is that I have to have allocated...
6
by: arashmath | last post by:
Hi everyone, I have a problem with large dynamic memory allocating. this is my code (in briefness): #include <stdio.h> #include <mem.h> #include <assert.h> #define SML_BOUNDS_CHECK
13
by: charlie | last post by:
I came up with this idiom for cases where a function needs a variable amount of memory for it's temporary storage so as to avoid constantly mallocing. It makes me feel a little uncomfortable to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.