473,320 Members | 2,104 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,320 software developers and data experts.

C/C++ usage of typedef struct

I'm trying to define a data type that is an array of structures. I
have borrowed the following C code, which appears in a textbook by Aho
and Ullman:

typedef struct {
float dist;
LIST successors;
POTNODE toPOT;
} GRAPH[MAX];

While g++ doesn't object to this definition, the first time we try to
use GRAPH as a parameter in a function prototype g++ complains that the
"non-local function...uses anonymous type." I can only assume this is
caused by differences between C and C++. Any ideas for how I can
implement the desired array of structures? Your help would be
appreciated.

Best Regards,
Josh

Aug 27 '06 #1
9 6677

Josh wrote:
I'm trying to define a data type that is an array of structures. I
have borrowed the following C code, which appears in a textbook by Aho
and Ullman:

typedef struct {
float dist;
LIST successors;
POTNODE toPOT;
} GRAPH[MAX];

While g++ doesn't object to this definition, the first time we try to
use GRAPH as a parameter in a function prototype g++ complains that the
"non-local function...uses anonymous type." I can only assume this is
caused by differences between C and C++. Any ideas for how I can
implement the desired array of structures? Your help would be
appreciated.
At the most basic level, like this:

struct Item {
float dist;
LIST successors;
POTNODE toPOT;
};

Item graph[MAX];

Better yet, make Item into a class and design an interface so that its
data members are not public, and then use a std::vector or other
container from the standard library rather than using an array. If
that makes no sense to you, you need to get a good book introducing
C++. I recommend Accelerated C++ by Koenig and Moo.

Best regards,

Tom

Aug 27 '06 #2

"Josh" <jz*****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
I'm trying to define a data type that is an array of structures. I
have borrowed the following C code, which appears in a textbook by Aho
and Ullman:

typedef struct {
float dist;
LIST successors;
POTNODE toPOT;
} GRAPH[MAX];

While g++ doesn't object to this definition, the first time we try to
use GRAPH as a parameter in a function prototype g++ complains that the
"non-local function...uses anonymous type." I can only assume this is
caused by differences between C and C++. Any ideas for how I can
implement the desired array of structures? Your help would be
appreciated.

Best Regards,
Josh
This is an unnamed type. The structure doesn't have a name. So give it
one. Incidently, you can get rid of the typedef in C++.

struct GraphStruct{
float dist;
LIST successors;
POTNODE toPOT;
} GRAPH[MAX];

Now this structure has a name, GraphStruct.

This is the simplest way to fix it.
Aug 27 '06 #3
Jim Langston wrote:
struct GraphStruct {
float dist;
LIST successors;
POTNODE toPOT;
} GRAPH[MAX];
Thanks, Jim. I changed the structure definition to read

struct GRAPH {
float dist;
LIST successors;
POTNODE toPOT;
} graph[MAX];

This works well enough, but it's not exactly what I had in mind. Is
there a way to define a structure that is an array in which each
element has multiple members as above?

Best Regards,
Josh

P.S. I should also mention I would prefer to implement without the use
of container classes from the standard library.

Aug 27 '06 #4
Josh wrote:
I'm trying to define a data type that is an array of structures.
Usually a bad idea; it can lead to obfuscated code.
I have borrowed the following C code, which appears in a
textbook by Aho and Ullman:

typedef struct {
float dist;
LIST successors;
POTNODE toPOT;
} GRAPH[MAX];
This is similar to:

struct _unnamed {
float dist;
/* etc */
};

typedef struct _unnamed GRAPH[MAX];

except that the struct has no name at all.
>
While g++ doesn't object to this definition, the first time we try to
use GRAPH as a parameter in a function prototype g++ complains that the
"non-local function...uses anonymous type." I can only assume this is
caused by differences between C and C++.
C++ does not allow you to declare an externally-visible function
that accepts an anonymous type as parameter -- presumably
because there is no way this function can be called externally!
Any ideas for how I can implement the desired array of structures?
Your help would be appreciated.
The easiest fix is to give your struct type a name, ie. stick something
after the keyword "struct" and before the open brace.

