Pointer Problem

Discussion in 'C Programming' started by Sikandar, Aug 12, 2006.

  1. Sikandar

    Sikandar Guest

    typedef struct smt_inst
    {
    u_int32 uUnitNumber;
    } SMT_INST;


    typedef struct smt_inst *SMT_HANDLE;


    static TESTH_STATUS getno ( SMT_HANDLE *Handle)
    {


    *Handle->uUnitNumber =1; //This is line giving error.


    }

    I want to assign a value to uUnitNumber of SMT_HANDLE structure .
    How do I do it ?


    Regards in anticipation of your replies,

    Sikandar
     
    Sikandar, Aug 12, 2006
    #1
    1. Advertising

  2. Sikandar

    Ian Collins Guest

    Sikandar wrote:
    > typedef struct smt_inst
    > {
    > u_int32 uUnitNumber;


    Why not use the standard uint32_t?

    > } SMT_INST;
    >
    >
    > typedef struct smt_inst *SMT_HANDLE;
    >
    >
    > static TESTH_STATUS getno ( SMT_HANDLE *Handle)
    > {
    >
    >
    > *Handle->uUnitNumber =1; //This is line giving error.
    >

    Should be

    Handle->uUnitNumber =1;

    Handle is a pointer, you don't have to dereference it.

    --
    Ian Collins.
     
    Ian Collins, Aug 12, 2006
    #2
    1. Advertising

  3. Sikandar

    Racaille Guest

    Ian Collins wrote:
    > > typedef struct smt_inst *SMT_HANDLE;
    > >
    > > static TESTH_STATUS getno ( SMT_HANDLE *Handle)
    > > {
    > > *Handle->uUnitNumber =1; //This is line giving error.
    > >

    > Should be
    >
    > Handle->uUnitNumber =1;


    in fact, that should be (*Handle)->uUnitNumber.
     
    Racaille, Aug 12, 2006
    #3
  4. Racaille schrieb:
    > Ian Collins wrote:
    >>> typedef struct smt_inst *SMT_HANDLE;
    >>>
    >>> static TESTH_STATUS getno ( SMT_HANDLE *Handle)
    >>> {
    >>> *Handle->uUnitNumber =1; //This is line giving error.
    >>>

    >> Should be
    >>
    >> Handle->uUnitNumber =1;

    >
    > in fact, that should be (*Handle)->uUnitNumber.


    no, in fact it then should be (*Handle).uUnitNumber ;-)


    --
    The idea is to die young as late as possible.
    -- Ashley Montagu
     
    Christoph Schweers, Aug 12, 2006
    #4
  5. Christoph Schweers schrieb:
    > Racaille schrieb:
    >> Ian Collins wrote:
    >>>> typedef struct smt_inst *SMT_HANDLE;
    >>>>
    >>>> static TESTH_STATUS getno ( SMT_HANDLE *Handle)
    >>>> {
    >>>> *Handle->uUnitNumber =1; //This is line giving error.
    >>>>
    >>> Should be
    >>>
    >>> Handle->uUnitNumber =1;

    >>
    >> in fact, that should be (*Handle)->uUnitNumber.

    >
    > no, in fact it then should be (*Handle).uUnitNumber ;-)


    In fact, Handle is of type (struct smt_inst**) since SMT_HANDLE is (struct
    smt_inst*), so (*Handle)->uUnitNumber is right.

    But the function signature might be wrong, so that it would better be:

    static TESTH_STATUS getno(SMT_HANDLE Handle)
    {
    Handle->uUnitNumber = 1;

    /* ... */
    }

    --
    Thomas
     
    Thomas J. Gritzan, Aug 12, 2006
    #5
  6. Thomas J. Gritzan schrieb:
    > Christoph Schweers schrieb:
    >> Racaille schrieb:
    >>> Ian Collins wrote:
    >>>>> typedef struct smt_inst *SMT_HANDLE;
    >>>>>
    >>>>> static TESTH_STATUS getno ( SMT_HANDLE *Handle)
    >>>>> {
    >>>>> *Handle->uUnitNumber =1; //This is line giving error.
    >>>>>
    >>>> Should be
    >>>>
    >>>> Handle->uUnitNumber =1;
    >>> in fact, that should be (*Handle)->uUnitNumber.

    >> no, in fact it then should be (*Handle).uUnitNumber ;-)

    >
    > In fact, Handle is of type (struct smt_inst**) since SMT_HANDLE is (struct
    > smt_inst*), so (*Handle)->uUnitNumber is right.
    >
    > But the function signature might be wrong, so that it would better be:
    >
    > static TESTH_STATUS getno(SMT_HANDLE Handle)
    > {
    > Handle->uUnitNumber = 1;
    >
    > /* ... */
    > }


    your right, I missed the second indirection :-o


    --
    The idea is to die young as late as possible.
    -- Ashley Montagu
     
    Christoph Schweers, Aug 12, 2006
    #6
  7. Sikandar

    Ian Collins Guest

    Racaille wrote:
    > Ian Collins wrote:
    >
    >>>typedef struct smt_inst *SMT_HANDLE;
    >>>
    >>>static TESTH_STATUS getno ( SMT_HANDLE *Handle)
    >>>{
    >>>*Handle->uUnitNumber =1; //This is line giving error.
    >>>

    >>
    >>Should be
    >>
    >>Handle->uUnitNumber =1;

    >
    >
    > in fact, that should be (*Handle)->uUnitNumber.
    >

    You're correct, I overlooked the extra level of indirection. Just goes
    to show using typedef for pointers isn't a good idea.

    --
    Ian Collins.
     
    Ian Collins, Aug 12, 2006
    #7
  8. Sikandar

    av Guest

    On Sat, 12 Aug 2006 15:19:20 +0200, Christoph Schweers wrote:
    >Racaille schrieb:
    >> Ian Collins wrote:
    >>>> static TESTH_STATUS getno ( SMT_HANDLE *Handle)
    >>>> {
    >>>> *Handle->uUnitNumber =1; //This is line giving error.
    >>>>
    >>> Should be
    >>>
    >>> Handle->uUnitNumber =1;

    >>
    >> in fact, that should be (*Handle)->uUnitNumber.

    >
    >no, in fact it then should be (*Handle).uUnitNumber ;-)


    it seems to me "Handle->uUnitNumber" too
     
    av, Aug 13, 2006
    #8
  9. On Sun, 13 Aug 2006 11:15:31 +0200, av <> wrote:

    >On Sat, 12 Aug 2006 15:19:20 +0200, Christoph Schweers wrote:
    >>Racaille schrieb:
    >>> Ian Collins wrote:
    >>>>> static TESTH_STATUS getno ( SMT_HANDLE *Handle)
    >>>>> {
    >>>>> *Handle->uUnitNumber =1; //This is line giving error.
    >>>>>
    >>>> Should be
    >>>>
    >>>> Handle->uUnitNumber =1;
    >>>
    >>> in fact, that should be (*Handle)->uUnitNumber.

    >>
    >>no, in fact it then should be (*Handle).uUnitNumber ;-)

    >
    >it seems to me "Handle->uUnitNumber" too


    Try again. Handle is a pointer to SMT_HANDLE (SMT_HANDLE*).
    SMT_HANDLE is a pointer to struct (struct*). Therefore Handle is a
    pointer to pointer to struct (struct**). The -> operator requires the
    left operand to be of type pointer to struct (struct*). Since there
    is no implied conversion between struct** and struct*, your code is a
    syntax error.


    Remove del for email
     
    Barry Schwarz, Aug 13, 2006
    #9
  10. Sikandar

    Racaille Guest

    Barry Schwarz wrote:
    > >>no, in fact it then should be (*Handle).uUnitNumber ;-)

    > >
    > >it seems to me "Handle->uUnitNumber" too

    >
    > Try again. Handle is a pointer to SMT_HANDLE (SMT_HANDLE*).
    > SMT_HANDLE is a pointer to struct (struct*). Therefore Handle is a
    > pointer to pointer to struct (struct**). The -> operator requires the
    > left operand to be of type pointer to struct (struct*). Since there
    > is no implied conversion between struct** and struct*, your code is a
    > syntax error.


    I think his point was that the two expressions
    ('(*Handle).uUnitNumber' and 'Handle->uUnitNumber')
    are exactly the same thing.

    That is correct: in C, 'p->q' is just a nicer way to write '(*p).q' and
    nothing more.
     
    Racaille, Aug 14, 2006
    #10
  11. On 14 Aug 2006 06:19:54 -0700, "Racaille" <>
    wrote:

    >Barry Schwarz wrote:
    >> >>no, in fact it then should be (*Handle).uUnitNumber ;-)
    >> >
    >> >it seems to me "Handle->uUnitNumber" too

    >>
    >> Try again. Handle is a pointer to SMT_HANDLE (SMT_HANDLE*).
    >> SMT_HANDLE is a pointer to struct (struct*). Therefore Handle is a
    >> pointer to pointer to struct (struct**). The -> operator requires the
    >> left operand to be of type pointer to struct (struct*). Since there
    >> is no implied conversion between struct** and struct*, your code is a
    >> syntax error.

    >
    >I think his point was that the two expressions
    >('(*Handle).uUnitNumber' and 'Handle->uUnitNumber')
    >are exactly the same thing.
    >
    >That is correct: in C, 'p->q' is just a nicer way to write '(*p).q' and
    >nothing more.


    While this is true it just means that both are a syntax error.
    Especially since he quoted the correct code two lines earlier.


    Remove del for email
     
    Barry Schwarz, Aug 15, 2006
    #11
  12. Barry Schwarz <> writes:
    > On 14 Aug 2006 06:19:54 -0700, "Racaille" <>
    > wrote:
    >>Barry Schwarz wrote:
    >>> >>no, in fact it then should be (*Handle).uUnitNumber ;-)
    >>> >
    >>> >it seems to me "Handle->uUnitNumber" too
    >>>
    >>> Try again. Handle is a pointer to SMT_HANDLE (SMT_HANDLE*).
    >>> SMT_HANDLE is a pointer to struct (struct*). Therefore Handle is a
    >>> pointer to pointer to struct (struct**). The -> operator requires the
    >>> left operand to be of type pointer to struct (struct*). Since there
    >>> is no implied conversion between struct** and struct*, your code is a
    >>> syntax error.

    >>
    >>I think his point was that the two expressions
    >>('(*Handle).uUnitNumber' and 'Handle->uUnitNumber')
    >>are exactly the same thing.
    >>
    >>That is correct: in C, 'p->q' is just a nicer way to write '(*p).q' and
    >>nothing more.

    >
    > While this is true it just means that both are a syntax error.
    > Especially since he quoted the correct code two lines earlier.


    No, it's not a syntax error (at least not as I, and I think most
    people, use the phrase).

    Racaille correctly stated what the "->" operator means.

    A syntax error is a violation of the grammar. Using p->q where p is
    not of an appropriate type is a semantic error, specifically a
    constraint violation. They're both errors, and they both trigger a
    required diagnostic, so it's not a hugely important distinction.

    --
    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, Aug 15, 2006
    #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. Replies:
    10
    Views:
    731
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    861
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,308
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    649
  5. , India

    pointer to an array vs pointer to pointer

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

Share This Page