Thread: pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    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.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by c99tutorial View Post
    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]);
    Wrong. You can not pass *A.

    You pass the array as I said and you handle it normally (of course not as you say...)
    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

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by std10093 View Post
    You pass the array as I said and you handle it normally (of course not as you say...)
    No. Your example only considers single-dimensional arrays. With an n-dimensional array passed as input into a function, the compiler needs to "know" about the the size of the highest n-1 dimensions of the array. In addition, the programmer needs to know the size of the lowest dimension of the array as well as where the array begins in memory. So in total, you must design your function to accept n+1 parameters. This fact is true independent of whether you use the C99 syntax or C89 syntax. Using the C99 syntax simply lets you use indices inside the function in a natural mathematical way, i.e. "in the normal way".

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes is the answer. When you want to pass a n dimensional array you pass n stars as pointers and n dimensions. So simple.

    Also, I suppose you understood your mistake before... I guess actually it won't even compile. Tip : Compile the code before you post it

    Enough said and this thread is solved.

    Bye
    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

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    24
    files and sockets come to mind also.

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