using pointers and handles

S

sajohn

I'm fairly new to c programming and am not very clear on pointers and
handles. Currently, I'm writting a wrapper script and I need to
interface with another application. I have decided that the wrapper
script could use some data structures and have defined a few. The
problem is that the application has defined some pointers that I could
use for my structures that are defined as unsigned long. I thought I
could write some code as follows

unsigned long HANDLE ;
unsigned long *PHANDLE ;

typedef struct
{
int i ;
float f ;
} Numbers ;

HANDLE hTest1 ;
PHANDLE *phTest2;
Numbers Num ;

hTest2 = (PHANDLE )malloc( HANDLE * ) ;
hTest1 = ( HANDLE )malloc( sizeof( Num ) ) ;

hTest2->i = 10 ;
hTest2->f = 127/13 ;

This code fails with an error indicating that I'm pointing to an hTest2
structure instead of a Num structure and cannot find i or f fields.

Then I thought I could something like this

hTest2->hTest1->i

to retrive the filed data but the results are the same.

I know a lot of this is my lack of understanding of pointers in general
but I was wondering if someone could tell me where I'm going wrong with
this code. Is it even possible to malloc a data structure in memory and
then re-cast its pointer type to an unsigned long?

TIA.
 
S

Spiros Bousbouras

sajohn said:
I'm fairly new to c programming and am not very clear on pointers and
handles.

I'm not sure what you mean by handle. There's no
such thing in standard C terminology.
Currently, I'm writting a wrapper script and I need to
interface with another application. I have decided that the wrapper
script could use some data structures and have defined a few. The
problem is that the application has defined some pointers that I could
use for my structures that are defined as unsigned long. I thought I
could write some code as follows

unsigned long HANDLE ;
unsigned long *PHANDLE ;

Based on some of the code below I suspect that
the above 2 lines were meant to start with typedef.
typedef struct
{
int i ;
float f ;
} Numbers ;

HANDLE hTest1 ;
PHANDLE *phTest2;

You don't get an error for the last 2 lines above ?
You should. You are declaring hTest1 as having
type HANDLE but there is no such type in the
language nor have you defined one yourself.
Numbers Num ;

hTest2 = (PHANDLE )malloc( HANDLE * ) ;

malloc() accepts an argument of type size_t.
You are giving it a pointer to a type HANDLE
which as I've said above you haven't defined.
Even if your intention was for HANDLE to mean
unsigned long the above line is still wrong and
the compiler should give you an error. Does your
code contain #include said:
hTest1 = ( HANDLE )malloc( sizeof( Num ) ) ;

hTest2->i = 10 ;

The notation -> is used when a pointer points to
a structure. hTest2 is not a pointer to a structure.
In fact it's not any sort of object since you have
not given a legal definition for it anywhere. As an
example if you had given the declaration
Numbers *p ;
then you could write
p->i = 5 ; p->f = 3.1 ;
hTest2->f = 127/13 ;

This code fails with an error indicating that I'm pointing to an hTest2
structure instead of a Num structure and cannot find i or f fields.

You should have received a lot more errors than that.
Then I thought I could something like this

hTest2->hTest1->i

to retrive the filed data but the results are the same.

I know a lot of this is my lack of understanding of pointers in general
but I was wondering if someone could tell me where I'm going wrong with
this code.

I hope I managed to be of some help but your
misunderstandings are so numerous I'm driven to despair.
Is it even possible to malloc a data structure in memory and
then re-cast its pointer type to an unsigned long?

Strictly speaking you don't malloc a data structure you
malloc memory for a data structure (or something else).
As for recasting its pointer type to unsigned long it might
work depending on your implementation but if you think
it's something you need to do then almost certainly you
are going wrong somewhere.
 
K

Keith Thompson

Spiros Bousbouras said:
sajohn wrote: [...]
hTest2->f = 127/13 ;

This code fails with an error indicating that I'm pointing to an hTest2
structure instead of a Num structure and cannot find i or f fields.

You should have received a lot more errors than that.
[...]

Specifically, you should have received a lot more errors *if* you had
attempted to compile the code you posted. It's clear that the code
you posted does not match the code you're actually trying to compile.

Posting a rough approximation of your actual code is generally a
wasted of everyone's time. Post your *actual* code. Don't try to
re-type it; copy-and-paste it. If your code is too long to post, trim
it.
 
S

Sourcerer

unsigned long HANDLE ;
HANDLE hTest1 ;
<snip>

The second line would only be valid if the first was

typedef unsigned long HANDLE;

However, your code is wrong through and through.
For example,
hTest2 = (PHANDLE )malloc( HANDLE * ) ;
has no meaning. What are you trying to do? To allocate memory for hTest2 when it
is of type PHANDLE, you need
hTest2 = (PHANDLE *) malloc(sizeof(PHANDLE));
But then you can't do
hTest2->i = 10;
because hTest2 is of type PHANDLE, and not of type Numbers.

To be able to do
hTest2->i = 10;
you need to redefine hTest2 to be a pointer to a structure of type Numbers.
Redefined, this would go like so:

Numbers *hTest2;
....
int main(void) {
....
hTest2 = (Numbers *) malloc(sizeof(Numbers));
hTest2->i = 10;
....
return 0;
}

If you want to do
hTest2->hTest1->i = whatever;
you need to define hTest1 also as a pointer to a structure of type Numbers. But
then the structure must become a linked list, therefore it must be changed to
this at least:

typedef struct Num
{
int i ;
float f ;
struct Num *hTest1;
} Numbers ;

in main() function, you then need to do the following:

int main(void) {
Numbers *listBegin;
...
listBegin = hTest2 = (Numbers *) malloc(sizeof(Numbers));
hTest2->hTest1 = (Numbers *) malloc(sizeof(Numbers));
hTest2->hTest1->hTest1 = NULL; // signifies the end of the list
// and then you can do what you wanted
hTest2->hTest1->i = 5;
...
return 0;
}

--
"It is easy in the world to live after the world's oppinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/
 

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

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top