473,382 Members | 1,441 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,382 software developers and data experts.

pointer to structure array

Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];

int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
int i, j;
for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
}
}
return 0;
}
int main(int argc, char* argv[]){

....
print_structure(link);
....
}

Could anybody help me spot the problem?

Thanks,

Paulo

Aug 19 '07 #1
12 2241
es******@gmail.com wrote:
Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];

int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
int i, j;
for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
}
}
return 0;
}
int main(int argc, char* argv[]){

...
print_structure(link);
...
}

Could anybody help me spot the problem?

Thanks,

Paulo
print_struct is called not according to its type (inferred from its
definition). [Remove the * in the definition]
Aug 19 '07 #2
es******@gmail.com wrote:
>
Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];

int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
int i, j;
for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
}
}
return 0;
}

int main(int argc, char* argv[]){

...
print_structure(link);
...
}

Could anybody help me spot the problem?
/* BEGIN new.c */

#include <stdio.h>

#define MAX_GRAPH 3

typedef struct edge {
long delay;
long bandw;
} edge;

size_t nodes = MAX_GRAPH;

void print_structure(edge (*graph)[MAX_GRAPH]);

int main(void)
{
edge link[MAX_GRAPH][MAX_GRAPH] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
};

print_structure(link);
return 0;
}

void print_structure(edge (*graph)[MAX_GRAPH])
{
size_t i, j;

for(i = 0; i < nodes; i++){
for(j=0;j < nodes; j++){
printf("[%d][%d].bandw = %ld\t", i, j, graph[i][j].bandw);
printf("[%d][%d].delay = %ld\n", i, j, graph[i][j].delay);
}
}
}

/* END new.c */
--
pete
Aug 20 '07 #3
Ark and Pete, Thank you _very_ much.

You guys are the best.
pete wrote:
es******@gmail.com wrote:

Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];

int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
int i, j;
for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
}
}
return 0;
}

int main(int argc, char* argv[]){

...
print_structure(link);
...
}

Could anybody help me spot the problem?

/* BEGIN new.c */

#include <stdio.h>

#define MAX_GRAPH 3

typedef struct edge {
long delay;
long bandw;
} edge;

size_t nodes = MAX_GRAPH;

void print_structure(edge (*graph)[MAX_GRAPH]);

int main(void)
{
edge link[MAX_GRAPH][MAX_GRAPH] = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
};

print_structure(link);
return 0;
}

void print_structure(edge (*graph)[MAX_GRAPH])
{
size_t i, j;

for(i = 0; i < nodes; i++){
for(j=0;j < nodes; j++){
printf("[%d][%d].bandw = %ld\t", i, j, graph[i][j].bandw);
printf("[%d][%d].delay = %ld\n", i, j, graph[i][j].delay);
}
}
}

/* END new.c */
--
pete
Aug 20 '07 #4
es******@gmail.com writes:
Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];

int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
Loose the initial *. You end up with:

edge graph[MAX_GRAPH][MAX_GRAPH]

is fine, but the first size is redundant -- because a pointer to the
array will be passed, C only insists that you say exactly what each
element of this array is like, not how many there are. This one can
also (and equivalently) write:

edge graph[][MAX_GRAPH]
edge (*graph)[MAX_GRAPH]
int i, j;
for(i=0;i<nodes;i++){
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
Since the array contains structures, you need to use . to access the
elements. Note, also, that you should use %ld to print 'long int's.
}
}
return 0;
}
int main(int argc, char* argv[]){

...
print_structure(link);
...
}
--
Ben.
Aug 20 '07 #5
On Sun, 19 Aug 2007 22:52:12 -0000, es******@gmail.com wrote:
>Hello,

I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.
Why do you execute code (the only way to get a seg fault) when the
compiler has already told you that the code is incorrect? If you did
not receive a diagnostic for the constraint violation in your call to
print_structure, you need to up the warning level of your compiler.
>
The best I could get out from googling was:

#define MAX_GRAPH 256

typedef struct edge{
long delay;
long bandw;
} edge;

