Thread: Pointers

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    19

    Pointers

    I'm just new to the C World. I just only know a few and coded some successfully in school. I'm better of reading a code than making one.

    Now, in school we have tackled about "Pointers" and its uses. My teachers said that in higher years pointers will be very useful, but most of the times i dont think that pointers are not important. Please show me the light

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jazzglenn421 View Post
    I'm just new to the C World. I just only know a few and coded some successfully in school. I'm better of reading a code than making one.
    That probably applies to nearly everyone. Just like I can read some legal documents in English (or Swedish which is my native language), but I could not write Legal English.

    Now, in school we have tackled about "Pointers" and its uses. My teachers said that in higher years pointers will be very useful, but most of the times i dont think that pointers are not important. Please show me the light
    Well, that would be a fairly large undertaking. Most books that deal with pointers would do so in about 15 pages or so.

    Pointers are important to some things, and less important in other types of code. The first instance of pointers is probably when you need to pass a single variable (say an int) to a function to be filled in, e.g.
    Code:
    #include <stdio.h>
    void getCoord(int *px, int *py)
    {
       *px = 47;
       *py = 11;
    }
    
    int main()
    {
       int x = 0, y = 0;
       getCoord(&x, &y);
       printf("x = %d, y = %d\n", x, y);
       return 0;
    }
    If you remove the * signs in the getCoord and the & from the calling code, you will not get x and y to change.

    But pointers are used for many, many other things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    If you need to pass a large array of data into a function, you simply pass a pointer to the array. Without pointers you'd have to pass a whole new copy of the array into the function, which is obviously far less efficient and wasteful of memory.
    Last edited by Sharke; 03-16-2009 at 07:44 PM.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    You just have to know that one day the power of pointers will suddenly dawn upon you, and you will think of better ways to do nearly everything.

    Also, it is important (I think) to realize that many languages say they don't have pointers but that is a lie, underneath the hood deep in the language there are pointers. Many things (well, pretty much every non-trivial thing) requires pointers to do correctly.

    For example, you can't allocate any memory on the heap without pointers. You can't draw graphics without pointers. You can't read and write to files without pointers. You can't ... the list goes on and on.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    Thanks for the replies guys. We on class meets like once a week, but we spend 5 hours.

    in class we tested a simple program:
    Code:
    main()
    {
    #include<stdio.h>
    #define p printf
    int firstvalue=5, secondvalue=10;
    int *p1, *p2;
    
    p1=&firstvalue;
    p2=&secondvalue;
    *p1=10;
    *p2=*p1;
    *p1=20;
    
    p("First Value is %d",firstvalue);
    p("Second Value is %d",secondvalue);
    
    getch();
    }
    I was just wondering why cant firstvalue be like "firstvalue=10"? instead of "*p1=10". and like "secondvalue=firstvalue" and "firstvalue=20"?

    just wondering thought.... through that thought i think that pointers are quite useless. maybe someone could show a program that pointers are really really important.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jazzglenn421
    I was just wondering why cant firstvalue be like "firstvalue=10"? instead of "*p1=10". and like "secondvalue=firstvalue" and "firstvalue=20"?
    It can, but presumably the purpose of the exercise is to practice working with pointer syntax and the address of operator.

    Quote Originally Posted by jazzglenn421
    just wondering thought.... through that thought i think that pointers are quite useless. maybe someone could show a program that pointers are really really important.
    Write a program that reads in an arbitrary number of integers from standard input and prints them to standard output in sorted order. You may freely use the C standard library (but you may not "pass the buck" to an external program/script).
    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

  7. #7
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Pointers are highly useful in many ways:

    1)
    Assume you have a large amount of data and you want a function to analyze it, e.g. compute its size. The naive approach is to simply pass all data as a function argument, which makes a local copy of the data. For an int, this is ok. For anything larger, it's probably not. So you don't pass the data itself, but merely a pointer to the data, i.e. you tell the function "go there and have a look".

    2)
    Assume you have a function that needs more than one return value, e.g. one for the actual result and one for the error condition (think of converting a string to an int; what do you return if the string doesn't contain the textual representation of a number?). In cases like this, the actual result is often stored at a memory location which is passed with a pointer, so the return value can be used to tell the caller whether it worked or not.

    3)
    Pointers allow for data structures which are more complex than arrays, e.g. linked lists, trees, graphs. This is useful because if you have a large set of items, your memory probably can't hold it contiguously. With pointers, you can store each item somewhere in memory and use a pointer to tell where the "next" item can be found. The items are distributed all over your physical memory, but the pointers glue them together logically.


    There are many more examples, but these three have analogies in the real world. I'd recommend reading about pointers without trying to understand everything at first glance. Once you're used to using pointers, go back to the pointer chapter and find out why it's working the way you're used to doing it.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #include<stdio.h>
    #define p printf
    trying to avoid typing four letters twice by typing #define p printf seems a lot of work to me, and it's naturally obfuscating your code.

    And #include <stdio.h> should be before main() definition.

    The example shown is definitely an example of "no need for pointers" here. But to make sensible and yet simple examples can be quite difficult. This might be one:
    Code:
    #include <stdio.h>
    int main()
    {
        int array[5];
        int *pMax, *pMin;
        int i;
        int sum;
        printf("Enter 5 numbers with space between them:\n");
    
        for(i = 0; i < 5; i++)
        {
          scanf("%d", &arr[i]);
        }
        // Now, find the max and min values:
        pMax = &array[0];
        pMin = &array[0];
        for(i = 0; i < 5; i++)
        {
           if (*pMax > array[i])
               pMax = &array[i];
           if (*pMin < array[i])
               pMin = &array[i];
           sum += array[i];
        }
        sum -= *pMax + *pMin;
        printf("Average of 5 items, less biggest and smallest: %5.2f\n", 
                  sum / 3.0);
        printf("Max = %d, min = %d\n", *pMax, *pMin);
        return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  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