Returning structs containing arrays

Discussion in 'C Programming' started by Michel Rouzic, Nov 20, 2005.

  1. It's the first time I try using structs, and I'm getting confused with
    it and can't make it work properly

    I firstly define the structure by this : typedef struct { char *l1; int
    *l2; int Nval; } *arrays;

    It's supposed to be a structure containing an array of chars, an array
    of ints and an int.

    I declare functions like this : arrays *parseline(char *line, int N)

    and I point to the arrays I want to return like this :

    out.l1=displayln;
    out.l2=sumline;
    out.Nval=Nval;
    return out;

    When compiling it gives me an error, such as "error: request of member
    << l1 >> in something that's not a structure or an union". What's wrong
    with the way I do it and how should I fix it?
     
    Michel Rouzic, Nov 20, 2005
    #1
    1. Advertising

  2. Michel Rouzic

    Flash Gordon Guest

    Michel Rouzic wrote:
    > It's the first time I try using structs, and I'm getting confused with
    > it and can't make it work properly
    >
    > I firstly define the structure by this : typedef struct { char *l1; int
    > *l2; int Nval; } *arrays;

    ^^^
    The * says pointer, why put it there?

    Also, l1 and l2 are pore choices for names, since they look too much
    like 11 and 12.

    > It's supposed to be a structure containing an array of chars, an array
    > of ints and an int.


    Well, what you have specified is pointer to char and pointer to int. I
    suggest you reread what your text books say about arrays, pointers and
    declarations of them.

    > I declare functions like this : arrays *parseline(char *line, int N)
    >
    > and I point to the arrays I want to return like this :
    >
    > out.l1=displayln;
    > out.l2=sumline;
    > out.Nval=Nval;
    > return out;
    >
    > When compiling it gives me an error, such as "error: request of member
    > << l1 >> in something that's not a structure or an union". What's wrong
    > with the way I do it and how should I fix it?


    Quick answer, remove the extraneous *. Although I'll bet that leaves you
    with lots of other problems. See my comments above.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Nov 20, 2005
    #2
    1. Advertising

  3. Michel Rouzic

    pete Guest

    Michel Rouzic wrote:
    >
    > It's the first time I try using structs,
    > and I'm getting confused with
    > it and can't make it work properly
    >
    > I firstly define the structure by this :
    > typedef struct { char *l1; int *l2; int Nval; } *arrays;
    >
    > It's supposed to be a structure containing an array of chars,
    > an array of ints and an int.


    > I declare functions like this : arrays *parseline(char *line, int N)


    > out.l1=displayln;
    > out.l2=sumline;
    > out.Nval=Nval;
    > return out;


    > and how should I fix it?


    struct arrays {
    char c_array[l1];
    int i_array[l2];
    int Nval;
    };

    #include <string.h>

    struct arrays out;

    memcpy(out.c_array, displayln, sizeof out.c_array);
    memcpy(out.i_array, sumline, sizeof out.i_array);
    out.Nval = Nval;
    return out;


    .... or sizeof the source object, whichever is smaller.
    If displayln is the name of a smaller array then

    memcpy(out.c_array, displayln, sizeof displayln);

    If displayln is the name of a pointer
    to the first element of a smaller array
    then you have figure something out.
    Likewise for out.i_array.

    --
    pete
     
    pete, Nov 20, 2005
    #3
  4. "Michel Rouzic" <> writes:
    > It's the first time I try using structs, and I'm getting confused with
    > it and can't make it work properly
    >
    > I firstly define the structure by this : typedef struct { char *l1; int
    > *l2; int Nval; } *arrays;


    Which is much more legible like this:

    typedef struct {
    char *l1;
    int *l2;
    int Nval;
    } *arrays;

    > It's supposed to be a structure containing an array of chars, an array
    > of ints and an int.


    Your structure type doesn't *contain* an array of anything. It
    contains two pointers and an integer. The pointers might point to
    arrays if you initialize them to point to allocated memory.

    The typedef "arrays" doesn't name a structure type; it names a
    pointer-to-structure type. You haven't provided a name for the
    structure type.

    You should consider dropping the typedef altogether and just using
    the structure tag directly, something like:

    struct arrays {
    char *l1;
    int *l2;
    int Nval;
    };

    You would then refer to the type as "struct arrays" rather than
    "arrays". The typedef saves you the effort of typing "struct", but
    that's really not much of a benefit; your code is clearer if it's
    obvious that the type is a structure. (There are rare cases where you
    might want to hide the nature of the type; this isn't likely to be one
    of them.)

    You should also choose better names. Flash Gordon has already
    mentioned that l1 and l2 are easily confused with 11 and 12. The name
    "arrays" could also be improved. Using a plural name for a single
    object causes confusion; would an array of them be called "arrayses"?
    And it's a structure, not an array. Pick a name that reflects what
    it's used for, perhaps "struct line" or "struct line_info".

    > I declare functions like this : arrays *parseline(char *line, int N)


    Given your original declaration, that would return a
    pointer-to-pointer-to-structure. You seem to be throwing in '*'s
    almost at random. Don't make something a pointer unless you have a
    specific reason. (In fact, don't do *anything* unless you have a
    specific reason.)

    You can just return a structure directly:
    arrays parseline(char *line, int N);
    or
    struct line_info parse_line(char *line, int n);
    All-caps names are usually used for macros. Underscores are usually
    used to separate words in identifiers; "parseline" is too easy to read
    as a nonexistent 3-syllable word.

    > and I point to the arrays I want to return like this :
    >
    > out.l1=displayln;
    > out.l2=sumline;
    > out.Nval=Nval;
    > return out;


    struct line_info parse_line(char *line, int n)
    {
    struct line_info result;
    ...
    result.l1 = something;
    result.l2 = something_else;
    result.Nval = something_else_again;
    ...
    return result;
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Nov 20, 2005
    #4
    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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    642
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    561
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    645
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,408
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    478
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page