edge link[MAX_GRAPH][MAX_GRAPH];
link is an array of struct.
>
int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
This function is expecting an array of pointer to struct. Due the
fact that an array expression in this context is converted to a
pointer to the first element, the actual type the function is
expecting is
edge (*)(*[MAX_GRAPH])
int i, j;
for(i=0;i<nodes;i++){
We won't ask about nodes.
for(j=0;j<nodes;j++){
printf("[%d][%d].bandw = %d\t\t", i, j, graph[i][j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[i][j]->delay);
}
}
return 0;
}
int main(int argc, char* argv[]){

...
print_structure(link);
Here you pass the array. Since the same conversion is performed, the
actual type passed is
edge (*)[MAX_GRAPH]

Notice that these are not the same type. There is also no implicit
conversion between these two types. Hence the required diagnostic.

If you delete the asterisk from the parameter in the function
definition, your code will be consistent. Then you can start to worry
about correcting the undetected coding errors, such as using %d when
you actually pass printf a long instead of an int.
Remove del for email
Aug 24 '07 #6
>On Sun, 19 Aug 2007 22:52:12 -0000, es******@gmail.com wrote:
[given: a typedef-alias named "edge", a #define constant for
MAX_GRAPH, and]
>>int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){
In article <ak********************************@4ax.com>
Barry Schwarz <sc******@doezl.netwrote:
>This function is expecting an array of pointer to struct.
Well, the type of the argument named "graph" is -- before the
adjustment you describe in a moment -- "array MAX_GRAPH of array
MAX_GRAPH of pointer to what-edge-is-short-for". So, array of
arrays, each element of the nested array being a pointer to struct
(since edge is short for a struct type).
>Due the fact that an array expression in this context is converted
to a pointer to the first element, the actual type the function is
expecting is
edge (*)(*[MAX_GRAPH])
Right -- a formal parameter whose type *appears* to be "array N of
T" (for some integer N, or even an omitted integer, and any valid
type T) really gets declared as having type "pointer to T", replacing
the first (and only the first!) "array of" part of the expanded
English version with "pointer to" -- except that the C spelling of
the type "pointer to array MAX_GRAPH of pointer to
whatever-edge-is-short-for" is actually:

edge *(*)[MAX_GRAPH]

The parentheses "look weird" because we lack the identifier that
would go in there. If this were an actual declaration instead of
just a type-name, we would have:

edge *(*ptr)[MAX_GRAPH];

Here, the need for the parentheses is clearer (if not exactly
"clear"!) since we need to bind the inner (second) "*" to "ptr",
then bind the "[MAX_GRAPH]" to that, and then bind the outer (first)
"*" to that, and finally bind the typedef-alias "edge" to that.
Without parentheses, the "[MAX_GRAPH]" would bind to "ptr" first,
then the second "*" would bind to that, and so on.

To turn a C declaration into a type-name, one simply removes the
identifier that was being declared. In this case, it results in
parentheses around the second "*".
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Aug 24 '07 #7
On 24 Aug 2007 06:43:22 GMT, Chris Torek wrote:
>[given: a typedef-alias named "edge", a #define constant for
MAX_GRAPH, and]
>>>int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){

In article <ak********************************@4ax.com>
Barry Schwarz wrote:
>>This function is expecting an array of pointer to struct.

Well, the type of the argument named "graph" is -- before the
adjustment you describe in a moment -- "array MAX_GRAPH of array
MAX_GRAPH of pointer to what-edge-is-short-for". So, array of
arrays, each element of the nested array being a pointer to struct
(since edge is short for a struct type).
>>Due the fact that an array expression in this context is converted
to a pointer to the first element, the actual type the function is
expecting is
edge (*)(*[MAX_GRAPH])

Right -- a formal parameter whose type *appears* to be "array N of
T" (for some integer N, or even an omitted integer, and any valid
type T) really gets declared as having type "pointer to T", replacing
the first (and only the first!) "array of" part of the expanded
English version with "pointer to" -- except that the C spelling of
the type "pointer to array MAX_GRAPH of pointer to
whatever-edge-is-short-for" is actually:

edge *(*)[MAX_GRAPH]

The parentheses "look weird" because we lack the identifier that
would go in there. If this were an actual declaration instead of
just a type-name, we would have:

edge *(*ptr)[MAX_GRAPH];

Here, the need for the parentheses is clearer (if not exactly
"clear"!) since we need to bind the inner (second) "*" to "ptr",
then bind the "[MAX_GRAPH]" to that, and then bind the outer (first)
"*" to that, and finally bind the typedef-alias "edge" to that.
Without parentheses, the "[MAX_GRAPH]" would bind to "ptr" first,
then the second "*" would bind to that, and so on.

