why use a pointer

This is a discussion on why use a pointer within the C++ Programming forums, part of the General Programming Boards category; what makes a pointer so cool ... why do we even use them...

  1. #1
    Unregistered
    Guest

    why use a pointer

    what makes a pointer so cool ... why do we even use them

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    They improve efficiency and memory usage, they increase readability and allow for more compact code, they allow us to create linked data structures such as lists and trees, shall I go on? Just take it on faith that pointers make your job as a programmer much easier.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    If you are just learning C++, then wait a little bit. You will see in time why they are so important. For example, with pointers u can create arrays whose size is inputted by the user. Or even better, you can create array-like groups that can be increased in size at any time.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    99
    I beleive you could do that without pointers...
    Code:
    cout<<"Enter the size"<<endl;
    cin>>size;
    char array[size]; //That would work wouldn't it?
    Or you could do it with 2D Arrays.

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    No, you cannot. The size of an array has to be a constant...

    However, you could do something like
    #define ARRAY_SIZE 8;

    char array[ARRAY_SIZE];

  6. #6
    Unregistered
    Guest
    Well I'm quite a ways past arrays and I don't see the reason for pointers yet. I must be missing something fundamental. Why is it that linked lists, dynamic arrays, etc can only be created on the heap rather than the stack.

  7. #7
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    with pointers u can change the values assigned to a variable in any function
    pointers are always used in HL programs.
    for sxample

    u assigned x;
    and then with pt u may later change the value of x into watever u want, usually it is used where user input is req'd in the program.


    so never say no to pointers unless u a noob and are not rdy to move to next step.

  8. #8
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Pointers are _HARD_ to understand. The books i've read did not pay enough attention to pointers... and I was forced to look elsewhere.

    I really don't think the pointer tutorial on this website is sufficient either... what really helped me learn was diagrams of memory addresses and such.

  9. #9
    Unregistered
    Guest
    i think i'm starting to grasp it ... with pointers crap can be changed dynamically by funs in the program ... but can't stuff on the stack be changed like this to?

  10. #10
    Unregistered
    Guest
    that's supposed to be funcs

  11. #11
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    This should give you everything you need to know..
    Best pointer tutorial I've found: http://www.cplusplus.com/doc/tutorial/tut3-3.html

    I think it also lists some of the possible uses for pointers.

  12. #12
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    pointers allow you to own pieces of data without naming it. think about it this way: if you had 20 cattle named beth, sarah, julia, kris.... and you had 20 other cattle who all responded to "hey you!", which can you manipulate easier? which group of cattle is more flexible in your eyes?

  13. #13
    Registered User InFeStEd-ArCh0n's Avatar
    Join Date
    Apr 2002
    Posts
    63
    Originally posted by dayknight
    with pointers u can change the values assigned to a variable in any function
    pointers are always used in HL programs.
    for sxample

    u assigned x;
    and then with pt u may later change the value of x into watever u want, usually it is used where user input is req'd in the program.


    so never say no to pointers unless u a noob and are not rdy to move to next step.
    Are you insinuating that they are not used in lower level programming? I use pointers all the time in Assembly. Personally, I think handling pointers in assembly is better (even more fun) than C++. I encourage all of you to program in assembly. x86 assembler is like a drug.
    -Mike
    {InFeStEd-ArCh0n}

  14. #14
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    Pointers are your Best FRIEND
    YES THEY ARE...
    LEARN THEM HARDCORE and USE THEM ALOT...

    If you dont, i can gaurentee you , you wont last long in the C++ world.
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  15. #15
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    Originally posted by InFeStEd-ArCh0n


    Are you insinuating that they are not used in lower level programming? I use pointers all the time in Assembly. Personally, I think handling pointers in assembly is better (even more fun) than C++. I encourage all of you to program in assembly. x86 assembler is like a drug.
    i meant that they are always used in HL programs, well u could use them in LL programs, but then u dont really need them in small prgrams.

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, 11:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 04:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 08:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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