NO

Author Topic: sizeof trouble  (Read 2963 times)

Alessio

  • Guest
sizeof trouble
« on: May 27, 2009, 11:21:55 AM »
Hi,

please see following code:

Code: [Select]
//
#include <stdio.h>

//
typedef struct
{
char *szName;
int iDummyValue;

} FOO;

//
static const FOO foo1[]=
{
{ "foo 1 - row 1", 1 },
};

//
static const FOO foo2[]=
{
{ "foo 2 - row 1", 1 },
{ "foo 2 - row 2", 1 },
{ "foo 2 - row 3", 1 },
};

//
static const FOO foo3[]=
{
{ "foo 3 - row 1", 1 },
{ "foo 3 - row 2", 1 },
};

//
static const FOO *foos[] = { foo1, foo2, foo3 };

//
int main(void)
{
int iFoos = sizeof(foos)/sizeof(foos[0]);
printf("%d foos founded.\n", iFoos);

for ( int i = 0; i < iFoos; i++ )
{
printf("foo %d has %d rows.\n", i+1, sizeof(foos[i])/(sizeof(foos[i][0])));
}

return 0;
}

How can I retrieve from foos pointer, number of rows for each elements (foo1, foo2, foo3) ?
I don't know how use sizeof operator in a decent way to get 1, 3 and 2 respectively.

Thanks.

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: sizeof trouble
« Reply #1 on: May 28, 2009, 07:19:48 AM »
I have look at your problem and found also not good solution. The easiest solution may be a second array like this:

Code: [Select]
static const int foo_row[] = { sizeof(foo1)/sizeof(foo1[0]), sizeof(foo2)/sizeof(foo2[0]), sizeof(foo3)/sizeof(foo3[0]) };

An other solution may be to add to each FOO-array an empty item and you write a function similar to strlen();
best regards
 Alex ;)

Seltsamuel

  • Guest
Re: sizeof trouble
« Reply #2 on: May 28, 2009, 05:36:34 PM »
Hi,

sizeof cannot help here.
The confusion arises because this is the one place that the use of an array is not converted into a pointer first. It is never possible, using sizeof, to find out how long an array a pointer points to is, you must have a genuine array name instead.

So i would advise a second table like AlexN suggested.

There is another tricky solution but there are many BUTs ^^
All Arrays must be allocated one after each other in memory and best #pragma 1
This Solution is NOT advised but works 100% ^^

#include <stdio.h>

//
#pragma(1)
typedef struct
{
   char *szName;
   int iDummyValue;

} FOO;

//
static const FOO foo1[]=
{
   { "foo 1 - row 1", 1 },
};

//
static const FOO foo2[]=
{
   { "foo 2 - row 1", 1 },
   { "foo 2 - row 2", 1 },
   { "foo 2 - row 3", 1 },
};

//

static const FOO foo3[]=
{
   { "foo 3 - row 1", 1 },
   { "foo 3 - row 2", 1 },
};

static const FOO endfoo[]=
{
   { NULL , 0 },
};

//
static const FOO *foos[] = { foo1, foo2, foo3, endfoo };

int main(void)
{
   int iFoos = sizeof(foos)/sizeof(foos[0])-1;
   printf("%d foos found.\n", iFoos);

   for ( int i = 0; i < iFoos; i++ )
   {
      printf("foo %d has %d rows.\n", i+1,( foos[i+1] - foos ) );
   }

   return 0;
}
« Last Edit: May 28, 2009, 05:40:37 PM by Seltsamuel »

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • Alex's Link Sammlung
Re: sizeof trouble
« Reply #3 on: May 28, 2009, 07:24:54 PM »
Hi,

So i would advise a second table like AlexN suggested.


my idea was something like this:

Code: [Select]
#include <stdio.h>

//
#pragma(1)
typedef struct
{
   char *szName;
   int iDummyValue;

} FOO;

//
static const FOO foo1[]=
{
   { "foo 1 - row 1", 1 },
   { NULL , 0 },
};

//
static const FOO foo2[]=
{
   { "foo 2 - row 1", 1 },
   { "foo 2 - row 2", 1 },
   { "foo 2 - row 3", 1 },
   { NULL , 0 },
};

//

static const FOO foo3[]=
{
   { "foo 3 - row 1", 1 },
   { "foo 3 - row 2", 1 },
   { NULL , 0 },
};


//
static const FOO *foos[] = { foo1, foo2, foo3 };

int foolen(const FOO *foo)
{
    int ret = 0;

    while (foo->szName != NULL)
    {
        foo++;
        ret++;
    }
    return ret;
}

int main(void)
{
   int iFoos = sizeof(foos)/sizeof(foos[0]);
   printf("%d foos found.\n", iFoos);

   for ( int i = 0; i < iFoos; i++ )
   {
      printf("foo %d has %d rows.\n", i, foolen(foos[i]) );
   }

   return 0;
}

I think this should work with each compiler.  ;)
best regards
 Alex ;)