• Subject: RE: Examples/Pointer useage
  • From: "Bob Crothers" <bob@xxxxxxxxxxxxxx>
  • Date: Thu, 15 Jun 2000 15:51:12 -0500
  • Importance: Normal

Greg,

For better or worse, pointers are a fact of life in C/C++.    The good part
is they give you a lot of power.

Below are some examples of getting to a structure using pointers.  Keep in
mind most of my work is in C++ so some of the syntax will be slightly wrong
when dealing with structures.  But you should get the idea.

Also, notice I initialize my pointers to NULL.  This way, if I use them
before initializing them, they point to nothing and the OS will slap me.
But if you do not do this, they will have garbage.  If you reference garbage
in a pointer, then you might get unlucky and be pointing to valid memory.
This is how corruptions happen.


struct MyStruct
        {
        int             myInt;
        char    myString[32];
        };

unsigned char   *baseptr = NULL;        /* ALWAYS initialize to NULL!!! */
struct MyStruct *structptr = NULL;


/* Do something to get your baseptr */

baseptr = GetMyPointer();

/* now get your offset */

int offset = 67;

/* now pull the structure out */

structptr = (struct MyStruct *)baseptr[offset];

/* now just use the pointer */

printf("Int value was %i, string was %s\n",
                        structptr->myInt,
                        structptr->myString);

/* Or maybe there are 10 structures in your buffer */

for (int x = 0; x < 10; x++)
        {
        structptr = (struct MyStruct *)baseptr[x * sizeof(*structptr)];
        printf("Structure %i Int value was %i, string was %s\n",
                                x,
                                structptr->myInt,
                                structptr->myString);
        }

Regards,
Bob Crothers
Cornerstone Communications
Business: www.theunifier.com and www.faxserver401.com
Personal:  www.cstoneindy.com/bob (Don't go here if you aren't a dog
person!)
Email: Bob@CStoneIndy.com
Voice: 317-802-0107 Ext 103
Fax:    317-803-3450

If a messy desk is a sign of a messy mind,
What does an empty desk say?



-----Original Message-----
From: owner-c400-l@midrange.com [mailto:owner-c400-l@midrange.com]On Behalf
Of Greg Leibfried
Sent: Thursday, June 15, 2000 2:26 PM
To: C400-L@midrange.com
Subject: Re: Examples

Thanks for the reply Bob,

I took your suggestion and changed my pointer declares to unsigned.

My biggest problem I had to overcome was learning how to manipulate the
pointerrs, structures, & offsets to the list data within the user
space.  I found some structure templates in the system includes that
helped me setup the structures so that I had a pointer the list data
returned by the api.  Now, I have the little routine working and have
added it to the program I needed it in.

Thanks again,

...gregl

Bob Crothers wrote:
>
> Greg,
>
> First, I'd recommend you use the following declaration for your space
> pointer:
>
> unsigned char *mySpacePtr;
>
+---
| This is the C/400 Mailing List!
| To submit a new message, send your mail to C400-L@midrange.com.
| To subscribe to this list send email to C400-L-SUB@midrange.com.
| To unsubscribe from this list send email to C400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator:
bob@cstoneindy.com
+---

+---
| This is the C/400 Mailing List!
| To submit a new message, send your mail to C400-L@midrange.com.
| To subscribe to this list send email to C400-L-SUB@midrange.com.
| To unsubscribe from this list send email to C400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: bob@cstoneindy.com
+---

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.