Velocity Reviews > Pointer Problem

# Pointer Problem

Sikandar
Guest
Posts: n/a

 08-12-2006
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

Ian Collins
Guest
Posts: n/a

 08-12-2006
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.

Racaille
Guest
Posts: n/a

 08-12-2006
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.

Christoph Schweers
Guest
Posts: n/a

 08-12-2006
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

Thomas J. Gritzan
Guest
Posts: n/a

 08-12-2006
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

Christoph Schweers
Guest
Posts: n/a

 08-12-2006
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

--
The idea is to die young as late as possible.
-- Ashley Montagu

Ian Collins
Guest
Posts: n/a

 08-12-2006
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.

av
Guest
Posts: n/a

 08-13-2006
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

Barry Schwarz
Guest
Posts: n/a

 08-13-2006
On Sun, 13 Aug 2006 11:15:31 +0200, av <(E-Mail Removed)> 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

Racaille
Guest
Posts: n/a

 08-14-2006
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.