[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] Initialiserings problem



Egon Andersen, Talura wrote:
> 
> Hej,
> 
> Jeg har nu en stund (eller to) bøvlet med initialisering af nogle
> struktur variable.
> Simplificeret er problemet kogt ned til et lille program som følger:
> ---
> typedef struct{double f;} beta_t;
> 
> struct Alpha
>    {
>    beta_t * a;
>    beta_t ** aa;
>    };
> const struct Alpha gamma = {.a = (beta_t[1]){{.f = 5.0}}};
> 
> /* Følgende linie er forkert, men hvordan skal den skrives ?? */
> const struct Alpha delta = {.aa = (beta_t**)((beta_t[1][1]){{{.f =
> 8.7}}})};
> 
> main()
>    {
>    printf("Yep %f\n", *gamma.a);
>    printf("Yep %p\n", gamma.aa);
>    printf("Yep %p\n", delta.a);
>    printf("Nope %f\n", **delta.aa);
>    return 0;
>    }
> ---
> 
> Problemet er at initialisere delta.aa, således at **delta.aa er 8.7
> Jeg ønsker at undgå hjælpevariable, da det hele bliver en del af en
> auto-genereret header-fil. Hjælpevariable vil potentielt give
> navne-sammenfald.
> 
> Er der nogen der har et godt bud?
> 
> /Egon
Okay, så kommer den da bare her!
(Det kunne jo være at der er et par stykker som kunne synes det kunne
være interessant.)

---

typedef struct{void * type;} setof_t;

typedef struct{double f;} beta_t;

struct Alpha
   {
   beta_t * a;
   beta_t ** aa;
   };
const struct Alpha gamma = {.a = (beta_t[]){{.f = 5.0}}};

const struct Alpha delta = {.aa = (beta_t **)(setof_t[]){
                                   (void *)&(beta_t[]){{.f = 8.7},{.f =
7.3}},
                                   (void *)&(beta_t[]){{.f = 10.7},{.f =
7.6}}
                                    }};
 
main()
   {
   printf("Yep %f\n", *gamma.a);
   printf("Yep %p\n", gamma.aa);
   printf("Yep %p\n", delta.a);
   printf("Yahoo!          %f\n", delta.aa[0][0]);
   printf("Yepper          %f\n", delta.aa[0][1]);
   printf("Yabba dabba doo %f\n", delta.aa[1][0]);
   printf("Doh!            %f\n", delta.aa[1][1]);
   return 0;
   }

---
Jeg har altså indført een 'generel' hjælpe-type kaldet setof_t, hvilket
også er helt okay - det er jo ikke en hjælpe-variabel!

/Egon


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