Re: struct pointer problem

Discussion in 'C Programming' started by Chris Dollin, Aug 22, 2003.

  1. Chris Dollin

    Chris Dollin Guest

    Tonci Nemes wrote:

    > Hi, all
    >
    > I have a struct and pointers problem I can't figure out how to get around.
    >
    > A struct is for example declared as such:
    >
    > typedef struct{
    > float value1;
    > float *array1;
    > float *array2;
    > } *mystructure
    >
    > Note the " *mystructure ". It has to be there and this confuses me a bit.
    > Does this mean that "mystructure" is actually a pointer to the struct?


    Sort of. It means that `mystructure` means `pointer to struct (whatever)`.
    `mystructure` is a type name. Variables of type `mystructure` are pointers.

    [The * doesn't "have" to be there. If you leave it out, `mystructure` is
    a type which isn't a pointer.]

    > Now there's a function that initializes the values and this is where I hit
    > into problems:
    >
    > mystructure *InitStructure(void){
    > ...
    > }
    >
    > What exactly does this function return, then? A pointer to a variable of
    > type mystructure?


    A pointer to a pointer to a struct, or a pointer to a thing of type
    mystructure, which means the same thing.

    > Then, inside that InitStructure() function, I want to allocate space for
    > the structure and the two arrays (array1 and array2). This is where I hit
    > a brick wall - malloc(sizeof(mystructure)) will allocate space just for
    > the pointer.


    Well, don't do that then. You probably don't need one of the *s.

    > mystructure *InitStructure(void){


    [`Init` is better named `Create`, I think; I'd use `Init` for something
    that took an object that needed Init'ing, not something that made a new
    one.]

    > mystructure *instanced;
    > instanced = malloc(sizeof(mystructure));
    >
    > (*instanced)->value1 = 10;
    > ...
    > return instanced;
    > }


    If you *must* return a pointer to a pointer to a struct thingy,

    mystructure *instanced = malloc( sizeof (*instanced) );
    *instanced = malloc (sizeof (**instanced) );
    (*instanced)->value1 = 10;

    Otherwise strip a * somewhere, give the struct a tag

    struct myStruct ...

    and use

    struct myStruct *result = malloc( sizeof (*result) );
    result->value1 = 10;
    return result;

    --
    Chris "electric hedgehog" Dollin
    C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
    C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html
     
    Chris Dollin, Aug 22, 2003
    #1
    1. Advertising

  2. Chris Dollin

    Tonci Nemes Guest

    Mr. Bruce and Mr. Dollin, thank you kindly for your quick replies, I will
    try your suggestions ASAP.

    As for the *, it technically doesn't have to be there, that's true. However,
    I am building up on source code that already had such types (and some
    procedures that use them - nothing related to this thread, though) defined.
    What I meant was "the * was specified in the source code and I am reluctant
    to change it". I apologize for the misunderstanding.

    Regards,

    Tonci


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.512 / Virus Database: 309 - Release Date: 19.8.2003
     
    Tonci Nemes, Aug 22, 2003
    #2
    1. Advertising

  3. Chris Dollin

    Default User Guest

    Tonci Nemes wrote:

    > As for the *, it technically doesn't have to be there, that's true. However,
    > I am building up on source code that already had such types (and some
    > procedures that use them - nothing related to this thread, though) defined.
    > What I meant was "the * was specified in the source code and I am reluctant
    > to change it". I apologize for the misunderstanding.




    If you are going to hide pointers behind typedefs (which I DO NOT
    recommend) you should make sure the resulting type reflects that in its
    name. So rather than mystructure, you should call it mystructureptr or
    something similar. You can see already the confusion this typedef has
    caused for you, other users of the code may have the same problem.



    Brian Rodenborn
     
    Default User, Aug 22, 2003
    #3
  4. Chris Dollin

    Tonci Nemes Guest

    Duly noted. The code in the post was an example, but your point is well
    made.

    Regards,

    Tonci

    >
    >
    > If you are going to hide pointers behind typedefs (which I DO NOT
    > recommend) you should make sure the resulting type reflects that in its
    > name. So rather than mystructure, you should call it mystructureptr or
    > something similar. You can see already the confusion this typedef has
    > caused for you, other users of the code may have the same problem.
    >
    >
    >
    > Brian Rodenborn



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.512 / Virus Database: 309 - Release Date: 20.8.2003
     
    Tonci Nemes, Aug 23, 2003
    #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. Chris Fogelklou
    Replies:
    36
    Views:
    1,393
    Chris Fogelklou
    Apr 20, 2004
  2. Fernan Bolando

    Pointer to struct or Struct parameters

    Fernan Bolando, Aug 7, 2004, in forum: C Programming
    Replies:
    6
    Views:
    384
    Fernan Bolando
    Aug 8, 2004
  3. beetle
    Replies:
    2
    Views:
    916
    beetle
    Jan 25, 2005
  4. Zero
    Replies:
    16
    Views:
    662
    Barry Schwarz
    Nov 19, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    474
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page