Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer to structure array

Reply
Thread Tools

pointer to structure array

 
 
estantep@gmail.com
Guest
Posts: n/a
 
      08-19-2007
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

 
Reply With Quote
 
 
 
 
Ark Khasin
Guest
Posts: n/a
 
      08-19-2007
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]
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-19-2007
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
 
Reply With Quote
 
estantep@gmail.com
Guest
Posts: n/a
 
      08-20-2007
Ark and Pete, Thank you _very_ much.

You guys are the best.


pete wrote:
> 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


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      08-20-2007
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.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-24-2007
On Sun, 19 Aug 2007 22:52:12 -0000, 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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-24-2007
Barry Schwarz wrote:
> 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

 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      08-24-2007
>On Sun, 19 Aug 2007 22:52:12 -0000, 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 <>
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 "*".
--
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.
 
Reply With Quote
 
¬a\\/b
Guest
Posts: n/a
 
      08-24-2007
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 <>
>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

 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      08-24-2007
¬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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Simple structure and copying data to pointer of the same structure A C++ 27 04-16-2011 11:07 PM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments