Pointer Problem

S

Sikandar

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
 
I

Ian Collins

Sikandar said:
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.
 
T

Thomas J. Gritzan

Christoph said:
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;

/* ... */
}
 
C

Christoph Schweers

Thomas said:
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
 
I

Ian Collins

Racaille said:
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.
 
B

Barry Schwarz

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
 
R

Racaille

Barry said:
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.
 
B

Barry Schwarz

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
 
K

Keith Thompson

Barry Schwarz said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top