Thread: pointers

  1. #16
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by std10093 View Post
    Code:
    int foo(int array[n])
    {
      //I have access to n??
    }
    Not, in this case, unless `n' is a compile-time constant (or a macro, that somehow expands to a compile-time constant).
    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.

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

  3. #18
    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".

  4. #19
    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

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

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by std10093
    When you want to pass a n dimensional array you pass n stars as pointers and n dimensions. So simple.
    Not quite:
    Quote Originally Posted by c99tutorial
    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.
    That is, if you pass an array of arrays as an argument, it is converted to a pointer to an array, not a pointer to a pointer.
    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. #22
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Well, you are correct. I had in mind the dynamic allocation of arrays
    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