Thread: pointer and array

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    32

    pointer and array

    hi ..
    the question may sound bit absurd..but answer to it would be of gr8 help to me !!
    how can i pass a 2D array to a function whose argument needs a 2D pointer...
    e.g.
    i want to pass a
    Code:
    char buff[4][4];
    to a function
    Code:
    void fun(char **inpbuff, int row, int col) {
    int i,j;
    /*do some operation here on all elements of the array passed. e.g.*/
    for(i = 0; i < row; i++)
        for (j = 0; j  <= col; j++ )
            inpbuff[i][j] = 'a' + i + j;
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, doesn't work (we've had a discussion on that before, how an array is basically a pointer). I believe this does:
    int func(int two_d[][4])
    Note: just like array syntax, but specify outer dimension (columns).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    how an array is basically a pointer
    It is because array is not a pointer. It is casted to pointer when needed. The automatic cast in opposite direction is not available
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I disagree. An array is a pointer. Do you want to argue?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    casting it should work....

    edit: nevermind it doesn't =(
    Last edited by dra; 02-12-2008 at 01:16 AM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    I disagree. An array is a pointer. Do you want to argue?
    You could read again the thread... 2 types having different size could not be the same.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An array requires a special pointer syntax that specifies the size of the memory to be allocated, thus the compiler can also see the actual size of the array inside the function it's defined. When passed to another function, its true nature shows as a pointer and you can't get the size.
    I could well argue about arrays beings pointers, but this is not the place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Elysia View Post
    I disagree. An array is a pointer. Do you want to argue?


    You are confused, you need help.

    An array and a pointer are DIFFERENT. You have a serious misconception.

    An array is a collection of homogeneous objects stored in contiguous memory.

    A pointer contains a reference to an object or function.

    THERE IS NO SIMILARITY THERE!!

    Your confusion, which is common, comes from all the cases where arrays decay to a pointer to their first element. There are only a handful of cases where they do not (sizeof operator, ++, &, that's all I can think of off the top of my head).

    An array of arrays decays to a pointer to its first element, which is another array, so the type is "pointer to array" NOT A POINTER TO POINTER!!

    That is also why casts DO NOT WORK!

    Stop posting in threads like this until you understand what you are doing with pointers, arrays, and indirect addressing. There is already enough confusion and misunderstanding.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What's the big deal? I know fully what pointers and arrays are. No misconception.
    However, believe what you want, I don't think there is anything "right."
    Believe what makes it easier for you.
    Last edited by Elysia; 02-12-2008 at 01:47 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    pointers work just like arrays.
    You know they do not - look at the first post

    array is r-value, pointer is l-value. You see the differencies, but still try to argue...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The whole l-value, r-value stuff is confusing, I'll give you that.
    No matter, this isn't going anywhere, so let's all just think what we want and get along.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Elysia View Post
    The whole l-value, r-value stuff is confusing, I'll give you that.
    No matter, this isn't going anywhere, so let's all just think what we want and get along.
    That's the spirit! Heaven forbid that you actually learn something. Hard-headed ignorance is bliss. /sarcasm

    technosavvy, if you don't want to change your function (especially if you need to use it on arrays of different sizes), you can also use an intermediate array of pointers:

    Code:
    char buff[4][4];
    
    char *pb[4];
    for ( int i = 0; i < 4; ++i )
        pb[i] = buff[i];          // use the automatic array-to-pointer conversion
    
    fun(pb, 4, 4);
    
    ...
    
    void fun(char **inpbuff, int row, int col) {
    int i,j;
    /*do some operation here on all elements of the array passed. e.g.*/
    for(i = 0; i < row; i++)
        for (j = 0; j  <= col; j++ )
            inpbuff[i][j] = 'a' + i + j;
    }
    Don't mind my blustering, Elysia's tip about changing the parameter type is also correct, even though the explanation was wrong. Choose what works best for you, but consider using a vector instead of an array altogether.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whoie View Post
    That's the spirit! Heaven forbid that you actually learn something. Hard-headed ignorance is bliss. /sarcasm
    There's no need to learn anything here. I know what arrays and pointers are and the whole ordeal that arrays decay into pointers is just absurd, so I'm sticking with my way of thinking.
    There's a reason for everything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    An array is a pointer.
    An array is not a pointer, and the C++ Standard says so.

    Do you want to argue?
    You are arguing against the C++ Standard. There is nothing to argue, since what you state is factually incorrect.
    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

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Even if it may sound rude or arrogant, I don't care what the standard says. I believe in assembly and assembly says it's a pointer.
    This silly discussion will get nowhere, so why bother?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM