Thread: pointers

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    307

    pointers

    ok i know pointers point to a memory location containing data, but i dont use pointers....i just use variables and pass them and use them directly...

    not to clear WHY you use them, When you use them, or the advantage in them over not using them.

    Pointers have always confused me...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Consider one common use case: suppose you have a variable that takes up a great deal of space, e.g., it is a struct with several large array members. You want to pass this variable to another function. If you pass a copy of that variable, then you have a great deal of stuff to copy. Hence, you might choose to pass a pointer to that variable instead.

    Oh, and if you are passing an array instead of a struct containing an array, then your array will be converted to a pointer to its first element, so you cannot really get away from pointers here.

    EDIT:
    Wait, yet another common use case comes to mind: consider a straightforward implementation of a linked list.
    Last edited by laserlight; 01-13-2013 at 08:47 AM.
    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

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    For a simple case: if your function wants to modify the things you pass to it, you use pointers:

    Code:
    void exchange(int *a, int *b)
    {
    	int temp = *a;
    	*a = *b;
    	*b = temp;
    }
    
    int main(void)
    {
    	int x = 123;
    	int y = 456;
    	exchange(&x, &y);
    	printf("x: %d, y: %d\n", x, y);
    	return 0;
    }
    This is why you give pointers to scanf -- otherwise scanf can't store the result(s) into your variables.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    ok so in C++ when i used to use the following to modify variables that were passed
    Code:
    void exchange(int &a, int &b)
    {    
     int temp = a;    
     a = b;    
     b = temp;
    }
    in C i have to use pointers instead of directly changing the variables?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. Actually, in C++ you are not "directly" changing the variables either: you do so by means of the C++ reference parameters.
    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

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    ok now it is making sense....
    but confused about your "your array will be converted to a pointer to its first element"

    so if my array is
    Code:
    int test[20];
    how would i pass it to a function, AND how would i change the entire array like in
    Code:
    for(int x=0;x<20;x++)
       {
           test[x]=x;
       }

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    int foo(int* array, const int size)
    {
    //handle array as you normally do
    }
    Maybe you have to study a little about pointers in C. It's a big chapter
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    yeah read it, but not 100% clear to when to use it, when not to. And they get so crazy with it, that it dont make sense for half of it!!! ill re-read it again, now that i have a better understanding of it now! Thanks again everyone!

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Post again if needed

    Good reading!!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Quote Originally Posted by std10093 View Post
    Code:
    int foo(int* array, const int size)
    {
    //handle array as you normally do
    }
    Maybe you have to study a little about pointers in C. It's a big chapter
    Are there any disadvantages/advantages to declaring your function parameters like std10093 example vs

    Code:
    int foo(int array[], const int size)
    {
    //handle array as you normally do
    }
    or do they accomplish the exact same thing?

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by camel-man View Post
    Are there any disadvantages/advantages to declaring your function parameters like std10093 example vs

    Code:
    int foo(int array[], const int size)
    Using brackets is potentially clearer. Also, you are allowed to declare the size of the array inside the brackets. This allows for more convenient notation in the case of multi-dimentional arrays. For example

    Code:
    int sum(int rows, int cols, int a[rows][cols]);
    Declaring in this way lets you use the normal a[r][c] notation within the function. The caller of the function is responsible for providing a pointer to an appropriately-sized memory area.

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by camel-man View Post
    do they accomplish the exact same thing?
    Yes.
    To me it is clearer without brackets.
    On the other hand, what the other use proposed seems to make you pass one parameter less.

    EDIT: But think of how clumsy all this bracket story starts to become, as the dimension of the array increases.
    Last edited by std10093; 01-13-2013 at 05:03 PM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by std10093 View Post
    To me it is clearer without brackets.
    If you have a three dimensional array declared as `double A[x][y][z]' is it clearer to address an arbitrary element with indices a, b, c as

    A[a][b][c] = foo;

    or

    *(((A+a*x*y)+b*x)+c) = foo;

    ? Especially when there are more than two dimensions, the bottom form becomes tedious and error prone.

    Creating a function that operates on the array A will always require 4 parameters no matter if you use the pointer notation or the array notation: you need to pass one parameter representing the array A, and you need to pass three integer parameters representing the maximum bound of each dimension.

    In other words, your choices for a function bar are pretty much just these two:

    void bar1(int x, int y, int z, double *A);

    void bar2(int x, int y, int z, double A[x][y][z]);
    Last edited by c99tutorial; 01-14-2013 at 05:24 AM.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by c99tutorial View Post
    Code:
    int sum(int rows, int cols, int a[rows][cols]);
    Declaring in this way lets you use the normal a[r][c] notation within the function. The caller of the function is responsible for providing a pointer to an appropriately-sized memory area.
    In addition to that, the called function is responsible for ensuring r and c are within appropriate bounds. There is no check of this unless the programmer implements it.

    This feature also was introduced in C99. It is not valid in previous versions of C (C89, K&R, etc).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Grumpy you are inside my head! I was about to ask how is it going to be possible to pass them and handle them???

    So, it's a new feature...
    You can do that?
    Code:
    int foo(int array[n])
    {
      //I have access to n??
    }
    ?
    Last edited by std10093; 01-14-2013 at 05:52 AM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  4. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM