Purpose of pointers?

This is a discussion on Purpose of pointers? within the C Programming forums, part of the General Programming Boards category; What exactly are pointers good for? It seems to me like they're the same thing as variables....

  1. #1
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187

    Purpose of pointers?

    What exactly are pointers good for? It seems to me like they're the same thing as variables.

  2. #2
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Re: Purpose of pointers?

    Originally posted by Yawgmoth
    What exactly are pointers good for? It seems to me like they're the same thing as variables.
    Yes pointers are varaible but variable that can point to the address of other varaible .Why is it so use full well one use of pointer in C is pass-by-reference that when you pass a value lets say from main() to a function it is by default passed by value that mean if the function does any change to that varaible that is passed it wont affect the one in main() but using pass-by-reference it will change the value in main().
    There are many other uses for pointer and its the back bone of C..
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Imagine if you have this structure:
    Code:
    typedef struct
    {
       int Ages[10000000];
       char Names[10000000][1024];
       char Locations[10000000][4096];
    }MYSTRUCT;
    And you need to pass it to a function for some modification. Then you can:

    A) Pass the structure, this means sending ~50 Gb of data.
    B) Pass a pointer to it, this means sending 4 bytes of data.

    Seems a little faster to send a pointer than the data, right?


    Another thing, run this program and compare the results:
    Code:
    void Modify1(int Var)
    {
       Var = 456;
    }
    
    void Modify2(int* Var)
    {
       *Var = 456;
    }
    
    int main()
    {
       int MyVar;
    
       MyVar = 123;
       printf("Before: %d\n", MyVar);
       Modify1(MyVar);
       printf("After: %d\n\n", MyVar);
    
       MyVar = 123;
       printf("Before: %d\n", MyVar);
       Modify2(&MyVar);
       printf("After: %d\n\n", MyVar);
    
       return 0;
    }
    As you see, in 2 you can modify a variable through a function, in 1 you cannot.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Pointers are variables that store memory address, and you can use it to pass variables to functions and modify them...
    You can also dynamically allocate memory using malloc() and free() in C, or new and delete in C++
    And pointers are very important in WIN32 Programming.
    none...

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    And don't forget about function pointers, also a very usefull application of pointers.

    >What exactly are pointers good for?

    In my opinion pointers are good for making programs more simple and more efficient.

  6. #6
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    pointers are for many purposes
    - without pointers you can not access memory address.
    - you can create dynamic array with pointer.
    - many data structures need pointer to solve.
    - function can't return char[] but it can return char*
    - etc.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

    Wink

    Pointers aren't important. You'll never use them.

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    7

    pointers are very useful

    Pointers allow you to keep hold of an adress of a variable or literal. This adress can then be passed on from function to function without multiple variable declarations. Many OS's use pointers extensively.

    If you know the adress of a variable or a literal, you can modify it's calling paramaters!

    This is one of those features that really makes C and C++ stand out from most other programming languages as it sort of like bridges between low level and high level programming.

    I recommend that you research this subject, hovewer this is Level 3 material (advanced diploma) and you shouldn't worry about it too much if you are studying for your high school exam or equvalent (unless of course it is in the syllabus).

    Most C programmers are very aware of pointers and so should you - even if you don't intend to use them.

    Best regards.... Happy studying -- C is a very nice language if you get use to it.

  9. #9
    ! |-| /-\ +3 1337 Yawgmoth's Avatar
    Join Date
    Dec 2002
    Posts
    187
    Thanks for answering my question, even though it was probably a newbie thing to ask.

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: pointers are very useful

    Originally posted by momo20016
    Pointers allow you to keep hold of an adress of a variable or literal.
    True literals (IE not string literals) can not be referred to by a pointer because they have no location in memory. They are strictly rvalues.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array of Pointers
    By Slavakion in forum C++ Programming
    Replies: 12
    Last Post: 03-31-2004, 04:05 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  3. moving pointers to pointers
    By Benzakhar in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 07:30 AM
  4. Array of pointers
    By falconetti in forum C Programming
    Replies: 5
    Last Post: 01-11-2002, 06:26 PM

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