Thread: Pointers

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    Pointers

    Iv'e just started learning about pointers. I understand how they work but I don't see the reason to use them. Why use a pointer when you could just use the actual variable. It doesn't make sense to me. Please explain carefully the reason remembering that I am a noob.

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Check the topic below as it may be useful. Don't know though since I was too lazy to examine it to any degree. But considering it is three pages long it should be helpful.

    http://cboard.cprogramming.com/showt...ointers+reason

    [edit]One thing I just thought of. Pointers are used for a variety of storage techniques. Two I can think of off the top of my head are linked lists and binary trees.[/edit]
    Last edited by Frobozz; 09-24-2004 at 01:34 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This is a pointer to yourself http://cboard.cprogramming.com/showthread.php?t=57094
    This is a pointer to void http://void.void.void/
    This is a pointer to a pointer tutorial http://pweb.netcom.com/~tjensen/ptr/pointers.htm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    thankz frobozz, that helped alot. now im learning about classes. theres just an example in the tutorial that seems crazy. ill post it
    Code:
    class Dog {
    public:
        void setAge(int age);
        int getAge();
        void setWeight(int weight);
        int getWeight();
        void speak();
    private:
        int age;
        int weight;
    };
    i got this tutorial from polonyman and its been great. what ticks me is that the tutorial never explained about void. what is it? how does it work? and what does it do? i understand public and private so don't worry about that just explain

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    183
    void almost means 'nothing' .
    for example when you have a function that it returns an ' int ' before the functaion name you write 'int'.
    anf if you have a function that it doesnt return anything(no int , no float , no char .......) before the name of this functoin you write 'void'
    hope it helps.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    An aside but I believe void in the parameter list of a C function means you can have any number of parameters in the argument list. correct me if i am wrong

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You're wrong
    void means that there are NO arguments, while nothing in the parameter list means, in C only, any number of arguments. In C++, an empty argument list and a void argument list are the same.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    is this legal ?
    Code:
    void variable1;
    if it is, what does it mean ?

    blow me ... ...

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks for correcting me there. I knew it was specific to C but was confusing an empty list with a void list of parameters. Pulled it off the back of my brain which thanks to CornedBee has been revealed to be defective. Can I get a trade in?

    I don't think
    void variable is legal but
    void * variable is

    again I could be wrong.
    Last edited by curlious; 09-24-2004 at 06:39 AM.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    void variable1;
    is not valid. Why? Because it would have no meaning. You can't have a variable of no type.

    void *ptr1;
    is valid though. The variable has the type "memory address without any information as to what might be at that address."
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    oh....... i get it. thanks,CornedBee

    blow me ... ...

  12. #12
    hacker in training AdamLAN's Avatar
    Join Date
    Sep 2004
    Posts
    56
    You can use it to link structures together, like in a linked list

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    so are you trying to say that void means that the data has no type, and needs to be specified before use? unless it means that the pointer is declareed, but the type eg int isn't declared. does that mean you need to declare a type before use, or is it comatible with any standard data type?

  14. #14
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    No for the case of the class the void function just means it doesn't return a value(int,float,char,...). As for the pointer I believe a void pointer is just a pointer that points to nothing.
    Woop?

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    void pointers do point to something: memory. Unless they're NULL pointers.
    void means that the data has no type or there is no data, just raw memory (malloc returns void*) or the type is unknown to whoever handles the pointer. Someone with more knowledge can always cast the pointer to some non-void type.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM