[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Array af pointere til strukturer



Henrik Brix Andersen wrote:
On Thu, 2003-03-13 at 05:11, Søren H.Sørensen wrote:

typedef struct {
 ...
} FooBar;

/* Vi antager at der er et antal af FooBars på n elementer */

FooBar **get_foobars (void)
{
FooBar *P[n]; /* Vores resulterende pointer-array */
for( i=0 ; i<n ; i++ )
 P[i]=malloc( sizeof(FooBar) );
return( &P );
}


Problemet er, at jeg ikke kender n, men måske det kan gøres som
nedenstående?


FooBar **get_foobars (void)
{
  FooBar **foo;
  int n;

n = /* beregn n */

info = malloc (sizeof(FooBar) * n + 1);
Jeg tror, at du er ude på at gøre det sådan her:
foo = malloc(sizeof(*foo) * n + 1);

/* kan det gøres sådan? */
/* her fyldes der noget information i foo[0..(n-1)] */

Hvis du skal fylde noget i foo[i] her, skal du huske, at udføre foo[i] = malloc(sizeof(FooBar)) først.

Tilgang sker sådan: foo[i]->bar = ...

Så passer resten af koden her også fint.

foo[n] = NULL;


  return foo;	
}




 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *