473,395 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with an array of pointers

I have a set of data structures that are collected together in array.
This array is in turn packaged in a struct itself. The structs look
like this:-

struct index_entry_t
{
char *id;
char *description;
};
typedef struct index_entry_t index_entry_t;

struct index_t
{
int no_of_entries;
index_entry_t *table[];
};
typedef struct index_t index_t;

The member 'table' of index_t contains an array of pointers to
index_entry_t's.

The program then goes on to make a pointer to an index_t. The result
is shown below:-
_____________
i ------>|index_t |
|_____________|
|no_of_entries|
|_____________|
|table | _______ _____________
|____________--------->| ----------------->|index_entry_t|
|_______| |_____________|
| ----------> |id |
|_______| |_____________|
| ----------> |description |
|_______| |_____________|
| ... |

My problem is, I can't find the right syntax to access the members, ie
the index_entry_t's.

To me these seem the right ways:

(i->table[n])->id
or
*(i->table[n]).id

but they both give the error:
test_ind2.c:86: error: request for member `description' in something
not a structure or union

on my compiler (gcc).

What is wrong?
Nov 14 '05 #1
9 1498
Rob Thorpe wrote:

I have a set of data structures that are collected together in array.
This array is in turn packaged in a struct itself. The structs look
like this:-

struct index_entry_t
{
char *id;
char *description;
};
typedef struct index_entry_t index_entry_t;

struct index_t
{
int no_of_entries;
index_entry_t *table[];
};
typedef struct index_t index_t;


This line:
index_entry_t *table[];
is not standard C.

--
pete
Nov 14 '05 #2
pete wrote:
Rob Thorpe wrote:
I have a set of data structures that are collected together in array.
This array is in turn packaged in a struct itself. The structs look
like this:-

struct index_entry_t
{
char *id;
char *description;
};
typedef struct index_entry_t index_entry_t;

struct index_t
{
int no_of_entries;
index_entry_t *table[];
};
typedef struct index_t index_t;

This line:
index_entry_t *table[];
is not standard C.


And why do you say this? Has the current standard been repealed? Have
flexible array members been removed from the standard?


Nov 14 '05 #3
pete <pf*****@mindspring.com> wrote:
Rob Thorpe wrote:

I have a set of data structures that are collected together in array.
This array is in turn packaged in a struct itself. The structs look
like this:-

struct index_entry_t
{
char *id;
char *description;
};
typedef struct index_entry_t index_entry_t;

struct index_t
{
int no_of_entries;
index_entry_t *table[];
};
typedef struct index_t index_t;


This line:
index_entry_t *table[];
is not standard C.


It's a flexible array member (N869 6.7.2.1#15)
This was introduced in ISO 9899:1999 as a legal alternative
to the 'struct hack'.

To the OP: the code compiles without error for me (using
gcc 3.4.2 in C99 mode), can you post the exact code that
gives the problem? My code was:

int main(void)
{
struct index_t *i;
i->table[4]->id;
}
Nov 14 '05 #4
Martin Ambuhl wrote:

pete wrote:

This line:
index_entry_t *table[];
is not standard C.


And why do you say this?


I frequently forget about C99 and make mistakes like that.

--
pete
Nov 14 '05 #5
On 6 Oct 2004 10:41:53 -0700, ro***********@antenova.com (Rob Thorpe)
wrote:
I have a set of data structures that are collected together in array.
This array is in turn packaged in a struct itself. The structs look
like this:-

struct index_entry_t
{
char *id;
char *description;
};
typedef struct index_entry_t index_entry_t;

struct index_t
{
int no_of_entries;
index_entry_t *table[];
Obviously in your real code there is some value specified for the
array size.
};
typedef struct index_t index_t;

The member 'table' of index_t contains an array of pointers to
index_entry_t's.

The program then goes on to make a pointer to an index_t. The result
is shown below:-
snip diagram
My problem is, I can't find the right syntax to access the members, ie
the index_entry_t's.

To me these seem the right ways:

(i->table[n])->id
This is correct but the parentheses are unnecessary.
or
*(i->table[n]).id
This is not correct since . has higher precedence than *. You need
(*i->table[n]).id

but they both give the error:
test_ind2.c:86: error: request for member `description' in something
not a structure or union
This error must be for a different line of code since the description
member is not referenced in either.

on my compiler (gcc).

What is wrong?


Post the real code. Preferably a small complete compilable example
that demonstrates the problem.
<<Remove the del for email>>
Nov 14 '05 #6
ol*****@inspire.net.nz (Old Wolf) wrote in message news:<84**************************@posting.google. com>...
pete <pf*****@mindspring.com> wrote:
Rob Thorpe wrote:

I have a set of data structures that are collected together in array.
This array is in turn packaged in a struct itself. The structs look
like this:-

struct index_entry_t
{
char *id;
char *description;
};
typedef struct index_entry_t index_entry_t;

struct index_t
{
int no_of_entries;
index_entry_t *table[];
};
typedef struct index_t index_t;
This line:
index_entry_t *table[];
is not standard C.


It's a flexible array member (N869 6.7.2.1#15)
This was introduced in ISO 9899:1999 as a legal alternative
to the 'struct hack'.


Aha, this construct is only legal C99. Unfortunately, my code must
work on a C90 or C++ compiler.

It seems to make perfect sense to write "index_entry_t *table[];",
after all I want a pointer to an array of index_entry_t's and I want
the pointer named table. The new standard makes more sense than the
old on this point.

If I replace this with index_entry_t **table; it works.

To the OP: the code compiles without error for me (using
gcc 3.4.2 in C99 mode), can you post the exact code that
gives the problem? My code was:

int main(void)
{
struct index_t *i;
i->table[4]->id;
}


Thanks to everyone who posted.
Nov 14 '05 #7
ro***********@antenova.com (Rob Thorpe) wrote in message news:<1a**************************@posting.google. com>...
I have a set of data structures that are collected together in array.
This array is in turn packaged in a struct itself. The structs look
like this:-

struct index_entry_t
{
char *id;
char *description;
};
typedef struct index_entry_t index_entry_t;

struct index_t
{
int no_of_entries;
index_entry_t *table[];
};
typedef struct index_t index_t;

[snip diagram]
My problem is, I can't find the right syntax to access the members, ie
the index_entry_t's.

To me these seem the right ways:

(i->table[n])->id
or
*(i->table[n]).id

but they both give the error:
test_ind2.c:86: error: request for member `description' in something
not a structure or union

on my compiler (gcc).

What is wrong?

Two questions:

1. Which version of gcc?
2. Are you sure you have the right statement for the error?

The first form should work just fine (type of i->table[n] should be
index_entry_t *), unless I've missed something. The second is wrong,
it should be (*i->table[n]).id (remember, x->y is equivalent to
(*x).y, not *(x).y).
Nov 14 '05 #8
ro***********@antenova.com (Rob Thorpe) wrote:
Rob Thorpe wrote:
>
> struct index_t
> {
> int no_of_entries;
> index_entry_t *table[];
> };
> typedef struct index_t index_t;

It seems to make perfect sense to write "index_entry_t *table[];",
after all I want a pointer to an array of index_entry_t's and I want
the pointer named table.


In a declaration, square brackets indicate an array.
The star indicates a pointer.
When both are combined in this case, the square brackets
have precedence; the original 'table' is an array of
pointers, not a pointer-to-array.
If I replace this with index_entry_t **table; it works.
Naturally. This declares a pointer (which you can then
point to the first member of an array).

Read through chapter 6 of the FAQ if you haven't already (you
may find a few useful tidbits you didn't already know).
The new standard makes more sense than the old on this point.


I'm not sure that it does what you think it does; it declares
an array of pointers, where the size of the array is unknown.
The only correct way to allocate a struct like this is:

struct index_t *ptr = malloc(sizeof (struct index_t)
+ sizeof(index_entry_t *) * N);

and then you can access ptr->table[0], ptr->table[1],
and so on, up to ptr->table[N-1] safely.

In C90 it wasn't possible to have a structure that
contained an array whose size wasn't known until runtime.
The common workaround was called the 'struct hack':
basically the same as what we just did, execpt that the
array member was declared as
index_entry_t *table[1];
and you would go ahead an access table[2] etc. anyway.
Nov 14 '05 #9
ol*****@inspire.net.nz (Old Wolf) wrote in message news:<84**************************@posting.google. com>...
ro***********@antenova.com (Rob Thorpe) wrote:
> Rob Thorpe wrote:
> >
> > struct index_t
> > {
> > int no_of_entries;
> > index_entry_t *table[];
> > };
> > typedef struct index_t index_t;
It seems to make perfect sense to write "index_entry_t *table[];",
after all I want a pointer to an array of index_entry_t's and I want
the pointer named table.


In a declaration, square brackets indicate an array.
The star indicates a pointer.
When both are combined in this case, the square brackets
have precedence; the original 'table' is an array of
pointers, not a pointer-to-array.


That's what I meant, when I coded it, I forgot and confused myself in
the time since. The reasons of the error seems obvious now.
If I replace this with index_entry_t **table; it works.
Naturally. This declares a pointer (which you can then
point to the first member of an array).

Read through chapter 6 of the FAQ if you haven't already (you
may find a few useful tidbits you didn't already know).


That's just what I've been doing in the past couple of days,
interesting reading.
The new standard makes more sense than the old on this point.
I'm not sure that it does what you think it does; it declares
an array of pointers, where the size of the array is unknown.
The only correct way to allocate a struct like this is:

struct index_t *ptr = malloc(sizeof (struct index_t)
+ sizeof(index_entry_t *) * N);

and then you can access ptr->table[0], ptr->table[1],
and so on, up to ptr->table[N-1] safely.


I see. That's not what I want since I can't tell how many
index_entry_t's there will be when I initialise the index_t. I need
to malloc 'table' separately.
In C90 it wasn't possible to have a structure that
contained an array whose size wasn't known until runtime.
The common workaround was called the 'struct hack':
basically the same as what we just did, execpt that the
array member was declared as
index_entry_t *table[1];
and you would go ahead an access table[2] etc. anyway.


I read about this, but never understood why anyone would want to do
it, I get it now, though it looks best avoided.
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Krzysztof | last post by:
Hi! I have a class: some_class{ private: char* seq; public: some_class(char* se); char* ret_seq(); ....
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
24
by: Bangalore | last post by:
Hi all, I have a problem in accessing elements using overloaded operator . Consider, const int SIZE=10; int FALSE=0; class Array { private: int x; public:
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
2
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
13
by: Bob | last post by:
I have been working on the following program. The goal is to have a tokenizing routine that avoids some of the problems of strtok(), the comments should explain the features. This runs fine on...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.