[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



On 13 Mar 2003 09:07:06 +0100
Henrik Brix Andersen <sslug@sslug> wrote:
> 
> 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);
>   /* kan det gøres sådan? */
>  
>   /* her fyldes der noget information i foo[0..(n-1)] */
> 
>   foo[n] = NULL; 
> 
>   return foo;	
> }

Det du gør her er at retunerer en adresse til en lokal variabel. Det
vil sige at du retunerer en pointer til en variabel som befinder sig på
stakken. Variabler på stakken bliver slettet når funktionen retunerer!

Det kan godt være det virker i en simpel test som ikke bruger særlig
meget. Men det går rigtig galt når en anden funktion overskriver selv
samme adresse med nogle andre værdier !!!

Du skal retunerer adressen på hvad 'foo' peger på og ikke
adressen på 'foo'. Altså sådan her:

   return &(*foo);


Ellers bruger mange i 'C' denne måde: 

   int MakeArray(int num, foobar **pa) {
      int ret = -1;               /* 0 == ok, -1 == fejl. */
      foobar **tmp = NULL;
      ...
      pa = *tmp;
      return ret;
   }
      

(John =;-)
--
Husk backup af dit netværk !!!
ALBackup - http://www.adilock.dk/~john/albackup/


 
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] *