declaring a structure

J

Jason A.

Hello,

I am trying to declare 2 structures, and another one other that
contains the first two, so that I can iterate more easily
thru all the members.. Unfortunatly the compiler complains that
structure X has no member named: `table'

tabstruct.c:34: structure has no member named `table'
tabstruct.c:35: structure has no member named `table'

I guess I haven't understood something about declaring structures.
What is my mistake ? Is it because the compiler doesn't now what
size the original structure is ??

#include <stdio.h>

#define WHITE 0
#define BLUE 1

struct white {
char name[12];
int red;
int green;
int blue;
} whitetab[] = {
{ "kk", 1, 2, 3 },
{ "kj", 4, 5, 6 }
};

struct blue {
char name[12];
int red;
int green;
int blue;
} bluetab[] = {
{ "tt", 1, 2, 3 },
{ "tx", 4, 5, 6 },
{ "tf", 8, 11, 4 }
};

struct tables_ {
struct table;
int count;
} kolor[2];

int main(void) {

kolor[WHITE].table = whitetab;
kolor[BLUE].table = bluetab;

return 0;
}
 
M

Michael Mair

Jason said:
Hello,

I am trying to declare 2 structures, and another one other that
contains the first two, so that I can iterate more easily
thru all the members.. Unfortunatly the compiler complains that
structure X has no member named: `table'

tabstruct.c:34: structure has no member named `table'
tabstruct.c:35: structure has no member named `table'

I guess I haven't understood something about declaring structures.
What is my mistake ? Is it because the compiler doesn't now what
size the original structure is ??

Nope. You declare a new structure type by
struct tag { /* members */ };
and a variable of that type by
struct tag variable;
or, in one go, by
struct tag { /* members */ } variable;
If you want to declare all variables of that type at once,
you do not need the tag:
struct { /* members */ } var1, var2, arr[4];
If you want to use typedef, you also do not need the tag:
typedef struct tag {....} typename;
and
typedef struct {....} typename;
both allow you to declare your variables by
typename variable;
#include <stdio.h>

#define WHITE 0
#define BLUE 1

struct white {
char name[12];
int red;
int green;
int blue;
} whitetab[] = {
{ "kk", 1, 2, 3 },
{ "kj", 4, 5, 6 }
};

struct blue {
char name[12];
int red;
int green;
int blue;
} bluetab[] = {
{ "tt", 1, 2, 3 },
{ "tx", 4, 5, 6 },
{ "tf", 8, 11, 4 }
};

Both structures have the same members and do the same.

Better:
struct colour {
char name[12];
int red;
int green;
int blue;
};

struct colour whitetab[] = { /* your initialiser */ };
struct colour bluetab[] = { /* your initialiser */ };
struct tables_ {
struct table;
int count;
} kolor[2];

Well here you say: "struct tag;" i.e. you do say that the kind of
structure you want to have it is "struct table" but you leave out
the variable. This make as much sense as writing "int;"

With the above, you probably do not want to have a single colour
but a colour table (of variable size), so it will probably be

struct tables_ {
struct colour *table;
int tablelen; /* count is IMO not descriptive enough */
} kolor[2];
int main(void) {

kolor[WHITE].table = whitetab;
kolor[BLUE].table = bluetab;

This becomes
kolor[WHITE].table = whitetab;
kolor[BLUE].table = bluetab;
Oh, it is the same. Why? If you use only the name of an array,
it is in most contexts interpreted as address of the first
array element.

You missed:
kolor[WHITE].tablelen = (sizeof whitetab)/(sizeof whitetab[0]);
...
return 0;
}

Something else: Consider making an enumeration containing your
colours as this is better for maintainability:

enum coltables { WHITE, BLUE, RED, ....., NUM_COLTABLES };

.....

struct tables_ kolor[NUM_COLTABLES];


If you use C99, then you may also want to have a look at "designated
initializers" (just look at groups.google for messages by me with
"designated" and "enum" in c.l.c).


Cheers
Michael
 
S

sipetar

Jason said:
tabstruct.c:34: structure has no member named `table'
tabstruct.c:35: structure has no member named `table'


struct tables_ {
struct table;
int count;
} kolor[2];



you must declare struct table just like you did blue, white..


struct tablebw{

struct white *w;
struct blue *b;
}
then:

struct tables_ {
struct tablebw table;
int count;
} kolor[2];

and then later write:

kolor[WHITE].table.w = whitetab; // this gives you adress of
whitetab[0] ->
//{ "kk", 1, 2, 3} or you can write

