beginner: structures...

Discussion in 'C Programming' started by rory, Oct 14, 2005.

  1. rory

    rory Guest

    Cna anyone point me in the right direction, I have a struture in my .h
    file:

    typedef struct mdata
    {
    int names[500];
    int dates[500];
    int ages[500];
    }m_Data;

    and in my .c file I declare a pointer to this strucutre and put aside
    memory to hold it:

    m_Data* firstlist;
    firstlist = (m_Data*)malloc(sizeof(m_Data));

    What I'm wondering about it is if there is a way that I can change each
    of the elements from being a fixed size of 500 to a being user defined
    size? I'm guessing I can make the elements pointers but do I use malloc
    on each of the elemnnts, and if so isn't that changing the size of the
    struture in which I have already put aside an initial memory of
    'malloc(sizeof(m_data))'. As you can see I'm a little confused, any
    ideas?

    regards,
    Rory.
     
    rory, Oct 14, 2005
    #1
    1. Advertising

  2. rory

    Ravi Uday Guest

    Given your orig. structure below there is no way to change the
    memory already
    allocated to the structural elements. Its fixed at '500'

    If you want to change the mem. allocated to the structure elements at
    runtime you need to define the original structural elements
    as pointers first .
    like -

    typedef struct mdata
    {
    int *names;
    int *dates;
    int *ages;
    }m_Data;

    then in your program you can allocate memory (yes, using malloc) to
    those elements depending on its usage scenario.
    Ex: my_Data->name = malloc (sizeof (m_Data->name));
    [given my_Data is declared as a pointer to m_Data.]

    Get a C-book and read the section on pointers !

    - Ravi



    > Cna anyone point me in the right direction, I have a struture in my .h
    > file:
    >
    > typedef struct mdata
    > {
    > int names[500];
    > int dates[500];
    > int ages[500];
    > }m_Data;
    >
    > and in my .c file I declare a pointer to this strucutre and put aside
    > memory to hold it:
    >
    > m_Data* firstlist;
    > firstlist = (m_Data*)malloc(sizeof(m_Data));
    >
    > What I'm wondering about it is if there is a way that I can change each
    > of the elements from being a fixed size of 500 to a being user defined
    > size? I'm guessing I can make the elements pointers but do I use malloc
    > on each of the elemnnts, and if so isn't that changing the size of the
    > struture in which I have already put aside an initial memory of
    > 'malloc(sizeof(m_data))'. As you can see I'm a little confused, any
    > ideas?
    >
    > regards,
    > Rory.
    >
     
    Ravi Uday, Oct 14, 2005
    #2
    1. Advertising

  3. rory

    ajm Guest

    Hi Rory,

    If you want variable sized members within your structure then you have
    no choice but to use pointers (i.e., malloc and co.) so you have:

    typedef struct mdata
    {
    int *names;
    int *dates;
    int *ages;
    } m_Data;

    it might be good to define a function to dimension your structure,
    e.g., with prototype

    m_Data * mdata_init(size_t names_sz, size_t dates_sz, size_t ages_sz);

    which uses the *_sz arguments to perform the necessary mallocs and
    return the
    pointer to the caller. you still use the (m_Data *)malloc call before
    returning the pointer
    to the caller since this is accounts for the memory cost of holding
    your int pointers and
    so can be determined consistently.

    you might also want to write a mdata_destroy to perform the necessary
    frees too.

    if in doubt just write a couple of short programs to try out the ideas
    and post any
    queries you still have.

    hth,
    ajm.
     
    ajm, Oct 14, 2005
    #3
  4. rory

    pete Guest

    rory wrote:
    >
    > Cna anyone point me in the right direction, I have a struture in my .h
    > file:
    >
    > typedef struct mdata
    > {
    > int names[500];
    > int dates[500];
    > int ages[500];
    > }m_Data;
    >
    > and in my .c file I declare a pointer to this strucutre and put aside
    > memory to hold it:
    >
    > m_Data* firstlist;
    > firstlist = (m_Data*)malloc(sizeof(m_Data));


    #include <stdlib.h>
    firstlist = malloc(sizeof *firstlist);

    The reference to m_Data is not needed with the malloc call.
    The cast can hide your failure to #include <stdlib.h>


    > What I'm wondering about it is
    > if there is a way that I can change each
    > of the elements from being a fixed size of 500
    > to a being user defined size?


    Yes.

    > I'm guessing I can make the elements pointers but do I use malloc
    > on each of the elemnnts,


    Yes.

    > and if so isn't that changing the size of the
    > struture in which I have already put aside an initial memory of
    > 'malloc(sizeof(m_data))'.


    No.
    The structure will only hold three pointers.

    > As you can see I'm a little confused, any
    > ideas?


    Use pointers like you were thinking.

    --
    pete
     
    pete, Oct 14, 2005
    #4
  5. rory

    rory Guest

    Cheers, I am now defining the original structure as you suggested with
    pointers, and I try to allocate the space for each like this

    m_Data* list1;
    list1 = (m_Data*)malloc(sizeof(m_Data));
    list1->note = (m_Data->names*)malloc(sizeof(m_Data->names)*50);
    list1->channel = (m_Data->dates*)malloc(sizeof(m_Data->dates)*50);
    list1->duration = (m_Data->ages*)malloc(sizeof(m_Data->ages)*50);

    however I get errors on the lines that I try to allocate memory to the
    elements on:

    example.c:23: parse error before '->' token
    example.c:24: parse error before '->' token
    example.c:25: parse error before '->' token

    I hope it isn't a simple syntax error, if so I apologise in advance!
     
    rory, Oct 14, 2005
    #5
  6. rory

    ajm Guest

    you don't need the m_Data-> etc. in the sizeof() calls and you can
    replace the cast with (int *) since your "cast" is not a type.

    Ravi's comment (jibe ;) to read the pointers section of a C book is not
    bad advice...

    hth
    ajm.
     
    ajm, Oct 14, 2005
    #6
  7. rory

    pete Guest

    rory wrote:
    >
    > Cheers, I am now defining the original structure as you suggested with
    > pointers, and I try to allocate the space for each like this
    >
    > m_Data* list1;
    > list1 = (m_Data*)malloc(sizeof(m_Data));
    > list1->note = (m_Data->names*)malloc(sizeof(m_Data->names)*50);
    > list1->channel = (m_Data->dates*)malloc(sizeof(m_Data->dates)*50);
    > list1->duration = (m_Data->ages*)malloc(sizeof(m_Data->ages)*50);


    list1 = malloc(sizeof *list1);
    list1 -> note = malloc(50 * sizeof *list1 -> note);
    list1 -> channel = malloc(50 * sizeof *list1 -> channel);
    list1 -> duration = malloc(50 * sizeof *list1 -> duration);

    --
    pete
     
    pete, Oct 14, 2005
    #7
  8. In article <>,
    rory <> wrote:
    >Cheers, I am now defining the original structure as you suggested with
    >pointers, and I try to allocate the space for each like this


    > m_Data* list1;
    > list1 = (m_Data*)malloc(sizeof(m_Data));
    > list1->note = (m_Data->names*)malloc(sizeof(m_Data->names)*50);
    > list1->channel = (m_Data->dates*)malloc(sizeof(m_Data->dates)*50);
    > list1->duration = (m_Data->ages*)malloc(sizeof(m_Data->ages)*50);


    >however I get errors on the lines that I try to allocate memory to the
    >elements on:


    >example.c:23: parse error before '->' token


    The parse error is in the second use of -> on each line.
    m_Data is a structure type, not a pointer (not even a pointer
    to a structure type -- it is list1 which is the pointer to a structure),
    so m_Data->names is not valid.

    You also appear to be doing something funky with what you are
    trying to take the size of. The left hand side of your assignments
    implies that list1 is a pointer to a structure which has elements
    'note', 'channel', and 'duration', but the sizeof() part of
    your expression implies that the structure has elements named
    'names', 'dates', and 'ages' instead. Are you trying to imply
    that 'note' is an array of structures of type 'names' ??

    --
    Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
     
    Walter Roberson, Oct 14, 2005
    #8
  9. rory

    rory Guest

    Cheers guys that's sorted it, I'm off now to the library to get a few
    pointers on C programming (pun fully intended!).

    Rory.
     
    rory, Oct 14, 2005
    #9
  10. rory

    Alex Fraser Guest

    "rory" <> wrote in message
    news:...
    > Cna anyone point me in the right direction, I have a struture in my .h
    > file:
    >
    > typedef struct mdata
    > {
    > int names[500];
    > int dates[500];
    > int ages[500];
    > }m_Data;
    >
    > and in my .c file I declare a pointer to this strucutre and put aside
    > memory to hold it:
    >
    > m_Data* firstlist;
    > firstlist = (m_Data*)malloc(sizeof(m_Data));


    Firstly, the cast here is unnecessary. Unnecessary casts can mask errors; in
    this case, forgetting to #include <stdlib.h>. If you forget, the compiler
    assumes malloc() returns an int. The result is undefined behaviour, but the
    cast typically avoids any warning.

    Secondly, it makes more sense to specify the size of the object rather than
    the object's type. That way, if the type changes you don't need to change
    the call to malloc().

    With these changes, the above is written:

    firstlist = malloc(sizeof *firstlist);

    > What I'm wondering about it is if there is a way that I can change each
    > of the elements from being a fixed size of 500 to a being user defined
    > size?


    Sure.

    > I'm guessing I can make the elements pointers but do I use malloc on each
    > of the elemnnts,


    Yes; make the members pointers and allocate space for them with malloc(). It
    is often useful to add members to the struct to hold the size of each
    allocated array, eg:

    typedef struct mdata {
    int *names;
    size_t names_size;
    int *dates;
    size_t dates_size;
    int *ages;
    size_t ages_size;
    } m_Data;

    If all the sizes are the same, you could have just a single size member, or
    perhaps an array of structs would make more sense.

    > and if so isn't that changing the size of the struture in which I have
    > already put aside an initial memory of 'malloc(sizeof (m_data))'.


    It increases the amount of memory "associated with" the structure, but
    sizeof(m_Data) is just the memory required to store the members, and doesn't
    include the memory that any pointer members point to.

    Alex
     
    Alex Fraser, Oct 14, 2005
    #10
  11. rory

    Default User Guest

    rory wrote:

    > Cheers, I am now defining the original structure as you suggested with
    > pointers, and I try to allocate the space for each like this



    Please read my sig below.


    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Oct 14, 2005
    #11
  12. rory

    Default User Guest

    ajm wrote:

    > you don't need the m_Data-> etc. in the sizeof() calls and you can
    > replace the cast with (int *) since your "cast" is not a type.



    Please read my sig below.


    Brian
    --
    Please quote enough of the previous message for context. To do so from
    Google, click "show options" and use the Reply shown in the expanded
    header.
     
    Default User, Oct 14, 2005
    #12
    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. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    634
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    1,022
    Mabden
    Sep 2, 2004
  3. tweak
    Replies:
    14
    Views:
    2,813
    Eric Sosman
    Jun 11, 2004
  4. Alfonso Morra
    Replies:
    11
    Views:
    740
    Emmanuel Delahaye
    Sep 24, 2005
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    245
    7stud --
    Apr 12, 2011
Loading...

Share This Page