pointer to structure array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • estantep@gmail.com

    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

  • Ark Khasin

    #2
    Re: pointer to structure array

    estantep@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]

    Comment

    • pete

      #3
      Re: pointer to structure array

      estantep@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

      Comment

      • estantep@gmail.com

        #4
        Re: pointer to structure array

        Ark and Pete, Thank you _very_ much.

        You guys are the best.


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

        Comment

        • Ben Bacarisse

          #5
          Re: pointer to structure array

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

          Comment

          • Barry Schwarz

            #6
            Re: pointer to structure array

            On Sun, 19 Aug 2007 22:52:12 -0000, estantep@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

            Comment

            • Chris Torek

              #7
              Re: pointer to structure array

              >On Sun, 19 Aug 2007 22:52:12 -0000, estantep@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 <akdsc31vcc3oh4 noc64e3n22bjlcn hu4di@4ax.com>
              Barry Schwarz <schwarzb@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.

              Comment

              • ¬a\\/b

                #8
                Re: pointer to structure array

                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 <akdsc31vcc3oh4 noc64e3n22bjlcn hu4di@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

                Comment

                • Mark Bluemel

                  #9
                  Re: pointer to structure array

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

                  Comment

                  • santosh

                    #10
                    Re: pointer to structure array

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

                    Comment

                    • santosh

                      #11
                      Re: pointer to structure array

                      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.

                      Comment

                      • CBFalconer

                        #12
                        Re: pointer to structure array

                        Barry Schwarz wrote:
                        estantep@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

                        Comment

                        • Barry Schwarz

                          #13
                          Re: pointer to structure array

                          On Thu, 23 Aug 2007 23:58:47 -0400, CBFalconer <cbfalconer@yah oo.com>
                          wrote:
                          >Barry Schwarz wrote:
                          >estantep@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_structur e, 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

                          Comment

                          Working...