Thread: pointer to multidimensional array

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    pointer to multidimensional array

    Hi all,

    what is the correct method of declaring pointer to multidimensional array
    Code:
    int array[3][4];
    int *p = array;
    The above code works but it shows warning. Is it an invalid way of using the pointer? the array is an address then why am i getting warning?

    Thanks and regards,
    Satya

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    An array is more than just an address. Rather, an array is a contiguous sequence of objects of the same type. Hence, the array itself has a type, i.e., a combination of the type of an element of the array and the number of elements in the array.

    In many contexts, an array is converted to a pointer to its first element. So, an array of 3 arrays of 4 ints is converted to a pointer to an array of 4 ints. Therefore, you should declare and initialise such a pointer:
    Code:
    int array[3][4];
    int (*p)[4] = array;
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-02-2009, 03:07 AM
  2. Pointer to multidimensional array
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 11:17 AM
  3. Problems with pointer to multidimensional array
    By esmeco in forum C Programming
    Replies: 3
    Last Post: 03-08-2008, 06:40 PM
  4. pointer to multidimensional array..
    By zoso123 in forum C Programming
    Replies: 8
    Last Post: 06-28-2006, 11:52 AM
  5. pointer to multidimensional array
    By Bigbio2002 in forum C++ Programming
    Replies: 4
    Last Post: 02-05-2006, 10:29 PM