Two dimensional arrays "C question"

Tech-Archive recommends: Speed Up your PC by fixing your registry



Hello,

My question has to do with two dimensional arrays. Please consider the
following code:

==================================
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char greet [] [8]=
{"Hello",
"Holla",
"Bonjour",
"Ciao"};

int main ()
{
int u;
u = strlen(greet[2]);
return 0;
}
===============================

In the above snippet, the compiler automatically calculates the number of
items for the first dimension subscript. So in reality it knows that there is
4 string of atleast 8 characters long... like this:

char greet [4] [8]=
{"Hello",
"Holla",
"Bonjour",
"Ciao"};
....

So we see that the compiler can automatically calculate the first dimension.
But consider the following code snippet and I don't understand why the
compiler is not able to calculate the first dimension like it did in the
above sample code:

===============================
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct abc
{
int zzz[2];
char a[][8];
}ABC;

typedef struct menu
{
struct abc *pColl;
}MENU;

MENU *F1(MENU *pC, ABC a[])
{
pC = malloc (sizeof (struct menu));
pC->pColl = a;
return pC;
}

int main ()
{
int u;
MENU *pMenu = NULL;
ABC x[] =
{
10, 20,
{"sss",
"ttthhh"}
};

pMenu = F1(pMenu, x);
u = strlen(pMenu->pColl[0].a[1]);

free(pMenu);
return 0;
}
==================================

I must be getting confused with the extra lines of code here. However I get
the following errors:

c:\dts_visual_c++\test1\test1\twodimenarray2.c(17) : error C2233: 'a' :
arrays of objects containing zero-size arrays are illegal

c:\dts_visual_c++\test1\test1\twodimenarray2.c(28) : error C2233: 'x' :
arrays of objects containing zero-size arrays are illegal

If I do this:

char a[2][8];

It compiles without errors though!

Why isn't the compiler able to calculate the first dimension for the latter
sample of code?

All help appreciated!

--
Best regards
Roberto
.



Relevant Pages

  • Re: Arrays and Functions (how to clean up code)
    ... int main{ ... If you change meqn and mx to #define macros, ... for variable length arrays. ... compiler diagnostic is required. ...
    (comp.lang.c)
  • Re: OK folks, corrected
    ... int function; ... since this tells the compiler that this pointer can be 1 or more ... since you can't have arrays of incomplete type. ...
    (comp.lang.c)
  • Re: Two dimensional arrays "C question"
    ... My question has to do with two dimensional arrays. ... int main ... the compiler automatically calculates the number of items for the first dimension subscript. ...
    (microsoft.public.vc.language)
  • Re: [C] functions and 2D arrays?
    ... >>to arrays of arrays of int, when you could be passing pointers ... >>to arrays of int. ... The standard doesn't require the compiler to *refuse* to compile ... And those are two different pointer types, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: TYPE declaration/statement in Fortran 77 code
    ... were functions and which were arrays. ... How many times have you seen someone forget to put in the DIMENSION ... statement and found the compiler thought it was a function? ... When you learn a new related rule which replaced an older one, ...
    (comp.lang.fortran)