Results 1 to 3 of 3

Thread: using a pointer to a structure..

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I'm using an API function that returns a pointer to a structure.

    How can I access this pointer to a structure?

    I'm trying to use the following:

    int pHost;

    pHost = gethostbyname("blablabla");

    But it doesn't seem to like it. I get the following error when compiling:

    error C2440: '=' : cannot convert from 'struct hostent *' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast


    Can someone help me?

    Visual Studio 2010

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    hostent *pHost;
    
    pHost = gethostbyname("blahblah");
    pHost->data_member (or whatever) gets you the data. Incidentally, these are equivalent:
    Code:
    struct me {
        int A;
        int B;
    }
    
    me *one;
    int temp;
    
    temp = one->A;
    temp = (*one).B;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    If you want to be technical:

    . is the direct member access operator
    -> is the indirect member access operator

    It's useful when you are using a heirarchy of classes/structures with pointers to other classes/structures. -> can simplify things a lot:

    (*(*object1).object2).object3

    can be written as:

    object1->object2->object3

    Far less confusing
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width