pointer to structure array

Discussion in 'C Programming' started by estantep@gmail.com, Aug 19, 2007.

  1. Guest

    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
    , Aug 19, 2007
    #1
    1. Advertising

  2. Ark Khasin Guest

    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[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]
    Ark Khasin, Aug 20, 2007
    #2
    1. Advertising

  3. pete Guest

    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[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 */


    --
    pete
    pete, Aug 20, 2007
    #3
  4. Guest

    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[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 */
    >
    >
    > --
    > pete
    , Aug 20, 2007
    #4
  5. 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[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);
    > ...
    > }


    --
    Ben.
    Ben Bacarisse, Aug 20, 2007
    #5
  6. 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[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
    Barry Schwarz, Aug 24, 2007
    #6
  7. CBFalconer Guest

    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
    CBFalconer, Aug 24, 2007
    #7
  8. Chris Torek Guest

    >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.
    Chris Torek, Aug 24, 2007
    #8
  9. ¬a\\/b Guest

    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
    ¬a\\/b, Aug 24, 2007
    #9
  10. Mark Bluemel Guest

    ¬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...
    Mark Bluemel, Aug 24, 2007
    #10
  11. santosh Guest

    ¬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.
    santosh, Aug 24, 2007
    #11
  12. santosh Guest

    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.
    santosh, Aug 24, 2007
    #12
  13. On Thu, 23 Aug 2007 23:58:47 -0400, CBFalconer <>
    wrote:

    >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.
    >


    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
    Barry Schwarz, Aug 29, 2007
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sangeetha

    Array of pointer Vs Pointer to Array

    sangeetha, Oct 8, 2004, in forum: C Programming
    Replies:
    9
    Views:
    347
    Tim Rentsch
    Oct 9, 2004
  2. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    674
    Martin Ambuhl
    Jan 28, 2008
  3. RSL
    Replies:
    14
    Views:
    3,250
  4. A
    Replies:
    27
    Views:
    1,593
    Jorgen Grahn
    Apr 17, 2011
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    450
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page