Results 1 to 22 of 22

Thread: what exactly ARE pointers

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312

    what exactly ARE pointers

    what exactly ARE pointers, and what are they for?

    i knwo they can refer things to the address in the memory in which tey are stroed, &number, par example! but i dobnt understand... WHY! you would want to do that!, why not use a normal int/char etc?
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  2. #2
    DaoK
    Guest
    I am at Chapter 9 Sams book (Sams Teach yourself C++). I have just finished the chapter 8 who talk about Pointer.

    Pointer is a ways to declare a variable to store is adress in the memory of the computer. With that you can take and change the value of that adress in all you function without declaring anything in public. I just know that it's very usefull but I do not understand all all I might read again that chapter later

    Hope that give you some information.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Pointers are lowlevel stuff

    Pointers aren't meant to exist in a highlevel programming language but C++ is a hybrid. Main reason why there are pointers is because of backward compability with C.
    Pointers are mainly useful for dynamical memory allocation, that is when you construct datastructures with variable size, such as arrays. Pointer aritmetics is a powerfull tool as well which often saves performance, dealing with C style strings you can skip a load of rutine that would waste performance of a string type, but on the other hand you would need to follow them strictly, a lot of bugs are related to lack of thinking lowlevel.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312
    yeh, but what good are they for, and what is the differance in using them and in usingint or osmething
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  5. #5
    Moon
    Guest
    My guess is that you lack imagination, or you never run into a programming problem which could be solved using pointers.
    Make more programs in C/C++ and you'll see by yourself the use of them. It's hard to explain what are they for as their use allow a lot of flexibility.
    Personally they turn me on.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312
    yeh, but i cant very welhem in a program if i dont know what to do with them can i!!!!!!!

    give me a program ot do, and a use of pointers in that program, and hwy a normal int/char/float etc. wouldnt be as good.

    I have heard the right use of pointers can make your program faster!!! but why!

    this is what my book is seemingly failing to help me with here
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    C example:
    Code:
    typedef struct _bigbugger {
        int a, b, c, d, e, f, g;
        double x, y, z;
    } bigbugger;
    
    void somecode(const bigbugger *ptr) {
        printf("%d, %d, %d\n", ptr->a, ptr->b, ptr->c);
    }
    Here, this means that rather than copying the ENTIRE structure to the function, you pass a pointer instead - thus only 4 bytes are copied (for the pointer), rather than sizeof(bigbugger) bytes.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Here's your program:
    Write a function that takes an integer and CHANGES it to it's own square. So when I write...
    Code:
    int i = 4;
    // call the function with i
    // then output i:
    cout << i;
    ...the output is 16. C++ references are not allowed (although you should use them when you're doing real things).
    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.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Another one: write your own resizable array
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312
    i wont include includes etc!!!
    Code:
    int main()
    {
    int i = 4;
    int t;
    
    t = i * i;
    
    cout << i;
    return 0;
    }
    is that what you mean?
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312
    Originally posted by kedaman
    Another one: write your own resizable array
    you mean like make

    char nanny[]={ted,bill}

    into something constantly addable to?
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Where did includes come into it?
    Code:
    void square_it(int *num) {
        *num *= *num;
    }
    
    void somecode(void) {
        int x = 5;
    
        square_it(&x);
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312
    so why did you need pointers in that, what would have happened without the pointers?
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hmm, looks like you need to learn some functional programming, but I'd recommend you go learn OOP directly, there's a online tutorial TYSC++ which is quite good: http://newdata.box.sk/bx/c/
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312
    ok im gonna read a bit about functions, maybe this will make pointers clearer to me!
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by JafferAB
    so why did you need pointers in that, what would have happened without the pointers?
    If I hadn't used a pointer, it would have modified the COPY that was sent to the function.

    C and C++ pass by value (ByVal) as the default - VB uses ByRef.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, I want you to write a function that basically does this:
    i *= i; // will make i the square of itself
    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.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    May 2001
    Location
    England
    Posts
    312
    ooooh, i see, i see why tht would be usefull because its very complex without that, you got a huge line thing in this here book and a massive program!

    its just a matter of knowing when to use em!
    Power to 2000 Electronic Donkeys!
    www.edonkey2000.com
    I hate case sensitivity... all you get down the M1 is "are we there yet" and "ouch, watch the bumps".

  19. #19
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    unfortunately parksie screwed it up
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  20. #20
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by kedaman
    unfortunately parksie screwed it up
    Why change the habit of a lifetime?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  21. #21
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by parksie
    Why change the habit of a lifetime?
    yeah why? life is unfortunately
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  22. #22
    jim mcnamara
    Guest
    Why you use pointers is the same reason why you pass a parameter ByRef in VB - so that:

    a: you don't have to pass a mess of data on the stack, just one long. This is one big reason why it's faster.

    b: so that when you alter the parameter, the change is reflected in the calling module as well.

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