pointer

This is a discussion on pointer within the C++ Programming forums, part of the General Programming Boards category; why do ppl use pointers> can they just acces cant other then directly?...

  1. #1
    Registered User deltabird's Avatar
    Join Date
    Jan 2003
    Posts
    73

    pointer

    why do ppl use pointers> can they just acces cant other then directly?

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    what...........hmmm........English please...!!??

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I think the most common use is to make a function affect more than one variable.

    Say you want to change the X and Y position of something on the screen. Your function could return one value to be assigned to either X or Y, but not both. You can pass-in as many pointers as you wish. So your function could directly change both X and Y.

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    what the hell is going on, i replied to the same thread as this!!!! http://cboard.cprogramming.com/showt...hreadid=33324, the id's have change! what the hell is going on?
    Be a leader and not a follower.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Ummm... Also, when you pass a variable to a function, you're NOT passing the variable. You are passing the VALUE of the variable. In fact this is called "passing by value". Often the variable name is even different inside the function. (This can be confusing for beginners.)

  6. #6
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    fine everyone, just ignore me! deltabird - did i not reply to your thread, and you said thankyou! I need some evidence....
    Be a leader and not a follower.

  7. #7
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    illustration...

    Code:
    #include <iostream>
    using namespace std;
    
    void swap_incorrect(int var1, int var2)
    {
    	int temp;
    
    	temp = var1;
    	var1 = var2;
    	var2 = temp;
    }
    
    void swap_correct(int *var1, int *var2)
    {
    	int temp;
    
    	temp = *var1;
    	*var1 = *var2;
    	*var2 = temp;
    }
    
    int main()
    {
    	int var1 = 5;
    	int var2 = 10;
    
    	cout << "default : " << var1 << ' ' << var2;
    
    	swap_incorrect(var1, var2);
    	cout << "\nswap incorrectly: " << var1 << ' ' << var2;
    
    	swap_correct(&var1, &var2);
    	cout << "\nswap correctly: " << var1 << ' ' << var2;
    
    	cout << endl << endl;
    
    	return 0;
    }
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by subdene
    fine everyone, just ignore me! deltabird - did i not reply to your thread, and you said thankyou! I need some evidence....
    I saw a similar question, and your answer. That thread is gone now (deleted I presume). oh well..
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    ahh, thank you! thought i wasn't going mad. wonder why they deleted it? anyways..........
    Be a leader and not a follower.

  10. #10
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Along with the reasons given above, pointers are necessary for runtime polymorphism, i.e. a combo with virtual functions.
    Also necessary for arrays as they technically cannot be passed to a function as a whole, only the address is passed. Thats much more efficient. And using pointer arithmetic to access array elements is executed faster than indexing an array. [I dont know the details about that fact, but it just is]
    Almost forgot. Strings - pointers are a must for working with strings. Frankly, I dont know how Java-ers manage.
    I AM WINNER!!!1!111oneoneomne

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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