To turn a C declaration into a type-name, one simply removes the
identifier that was being declared. In this case, it results in
parentheses around the second "*".
possibly
i'm not enough smart
possibly
all you game with nothing (i think all is easy but there are people
that complicates languages)
with an easy language is possible to write something that in a
difficult language can not to be written

i hope i offend nobody

Aug 24 '07 #8
¬a\/b wrote:
On 24 Aug 2007 06:43:22 GMT, Chris Torek wrote:
<snip>
>
possibly
i'm not enough smart
possibly
all you game with nothing (i think all is easy but there are people
that complicates languages)
with an easy language is possible to write something that in a
difficult language can not to be written

i hope i offend nobody
I'd have to be able to understand what you've written before I could be
offended by it...
Aug 24 '07 #9
¬a\/b wrote:
On 24 Aug 2007 06:43:22 GMT, Chris Torek wrote:
[yet another superb but intricate explanation]
possibly
i'm not enough smart
possibly
all you game with nothing (i think all is easy but there are people
that complicates languages)
with an easy language is possible to write something that in a
difficult language can not to be written

i hope i offend nobody
Check out Lisp or Forth.

Aug 24 '07 #10
Mark Bluemel wrote:
¬a\/b wrote:
>On 24 Aug 2007 06:43:22 GMT, Chris Torek wrote:
<snip>
>>
possibly
i'm not enough smart
possibly
all you game with nothing (i think all is easy but there are people
that complicates languages)
with an easy language is possible to write something that in a
difficult language can not to be written

i hope i offend nobody

I'd have to be able to understand what you've written before I could be
offended by it...
I think he is saying that C's rules complicate what would otherwise be easy.
This is the typical reaction to C from someone who knows only one hardware
architecture and fails to understand that the reason behind much of the
intricacy behind some of the rules of C is the desire to maintain maximum
portability of the language. It is also an old language that people only
exposed to modern machines and languages find cruelly primitive or stupid.
It's the default reaction from much of the current alt.lang.asm crowd.

Aug 24 '07 #11
Barry Schwarz wrote:
es******@gmail.com wrote:
>I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

Why do you execute code (the only way to get a seg fault) when the
compiler has already told you that the code is incorrect? If you did
not receive a diagnostic for the constraint violation in your call to
print_structure, you need to up the warning level of your compiler.
Why do you assume that the compiler objected? I can think of
various easy ways to get to such a fault without a compiler squeak.

To the OP: Report your complete code, and someone will diagnose.
Reduce it to at most 200 lines that is compilable, standard, and
exhibits the flaw. For c.l.c. publication, keep linelengths under
72 and use neither tabs nor // comments.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 25 '07 #12
On Thu, 23 Aug 2007 23:58:47 -0400, CBFalconer <cb********@yahoo.com>
wrote:
>Barry Schwarz wrote:
>es******@gmail.com wrote:
>>I am having some trouble trying to pass a pointer to an structure
array (as function argument), when executing I get seg faults.

Why do you execute code (the only way to get a seg fault) when the
compiler has already told you that the code is incorrect? If you did
not receive a diagnostic for the constraint violation in your call to
print_structure, you need to up the warning level of your compiler.

Why do you assume that the compiler objected? I can think of
various easy ways to get to such a fault without a compiler squeak.
Because, as you carefully chose to omit from your quote, the original
post passed an argument of type
edge (*)[MAX_GRAPH]
to a function that was expecting one of type
edge (*)(*[MAX_GRAPH])

Since there is no implicit conversion between the two types, it is a
constraint violation requiring a diagnostic.

The fact that there are other reasons for receiving a seg fault has no
bearing on whether the code requires a compile time diagnostic.
Remove del for email
Aug 29 '07 #13

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

Similar topics

8
by: Frank Münnich | last post by:
Hi there.. My name is Frank Münnich. I've got a question about pointers that refer to an array of a structure. How do I declare that type? If I have declared a structure struct mystruc {...
20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
1
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
8
by: Sam | last post by:
I have a situation occuring in my code and I just can't see to figure out why I have an structure called employee that will put all of the employee id's into a char array set to 10 struct...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.