#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2

    Could someone please explain pointers to me, again?


    I've watched many tutorials and read about pointers and references time and again, but I still have a hard time grasping why they exist, why we use them, and what they actually are. Once I grasp the idea of them, I think I can better proceed with my learning.

    I know how to use them, but I don't understand the concept of them yet, fully. I don't understand how they fit into the bigger picture.

    And character arrays are the same as character pointers? wtf?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    http://pw1.netcom.com/~tjensen/ptr/pointers.htm

    I suggest you read it, then ask some specific questions if you're still stuck.

    Comments on this post

    • eramit2010 agrees
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    The concept of pointers is extremely simple.

    Basically, computer hardware consists of a central processing unit (CPU), random-access memory (RAM), and peripheral circuitry that provides I/O. Part of that I/O can be to a mass storage device, such as a hard disk drive. While your compiled program may (and most likely does) reside on hard drive, it has to be loaded into RAM before the CPU can execute it -- any file data that the program uses must likewise be loaded into RAM before the program can do anything with it.

    RAM is organized into a series of sequential memory locations, each containing a number of bits; nowadays it's commonly 8 bits which we call "bytes". Therefore, in your RAM, each byte is in a memory location and and each memory location has a unique address.

    When your program is loaded in RAM, it resides starting at a particular location. You start executing the program at a start address which is where the first machine instruction is and the CPU reads each successive instruction as successive bytes from that point, until it jumps to another location to continue to read and execute instructions from that location. Each location has a unique memory address.

    Your program's data -- variables and arrays, etc -- all reside in a their own individual memory locations. Each location has a unique memory address.

    A pointer is a variable that contains a memory address.

    In C++, a reference is a pointer that doesn't use C's pointer syntax. C doesn't have references as such.

    And an array name is largely equivalent to a pointer. A big difference is that you cannot change the address of an array while you can change the address contained in a pointer.

    Very simple and straight-forward.

    Pointers have many uses; eg:
    1. Implementing "call by name" (now commonly called "call by reference") in function calls.

    2. Iterating through an array without using indexing syntax (actually indexing in arrays is done with pointers; the index syntax is simple syntactic sugar to make things simpler for the programmer).

    3. Dynamically creating arrays and data structures.


    If you're still not clear on what pointers are, then learn assembly programming. In assembly, you will often load an address into a register and then use that register to perform indirect addressing. Once you've learned assembly, concepts like pointers are perfectly natural.
    Last edited by dwise1_aol; July 26th, 2013 at 01:33 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2
    Originally Posted by salem
    http://pw1.netcom.com/~tjensen/ptr/pointers.htm

    I suggest you read it, then ask some specific questions if you're still stuck.
    Ok, I think it just hit me. Pointers actually contain the address of a variable unless they are dereferenced to display the value of that variable.

    Because if you do:

    int *pointer = &k;

    cout << pointer;

    It would actually be spitting out the address of variable k.

    But if you did:

    int *pointer = &k;
    cout << *pointer;

    then it would spit out the value of k.


    Now you can change the value of a variable using pointers even though the return type is void? If thats the case than what is the point of a return type?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2
    Ok, great. Now I think I understand the relationship between ponters and arrays.

    Pointers contain addresses and if you increment the pointer it will change the adress accordingling, just like how an array is laid out.

    So if you want to increment pointer values youd have to spit it out like this:


    int *pointer = array[];

    cout << *(pointer + 1);

    It would first have to add one space to the pointer itself, which is moving one address up, then derefernce it with the *.

    I'm catching on!!!! :trockon:
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > Now you can change the value of a variable using pointers even though the return type is void? If thats the case than what is the point of a return type?
    What if the value you want to return isn't a scalar value, but an array?

    Study strcpy()
    Understand why it exists and why you can't do things like this
    char array[10];
    array = "hello";

    What if you want to return more than one result from a function, like say some data, and a success/fail status.
    Study fgets()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    54
    Rep Power
    2
    Originally Posted by salem
    > Now you can change the value of a variable using pointers even though the return type is void? If thats the case than what is the point of a return type?
    What if the value you want to return isn't a scalar value, but an array?

    Study strcpy()
    Understand why it exists and why you can't do things like this
    char array[10];
    array = "hello";

    What if you want to return more than one result from a function, like say some data, and a success/fail status.
    Study fgets()
    So:

    int array[10];

    int *pointer = array;

    So pointer would actually equal the first address location of the array?

    Which is essentially saying:

    pointer = array[0];
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    One example is when you have an array of arrays as such:

    Colors[4][]:

    Colors[0]="white"
    Colors[1]="green"
    Colors[2]="blue"
    Colors[3]="Purple"

    And you want to put them in alphabetical order-- SWAP them.

    Instead of moving whole arrays (which i'm not sure you can do in C), you swap the POINTERS which POINT to each array:

    p1--->Colors[0] /* p1=&Colors[0]
    p2--->Colors[1] /*p2=&Colors[1]

    swap(&Colors[0],&Colors[1])

    --I didn't write its code, but swap is also a function--

    And because we changed the position of the pointers, if you try to print the array thru pointers you will get the array in the right order.

    Character array is a bunch of characters that make up a sentence for example. Pointer to a character points to a single character, whereas in array, you only point to the beginning of the array. The name of the array is also a pointer to its beginning in the memory.

IMN logo majestic logo threadwatch logo seochat tools logo