Two dimensional arrays "C question"

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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: Clarify An Old Question: define a user data containing array with dimensions decided in running
    ... will then know the dimension already. ... When you use an integer parameter to declare arrays using that parameter in their dimensions, the compiler knows the size of the arrays. ... There is a reason why the ALLOCATABLE attribute is required in order to allocate a variable. ...
    (comp.lang.fortran)
  • 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)