473,414 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

using pointers and handles

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.

Dec 11 '06 #1
3 1605
sajohn wrote:
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 <stdlib.hsomewhere ?
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.

Dec 11 '06 #2
"Spiros Bousbouras" <sp****@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Dec 11 '06 #3
"sajohn" <sa******@yahoo.comwrote in message
news:11**********************@73g2000cwn.googlegro ups.com...
<snip>
unsigned long HANDLE ;
<snip>
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/

Dec 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: enki | last post by:
I have been reading Ruminations on C++, a very interesting book. They are going into handles. I think what they are doing is very intersting and very confusing. I have to read each paragraph...
3
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object...
2
by: IcedCrow | last post by:
I am a little bit confused so am proposing this question. Thanks in advance to anyone who can answer it for me. I was told many times that one of the things that .NET got rid of was pointers. ...
7
by: Rich Milburn [MVP] | last post by:
Ok I am not a programmer, I copied some code and very painfully got it working in VB6. I can adjust the volume with waveOutSetVolume on winmm.dll. But I could not seem to be able to figure out how...
20
by: Joe Van Dyk | last post by:
Is there some rule of thumb about when to use pointers to an object and when to use a reference* to an object when a class needs to have objects as data members? Example: class A { B* b_ptr;...
4
by: =?Utf-8?B?QmlsbCBTcGFobg==?= | last post by:
I have an abstract base class defined in an MC++ class library. I then create a descendent class in a C# class library. This seems to work OK with the exception of one of the abstract member...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
11
by: memeticvirus | last post by:
I have an array cli::array<float, 2and I would like to access a subset of it's values by compiling an array of pointers. But, it's not possible to create an array of type...
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.