pointer and array

This is a discussion on pointer and array within the C++ Programming forums, part of the General Programming Boards category; hi .. the question may sound bit absurd..but answer to it would be of gr8 help to me !! how ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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
    dra
    dra is offline
    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 12:16 AM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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 12: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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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.

Page 1 of 4 1234 LastLast
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, 02: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, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21