The second-easiest is to declare the function as having internal
linkage (eg. by prefixing the declaration with "static", or putting
it in an anonymous namespace).

Personally I would suggest you avoid the typedef entirely, and
write out the array parameter as you need it.

Aug 28 '06 #5
Josh wrote:
struct GRAPH {
float dist;
LIST successors;
POTNODE toPOT;
} graph[MAX];

This works well enough, but it's not exactly what I had in mind. Is
there a way to define a structure that is an array in which each
element has multiple members as above?
With this new defintion of GRAPH, the line:
GRAPH graph[5][MAX];

has the same effect as this would have with your original definition:
GRAPH graph[5];

and it is more obvious what the code is doing.

Aug 28 '06 #6
Josh posted:
typedef struct {
float dist;
LIST successors;
POTNODE toPOT;
} GRAPH[MAX];
Firstly, only use CAPSLOCK for macro names.

Change it to:

struct MyStruct {
float dist;
List successors;
PotNode toPot;
};

typedef MyStruct Graph[MAX];

Or, if you want to retain compatibility with C:

typedef struct MyStruct {
float dist;
List successors;
PotNode toPot;
} MyStruct;

typedef MyStruct Graph[MAX];

--

Frederick Gotham
Aug 28 '06 #7
Josh wrote:
Jim Langston wrote:
struct GraphStruct {
float dist;
LIST successors;
POTNODE toPOT;
} GRAPH[MAX];

Thanks, Jim. I changed the structure definition to read

struct GRAPH {
float dist;
LIST successors;
POTNODE toPOT;
} graph[MAX];

This works well enough, but it's not exactly what I had in mind. Is
there a way to define a structure that is an array in which each
element has multiple members as above?
You can typedef the array type, but I wouldn't recommend it. Unless
you're dealing with a bunch of legacy C code, the only time I can see
that kind of construct as being useful is for something that never
changes size, like say a hardware descriptor table. This certainly does
not appear to be the case here. But even then the typedef only
obfuscates the code. It is better to have clarity at the point of use
than at the point of declaration.
P.S. I should also mention I would prefer to implement without the use
of container classes from the standard library.
Your reasons may well be valid, but in general excuses for not using
the standard library are rather poor. Perhaps you would like to tell us
what you're really trying to do?

Regards,
Bart.

Aug 28 '06 #8
Frederick Gotham wrote:
struct MyStruct {
float dist;
List successors;
PotNode toPot;
};

typedef MyStruct Graph[MAX];

Or, if you want to retain compatibility with C:

typedef struct MyStruct {
float dist;
List successors;
PotNode toPot;
} MyStruct;

typedef MyStruct Graph[MAX];
Thanks for bearing with my inexperience, guys. I think I'm going with
the code suggested by Frederick and several others.

Bart, if you still want to know where this code is being used, it's an
implementation of Dijkstra's algorithm for shortest paths.

-Josh

Aug 28 '06 #9
Josh Zenker wrote:
Bart, if you still want to know where this code is being used, it's an
implementation of Dijkstra's algorithm for shortest paths.
I figured it's something like that. Now, Aho & Ullman may be
respectable authors when it comes to algorithms, but that doesn't mean
you should copy their code blindly. In fact, textbook code is usually
not the best example to follow for writing real code, especially C
textbook code in a C++ program.

Just my two cents.

Regards,
Bart.

Aug 29 '06 #10

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

Similar topics

15
by: Merrill & Michele | last post by:
typedef struct { WORD versionNumber; WORD offset; } MENUITEMTEMPLATEHEADER; This is from vol 5 of unnamed platform's programmer's reference. I could make this conforming by enclosing...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
6
by: Alex | last post by:
Hello people, I am getting errors from VS2003 when working with typedef'ed types. For example, assume that I have a type T, defined in a 3rd party include file based on some condition #if...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
0
by: shrini | last post by:
hi all..... im not clear of the usage of the following typedef for the function pointer.. can any one help me out, briefing the syntax or usage importance?? code is below.. typedef void...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.