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? 9 1513
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
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?
pete <pf*****@mindsp ring.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;
}
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
On 6 Oct 2004 10:41:53 -0700, ro***********@a ntenova.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>> ol*****@inspire .net.nz (Old Wolf) wrote in message news:<84******* *************** ****@posting.go ogle.com>... pete <pf*****@mindsp ring.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_entr y_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. ro***********@a ntenova.com (Rob Thorpe) wrote in message news:<1a******* *************** ****@posting.go ogle.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). ro***********@a ntenova.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_entr y_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_en try_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. ol*****@inspire .net.nz (Old Wolf) wrote in message news:<84******* *************** ****@posting.go ogle.com>... ro***********@a ntenova.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_entr y_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_en try_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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Krzysztof |
last post by:
Hi!
I have a class:
some_class{
private:
char* seq;
public:
some_class(char* se);
char* ret_seq();
....
|
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.
----------------------------------------------
//example 1:
typedef int t_Array;
int main(int argc, char* argv)
|
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:
|
by: Davy |
last post by:
Hi all,
I found
char x={"my"};
can be compiled.
But
char x;
x={"my"};
can not be compiled.
|
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;
| |
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 have an array of structs containing data which I'd like to output
ordered based on the value of a single member. I was wondering if there is
a relatively simple way of doing this without actually modifying the
structure of the array?
I had a...
|
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 */
#include <time.h>
|
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) What's the difference between these 3 statements:
(i) memcpy(&b, &KoefD, n); // this works somewhere in my code
|
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 Solaris/gcc, but crashes when run from VC++ (C mode).
The problem occurs after the first true reallocation (second pass
through main loop). The little debug part at the end of the loop prints
one time, it crashes before a second is displayed. Seems...
|
by: sam_cit |
last post by:
Hi Everyone,
I have the following structure in my program
struct sample
{
char *string;
int string_len;
};
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |