pointer to structure array

E

estantep

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[j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[j]->delay);
}
}
return 0;
}


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

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

Could anybody help me spot the problem?

Thanks,

Paulo
 
A

Ark Khasin

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[j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[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]
 
P

pete

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[j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[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[j].bandw);
printf("[%d][%d].delay = %ld\n", i, j, graph[j].delay);
}
}
}

/* END new.c */
 
E

estantep

Ark and Pete, Thank you _very_ much.

You guys are the best.

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[j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[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[j].bandw);
printf("[%d][%d].delay = %ld\n", i, j, graph[j].delay);
}
}
}

/* END new.c */
 
B

Ben Bacarisse

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[j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[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);
...
}
 
B

Barry Schwarz

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[j]->bandw); //
SEG FAULT!
printf("[%d][%d].delay = %d\n", i, j, graph[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
 
C

CBFalconer

Barry said:
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.
 
C

Chris Torek

a typedef-alias named "edge", a #define constant for
MAX_GRAPH, and]
int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){

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 "*".
 
¬

¬a\\/b

[given: a typedef-alias named "edge", a #define constant for
MAX_GRAPH, and]
int print_structure(edge *graph[MAX_GRAPH][MAX_GRAPH]){

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
 
M

Mark Bluemel

¬a\/b said:
On 24 Aug 2007 06:43:22 GMT, Chris Torek wrote:
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...
 
S

santosh

¬a\/b said:
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.
 
S

santosh

Mark said:
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.
 
B

Barry Schwarz

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top