|
James Rich wrote:
>
> Hey everyone,
>
> I need to make an array of pointers to struct that can be dynamically
> resized. Right now i have this:
>
> typedef struct _screenfields
> {
> unsigned int totalfields;
> field5250 fields[1782];
> } screenfields;
>
> screenfields *fields5250[10];
>
> I need to make the fields5250 array be dynamic (instead of only 10
> elements). I think I need to do:
>
> screenfields **fields5250;
>
> I don't know how to do the malloc() properly for that, though. Currently
> for my static array of pointers I do this:
>
> fields5250[0] = (screenfields *) malloc (sizeof (screenfields));
>
> Which works great. I think I should be able to keep the same statement to
> allocate each element using a dynamic array, but I don't know how to
> allocate the array itself. I think I don't have to allocate the array
> itself at all, since the array is just a pointer to the first element in
> the array. But it's been seg faulting, so I think I'm doing something
> wrong.
>
An array of pointers is an array of data just like an array of
integers. For a dynamic array of integers, you'd have to allocate the
array; same with the array of pointers.
fields5250 = (screenfields **) malloc (num * sizeof(screenfields *));
Me, I like to use typedef:
typedef screenfields *screenfieldsPtr;
screenfieldsPtr *fields5250;
fields5250 = (screenfieldsPtr *) malloc (num *
sizeof(screenfieldsPtr));
fields5250[0] = whatever
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.