Thread: Pointer

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    Pointer

    I have a variable called myVar

    so i declared and initialised it.
    Code:
    int myVar;
    Now, I would like to create a pointer pointing to myVar
    What should i do?

    1. int* myPtr = &myVar;
    OR
    2. void* myPtr = &myVar;

    What is the difference between void* and int*? does it depend on what it is pointing to?
    For example, if the pointer is pointing to an int, it has to be declared using int*.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    I think it's better to declare like this.
    Code:
    int* myPtr = &myVar;
    void * is a generic pointer, which can point to any type of variable, be it char,int or float. int * means it can only point to an int. I preffered 1 because you know that myVar is an int.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't use void* if you can avoid it. Because it's generic, the compiler loses all type information about what it points to. This can easily cause bugs on your part and make the compiler unable to do optimizations.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    1337
    Join Date
    Jul 2008
    Posts
    135
    By the way, can anyone explain the use of a pointer to a function.

    eg. int* call ( int* number)

    why must the function be a pointer type?

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    That's not a pointer TO a function, that's a function that returns a pointer TO an int. So it would be any function that fits this form:

    Code:
    int* call(int* number) {
        int* result;
        ...
        return result;
    }
    However, unless you're returning a pointer to dynamically allocated memory, you're returning a pointer to data that has now gone out of scope - so be careful.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Remember also that functions are always stored somewhere in the memory (code segment, most likely), so therefore they have an address, and thus you can create a pointer to point to that function. That's called a function pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Have a look at this tutorial. It really is a good one (that is how i understood this)

    Function Pointers in C and C++ - Cprogramming.com

  8. #8
    1337
    Join Date
    Jul 2008
    Posts
    135
    Well, I want to ask one more question.

    void (*foo)(int);
    can it be written as
    void *foo(int);
    ?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First is a function pointer, second is a function prototype taking an int, returning void*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by valthyx View Post
    Well, I want to ask one more question.



    can it be written as ?
    Code:
    void (*foo)(int);
    Here foo is a pointer to a function that takes an int and returns nothing(void).
    Code:
    void *foo(int);
    Here foo is a function that takes an int and returns void *.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  11. #11
    1337
    Join Date
    Jul 2008
    Posts
    135
    Code:

    void (*foo)(int);

    Here foo is a pointer to a function that takes an int and returns nothing(void).
    Code:

    void *foo(int);

    Here foo is a function that takes an int and returns void *.
    Well, what does it really mean? I don't really get the second one.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by valthyx
    Well, what does it really mean? I don't really get the second one.
    The explanation for the second one seems pretty clear to me. For example, here is a possible definition of such a function:
    Code:
    void *foo(int n)
    {
        return malloc(n);
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    1337
    Join Date
    Jul 2008
    Posts
    135
    IC, ok, now I understand. Thanks

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