Pointer...eh?

This is a discussion on Pointer...eh? within the C++ Programming forums, part of the General Programming Boards category; While looking over some examples on this site I realized I didn't understand pointers. Well, I took the example code ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    361

    Pointer...eh?

    While looking over some examples on this site I realized I didn't understand pointers. Well, I took the example code and decided to play with that some to see if I could better understand them, when to use them or why.

    Here is the original code
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      int x;
      int *p;
    
      p = &x;
      cin>> x;
      cin.ignore();
      cout<<*p <<"\n";
      cin.get();
    }
    Well, I decided to see what kind of answers I would get if there were no pointers in this code in order to solve the why question I had for these tricky guys. Here is where the interesting part comes in. As long as I enter a number(thats not too large) I will get the answer 2 when all pointers are removed(3 if your counting). I couldn't seem to figure out why. Hopefully someone here will be able to tell me why...and perhaps give me a good definition of when and why to use pointers. I think I understand...but I am sure hearing a few different definitions will help out a lot.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    As long as I enter a number(thats not too large) I will get the answer 2 when all pointers are removed(3 if your counting).
    Huh?

    To understand the "why" of pointers, you have have to understand what a function is and the different ways values can be passed to a function.
    Last edited by 7stud; 05-04-2005 at 08:07 PM.

  3. #3
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51

    Cool

    Also pointers can eliminate a variable when dealing with arrays.
    (Sorry this is in C)
    Code:
    int main()
    {
          int a[] = {0,1,2,3,4,5,6,7,8,9};
          int *ptr;
          
          //array plus indexing variable
          for(int i = 0; i < 10; i++)
            printf("%d",a[i]);
          printf("\n");
          
          //pointer only
          for(ptr = a; ptr < a + 10; ptr++)
            printf("%d",*ptr);
          printf("\n");
    
          return 0;
    }
    With the first you have two variables. a and i. With the second, only one.

    (Credit to John from XtremeVBTalk for that code)
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    I understand functions...I just don't know when to use which type of the pointers and I would also like a better understanding of them.

    In the code I gave you... if I remove all of the * and & then every time I enter a number it gives back 2. Say I enter 5...well it comes back as 2. Just curious as to why it is doing this.

    I guess I understand how they work...I just have no clue when I really need to use them. In the above code...why do you need to use it there? I know the compiler gives ya an error...but why does that conflict occur?

    Maybe I am just missing something...maybe I just need to read the pointer tutorial on this site for the 10th time...or maybe it's because I failed the test of 6 F's in the general forums. Oh well...I'll get it eventually.

    EDIT: With the above code...if I add a second printf("%d",*ptr); below the last \n printf...I get some random numbers. So why doesnt it print a like the others? This the type of stuff that confuses me.
    Last edited by Glirk Dient; 05-04-2005 at 08:31 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by elnerdo
    With the first you have two variables. a and i. With the second, only one.
    a, i <---->a, ptr
    Tie score: 2-2.

    (credit my First Grade teacher who taught me how to count to 2)

    In the code I gave you... if I remove all of the * and & then every time I enter a number it gives back 2. Say I enter 5...well it comes back as 2.
    It's not productive to discuss errors in hypothetical code.
    Last edited by 7stud; 05-04-2005 at 09:14 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You should have realy posted the code with * and & removed but here it is
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      int x;
      int p;  //declare 2 undefined ints
    
      p = x; //the value of p is set to the value of x (undefined)
      cin>> x; //read in value and set it to x
      cin.ignore();
      cout<<p <<"\n"; //output p (same unchancged undefined value)
      cin.get();
    }
    As you can see the only value that was changed by this program was x and since in the origional program p pointed to x then printing out the value pointed to by p would print the variable x. When p is not a pointer it isn't affected by the value of x and is undefined which on your machine made it equal 2.
    As I think was allready mentioned when you send a pointer to a function you can access the memory pointed to by that pointer from the function. This is usefull when you want a function to operate on a string or structure.
    Last edited by Quantum1024; 05-05-2005 at 04:23 AM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by 7stud
    a, i <---->a, ptr
    Tie score: 2-2.

    (credit my First Grade teacher who taught me how to count to 2)
    Good one 7stud! I noticed that too, guess I can say the same thing.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation A function can only return ONE value.

    I understand functions...I just don't know when to use which type of the pointers and I would also like a better understanding of them.
    I'd say the most common reason for using pointers is to get-around the fact that a function can only return one value. By using a pointer, a function can "get to" all of the elements in an array, or access an entire structure or object.

    A single variable can only hold one value. However, a single pointer can point-to an entire array, or an entire structure or object.

    Well... Actually, the pointer only holds ONE address... the first memory location used by the array/structure/object. But, these things are stored sequentially in memory, so if you can find the first element in the array you can get to all of the other elements. The compiler knows how to find all of the stuff in a structure or object, given the starting address.



    Most beginning C++ books will introduce pointers with a swap() function...

    i.e. Starting with X=5 and Y=10. The function "swaps" the values so that X=10 and Y=5.

    A function could return either a modified X or Y, but not both!

    In order for a function like swap() to work, you need to pass the addresses of the variables into the function. You can do this with pointers or references.

    Full disclosure - References are easier to use are generally preferred over pointers when you have a choice.
    Last edited by DougDbug; 05-05-2005 at 01:08 PM.

  9. #9
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    The first Time I saw pointers used in a real situation was when I was using textures and other files in my openGL Programs, not saying they dont have any other use, but it definately is a complex topic, I don't fully understand them yet

  10. #10
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    Quote Originally Posted by 7stud
    a, i <---->a, ptr
    Tie score: 2-2.

    (credit my First Grade teacher who taught me how to count to 2)
    You idiot. In the first, you're using two variables EVERY time through the loop, a and i.

    In the second, you are only using one, ptr.

    Yes, in the second you are using a in the declaration of the for loop, but that's only once.
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

  11. #11
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Quote Originally Posted by elnerdo
    You idiot.

    Jeeze! :\

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Quote Originally Posted by elnerdo
    You idiot. In the first, you're using two variables EVERY time through the loop, a and i.

    In the second, you are only using one, ptr.

    Yes, in the second you are using a in the declaration of the for loop, but that's only once.
    In what way does the number of times a variable is used matter? The only possible reason I can think of is code clarity, but then, the first piece of code is still more clear despite using only one variable inside the loop body.

    And by the way, the variable a is used once for each time through the loop because the comparison "ptr < a + 10;" is evaluated each time. So what you really mean is that the second version uses only one variable in the code inside the loop body, which, again, doesn't matter at all.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I understand functions...I just don't know when to use which type of the pointers
    I guess I understand how they work...I just have no clue when I really need to use them.
    There are two aspects to using pointers:

    1) Efficiency.
    Whenever the data you are passing to a function is not a simple data type like int, double, etc. using a pointer is more efficient. Functions have to make a copy of whatever arguments are passed to them. When you pass a pointer, you are passing an address. An address is essentially an integer, and making a copy of an integer doesn't take up much room in memory. On the other hand, if you pass an object to a function, the function has to copy the whole object, and an object can have an enormous amount of data inside it, which takes up more room in memory.

    2) Being able to change the original object you pass to a function.
    If you pass an object to a function, the function makes a copy of the object for the function. If the function then makes changes to the object, the changes are made to the copy, and when the function ends, the copy is destroyed leaving the original object unchanged. Instead, if you pass a pointer to the function, the function copies the pointer. But, a copy of a pointer points to the same place in memory as the original pointer, so you can use the copy of the pointer to access the original object and change it. Like before, when the function ends, the copy of the pointer is destroyed, however the original object remains permanently changed. The original object is permanently changed because the changes you made were directly to the original object--not a copy of the object.

    You should know that when you declare an array like this:

    int myArray[100];

    and pass the array name to a function, you are automatically passing an address, so you don't have to worry about using pointers to ensure that only the address is copied and not the whole array.
    Last edited by 7stud; 05-05-2005 at 04:20 PM.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by elnerdo
    You idiot. In the first, you're using two variables EVERY time through the loop, a and i.

    In the second, you are only using one, ptr.

    Yes, in the second you are using a in the declaration of the for loop, but that's only once.
    ...then does this use 0 variables in the loop:
    Code:
    int a[] = {0,1,2,3,4,5,6,7,8,9};
    for(int i = 0; i < 10; cout<<a[i], i++)
    ;
    Last edited by 7stud; 05-05-2005 at 04:23 PM.

  15. #15
    Resident nerd elnerdo's Avatar
    Join Date
    Apr 2005
    Location
    Northern NJ
    Posts
    51
    Don't try to be a smart.alleck

    Look, the point of that program is to output the numbers in the array. Yours doesn't accomplish that. The one with the pointer accomplishes that task and uses one less variable than the one with the array and the index number.

    Daved, I'm not exactly sure, but I'm fairly certain that using one variable will be faster than using two variables. This just seems like common sense to me.
    nerds unite!

    I'm using windows XP.
    I'm using dev-C++ by bloodshed.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 01:36 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 08:40 PM
  4. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21