Thread: Help with pointers please.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    A quick run-down on what a pointer is:

    A pointer is just a special type of numeric variable, designed to store a memory address. So you can manipulate the pointer itself (the memory address stored), and you can also manipulate the contents of that memory address, be "dereferencing" the pointer.

    Take a look at this thread for some ideas on what a pointer is and how it works:
    changing variables with pointers
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  2. #2
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by Cat View Post
    A quick run-down on what a pointer is:

    A pointer is just a special type of numeric variable, designed to store a memory address. So you can manipulate the pointer itself (the memory address stored), and you can also manipulate the contents of that memory address, be "dereferencing" the pointer.

    Take a look at this thread for some ideas on what a pointer is and how it works:
    changing variables with pointers
    Thank you! When I wake up a little more, I'll read the whole thing. I majored in Computer Science but I went into the Marine Corps and had some damage to the head while I was in there. My memory gets a little messed up sometimes. It seems to take extra time to memorize new stuff and the old stuff seems a little blurry. If I can get a good fundamental understanding of all these pointers, I think I'll be good.

    I have trouble with this whole dereferencing thing. Like when do you dereference a pointer? When don't you? If you notice, in my code, with the if statement, I have to a * in front of ports. My understanding (from what I remember from college), this means compare what's stored at wherever port points to to NULL and if they're equal, print 0 com ports found. If I didn't have the * in front of it, I'd just be comparing NULL to a memory address or something.

    I'm sure the link you sent will correct all my misinformation and reinforce all the information I once learned. Thanks!

  3. #3
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by Spork Schivago View Post
    ...

    I have trouble with this whole dereferencing thing. Like when do you dereference a pointer? When don't you? If you notice, in my code, with the if statement, I have to a * in front of ports. My understanding (from what I remember from college), this means compare what's stored at wherever port points to to NULL and if they're equal, print 0 com ports found. If I didn't have the * in front of it, I'd just be comparing NULL to a memory address or something.
    ...
    Actually, according to the documentation of the function sp_list_ports(), the ports variable should be defined of type: struct sp_port **

    It's my understanding that the function dynamically creates and fills in ports, as a NULL terminated array of pointers to sp_port structures, hence the need to pass the address of ports to the function (that is, a triple pointer). If something goes wrong, the function sets ports to NULL instead.

    So, after calling sp_list_ports( &ports ) you should check ports (the name of the dynamically created array) against NULL.

    *ports is the 1st element of the dynamically created array. That element is a pointer to a sp_port structure, which may be NULL I guess if no ports were found, but most probably it won't even exist/been allocated if no ports were found... it depends on how sp_list_ports() works internally.

    PS. Here's a fairly decent free resource on pointers. Regarding NULL, you may also want to take a look at this thread.
    "Talk is cheap, show me the code" - Linus Torvalds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 01:15 PM
  2. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  3. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  4. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM