View Poll Results: When should C++ be taught

Voters
9. You may not vote on this poll
  • High School

    7 77.78%
  • Before College

    1 11.11%
  • During College

    1 11.11%
  • Post College Education

    0 0%

Need help with pointers

This is a discussion on Need help with pointers within the C++ Programming forums, part of the General Programming Boards category; I need help in understanding how to use/write a pointer. I am only a junior and i have been told ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    2

    Talking Need help with pointers

    I need help in understanding how to use/write a pointer. I am only a junior and i have been told this class should not be taught to HS Students and i am lost, in it, and i can't drop the class, whats a girl to do?

  2. #2
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    yes

    some people think, when they they first learn pointers, "what will i ever need this for in c++?". Thats what I first thought. First, you will need them because other concepts taught in c++ have pointers, so they are a must. Second of all, to change the values of variables without creating an extra variable, you need to pass pointers to the function. Pointers are simple to understand. First of all, they of course point to the address of a variable or function mainly. You declare it like this: int* ptr; You need the asterisk after the variable type. Now, if you want to change whats in the address, you write something like this: ptr++. It increments the address by 1. If it was pointing to an array: ptr=array[1]; Then: ptr++; will make it point to the third element of the array, or array[2]. Also you can change the value of what is in the pointer like this: *ptr++; that will increment the value of whats in the pointer by 1. Blah blah, that enough or you need more?

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421

    Thumbs down

    What's a girl to do?

    Search the web!!! There are literally thousands of tutorials on this kind of stuff.... search the web first, and post if you have trouble with something afterwards.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    5
    Just remember, a pointer just stores a memory address. That's all. That seemed to clear everything up for me.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    60

    example of pointers used vs private

    Pointers int c++ are handled in a pleasent way.
    They get tricky when used in arrays with three or more dimensions, it's not easy passing such arrays to functions.

    class dummy{
    int nonsense1;
    double nonsense2;
    public:
    void dononsense(int nonsense1){
    nonsense1++; nonsense2=1.0;}
    }

    dummy XYZ;
    int main(){
    dummy ABC,*UVW; // nonsense1 is private, but ...
    UVW = &ABC;
    void *ptr = UVW;
    int*pt=(int*)ptr;
    *pt=2;
    XYZ.dononsense(5);
    ABC.dononsense(5);
    }

    QUESTION: Which values are stored in XYZ.nonsense1, XYZ.nonsense2, ABC.nonsense1, ABC.nonsense2 after the calls to dononsense?
    (to do less nonsense you may need to know the this-pointer and its use; however, finding the correct answers would prove you're an expert on pointers.)

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Pointers are pretty straightforward until you get into multiple indirection. Then they can become a little harder, but not much.

    Pointers do exactly that - point at something. They point at memory addresses - you could think of as though they point to one little mailbox in a huge grid of mailboxes. The value at an address would be what's in the mailbox. The address of the mailbox is where the mailbox is.

    This stuff really should be taught in High School in order to prepare students for the everchanging everexpanding world of computers. If I had learned this in high school, I would be much farther along than I am. It is a shame that most have to wait until college to learn C/C++. My opinion is that you will learn more on your own, but school will get you into some very good habits and will teach you good program structure (hopefully).

    Create some integer pointers and use printf("%p",yourptr) to see it's address and printf("%d",*yourptr) to see what it's value is or what's in it's mailbox.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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