// &whitetab[0] or &whitetab[1] if you want to set
pointer
// to point on { "kj", 4, 5, 6 }
kolor[BLUE].table.b = bluetab; // this gives you adress of bluetab[0]

but of course I think that this is not very good solution becouse you
have two
pointers kolor[WHITE].table.b and kolor[BLUE].table.w that are not set
so dont try to dereference them.


or you can use something like this:

#include <stdio.h>

#define WHITE 0
#define BLUE 1


typedef struct table {
char name[12];
int red;
int green;
int blue;
}table;


typedef struct tables_ {
table t;
int count;

}tables_;

int main(void) {

tables_ kolor[1];

table whitetab[]={{ "kk", 1, 2, 3 },{ "kj", 4, 5, 6 }};
table bluetab[]={{ "tt", 1, 2, 3 },{ "tx", 4, 5, 6 },
{ "tf", 8, 11, 4 }};


kolor[WHITE].t =whitetab[0];

kolor[BLUE].t = bluetab[0];

return 0;

}

hope I help you
Sinisa
 
A

Al Bowers

Jason said:
Hello,

I am trying to declare 2 structures, and another one other that
contains the first two, so that I can iterate more easily
thru all the members.. Unfortunatly the compiler complains that
structure X has no member named: `table'

tabstruct.c:34: structure has no member named `table'
tabstruct.c:35: structure has no member named `table'

I guess I haven't understood something about declaring structures.
What is my mistake ? Is it because the compiler doesn't now what
size the original structure is ??

Yes, your data structures are incorrect.
There are several ways you can do this.
The way you have invisioned would require you to
declare a struct to hold the members name, red, green
and blue. Ex:

struct table
{
char name[12];
int red;
int green;
int blue;
};

then declare a structure

struct tables_
{
struct table *table;
size_t count;
};

See the declarations and use in the code below.
#include <stdio.h>

#define WHITE 0
#define BLUE 1

struct white {
char name[12];
int red;
int green;
int blue;
} whitetab[] = {
{ "kk", 1, 2, 3 },
{ "kj", 4, 5, 6 }
};

struct blue {
char name[12];
int red;
int green;
int blue;
} bluetab[] = {
{ "tt", 1, 2, 3 },
{ "tx", 4, 5, 6 },
{ "tf", 8, 11, 4 }
};

struct tables_ {
struct table;
int count;
} kolor[2];

int main(void) {

kolor[WHITE].table = whitetab;
kolor[BLUE].table = bluetab;

return 0;
}

#include <stdio.h>

#define WHITE 0
#define BLUE 1

struct table
{
char name[12];
int red;
int green;
int blue;
} ;

struct tables_
{
struct table *table;
size_t count;
} ;

int main(void)
{
struct table bluetab[] = {{ "tt", 1, 2, 3 },
{ "tx", 4, 5, 6 },
{ "tf", 8, 11, 4 }};
struct table whitetab[] = {{ "kk", 1, 2, 3 },
{ "kj", 4, 5, 6 }};
struct tables_ kolor[2];
size_t i,j;

kolor[WHITE].table = whitetab;
kolor[WHITE].count = sizeof whitetab/sizeof *whitetab;
kolor[BLUE].table = bluetab;
kolor[BLUE].count = sizeof bluetab/sizeof *bluetab;
/*test*/
for(i = 0; i < 2;i++)
{
printf("\n\n%s\n",i?"BLUE:":"WHITE:");
for(j = 0; j < kolor.count; j++)
printf("name = %s\n"
"red = %d\n"
"green = %d\n"
"blue = %d\n\n",kolor.table[j].name,
kolor.table[j].red, kolor.table[j].green,
kolor.table[j].red);
}
return 0;
}
 
A

Al Bowers

#include <stdio.h>

#define WHITE 0
#define BLUE 1
.............SNIP...........

int main(void) {

tables_ kolor[1];

.............SNIP............


kolor[WHITE].t =whitetab[0];

kolor[BLUE].t = bluetab[0];

Something doesn't seem right here. You have declared
kolor as an array of 1 element. Yet you indexing in
kolor[WHITE] and kolor[BLUE] would be equivalent to:
kolor[0] and kolor[1]. This would require an array of
at least two elements.
 
S

sipetar

Al said:
Something doesn't seem right here. You have declared
kolor as an array of 1 element. Yet you indexing in
kolor[WHITE] and kolor[BLUE] would be equivalent to:
kolor[0] and kolor[1]. This would require an array of
at least two elements.
return 0;

}
yes I see that now, I am beginner..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top