Results 1 to 11 of 11

Thread: Pointer c++

  1. #1
    DaoK
    Guest

    Pointer c++

    I just want to know what is the utility of a pointer ? I just started the chapter about it and after the second page I do not understand why I have to learn that ? Please give me just the utility about it ( I will continue to read to know how to use it but I really want real person explication ).

  2. #2
    DaoK
    Guest
    ok now I am lost why should I need to use : & before a variable ? That give me only a strange number...

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    The clue to what pointers are is in the name.

    A pointer is a variable that can be used to point to another variable. Which variable it points to can be changed at runtime.

    & in the context you are talking about, when you put it before a variable identifier, will yield the address of the variable in memory.

    For instance:

    Code:
    char *p;
    char A;
    char B;
    
    A = 'a';
    B = 'b';
    
    p = &A;  // read as "p equals the address of A"
    At this point, p points to A. You can now use *p just like you'd use A. the * is the indirection operator. It allows you to use the variable the pointer is pointing to. *p evaluates to 'a' here.

    later in the code you could do this:
    Code:
    p = &B;
    At this point, p points to B, and *p evaluates to 'b' now.

    This functionality is very useful. Honest

    Keep reading about pointers, they're very important. VB hides them from you to make your life simpler but still uses them behind the scenes virtually all the time. C and C++ are lower level so you get to deal with these things yourself.
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    So would this print 5?
    Code:
    p = &B;
    *p = 5;
    cout<<B;

  5. #5
    Lively Member
    Join Date
    Dec 2000
    Location
    Indiana
    Posts
    73
    Originally posted by Wynd
    So would this print 5?
    Code:
    p = &B;
    *p = 5;
    cout<<B;
    Yes, but thats not what you want to do in most cases
    That says "p points to B. The value of the variable that p points to is 5. (That makes B 5) and then print out B."

    Most likely, you'll see this:

    PHP Code:
    B=5;
    p=&B//To get the address of B
    cout<<*p
    Think of pointers as a regular variable that holds the address of the variable, that is, where it is in memory. To get the address of a varaible, put a & in front of its name.
    if(GetWindowLong(hwnd,GWL_ID)==IDC_MICROSOFT_APPLICATION)
    {
    SetWindowText(hwnd,"I suck.");
    SendMessage(hwnd,WM_START_SUCKING,0,0);
    SendMessage(hwnd,WM_CRASH,0,0);
    }

  6. #6
    DaoK
    Guest
    Give me an exemple because if I need the variable B will use B and not a pointer....

    I think I do not understand sorry

  7. #7
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Code:
    //Will not work
    
    void myfunc(int x);//function declaration
    
    main()
    {
    
         int x=5;
         myfunc(x);  //Only a copy of the value is pass(variable passed by value)
         cout<<x;    //prints 5;
    
         return 0;
    }
    
    void myfunc(int x)
    {
         x=8;
         return;
    }
    Code:
    //Will work
    
    void myfunc(int *x);//function declaration
    
    main()
    {
    
         int x=5;
         myfunc(&x);  //a copy of the address is pass(variable passed by reference)
         cout<<x;    //prints 8;
    
         return 0;
    }
    
    void myfunc(int *x)
    {
         *x=8;
         return;
    }
    Code:
    //Will work(another version)
    
    void myfunc(int *x);//function declaration
    
    main()
    {
    
         int x=5;
         int *xp=&x;
         myfunc(xp);  //No * on the left of xp
         cout<<x;    //prints 8;
    
         return 0;
    }
    
    void myfunc(int *x)
    {
         *x=8;
         return;
    }
    I'm a VB6 beginner.

  8. #8
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Pointers are usually used in and passed to functions which need to change the value of the original variables. return can be used if you only want to return one value.

    int myfunc(int ...., int....., int......)
    {
    ............
    x=8
    return x;// can only return one value
    }

    hope it helps.
    I'm a VB6 beginner.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    void myfunc(int i)
    {
    // nothing
    }
    
    void main()
    {
       int val = 3;
       myfunc(val);
    }
    this creates a copy, as already said. That's no disaster, since an int is only 4 bytes. But now imagine:

    Code:
    typedef struct _bigstruct  // did you learn about structs? They are just a collection of variables that are grouped together under a new name
    {
      int a;
      int b;
      int c;
      int d;
      int e;
      int f;
      int g;
      int h;
      int i;
      int j;
      int k;
    } BIGSTRUCT;
    
    void myfunc(BIGSTRUCT bs)
    {  // still nothing
    }
    
    void main()
    {
        BIGSTRUCT big;
      // initialize values
       myfunc(big);
    }
    With every int taking 4 bytes, this structure takes a full 44 bytes. 44 bytes that have to be allocated and copied every time the function is called. But look at this:

    Code:
    typedef struct _bigstruct
    {
      int a;
      int b;
      int c;
      int d;
      int e;
      int f;
      int g;
      int h;
      int i;
      int j;
      int k;
    } BIGSTRUCT;
    
    void myfunc(BIGSTRUCT* pbs)
    {  // still nothing
    }
    
    void main()
    {
        BIGSTRUCT big;
      // initialize values
       myfunc(&big);
    }
    Voila, only 4 bytes (pointer size) to copy. This is much faster code. Just watch out when changing the values of this struct. They will be changed in the original object. But you can tell everyone that you do not intend to change the values:
    void myfunc(const BIGSTRUCT* pbs);
    This also tells the compiler to generate errors if you accidently change the values.

    I hope I didn't confuse you...
    Last edited by CornedBee; Oct 24th, 2001 at 12:04 PM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    DaoK
    Guest
    I think i understand maybe I will have some other question later about it.

  11. #11
    Addicted Member
    Join Date
    Apr 1999
    Location
    Freeport
    Posts
    204
    Maybe this will help maybe not. A pointer points to the place in memory where the varible is stored. All varible are stored in memory so this way you do not have to pass the byte of the varible to the function just the address. It keep you overhead(fat of the program) down and allow the program to run faster.

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