passing multidimensional array to function leads to incorrect size detection

This is a discussion on passing multidimensional array to function leads to incorrect size detection within the C Programming forums, part of the General Programming Boards category; HI, I am trying to pass a multidimensional array to a function with the following code: Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Question passing multidimensional array to function leads to incorrect size detection

    HI,

    I am trying to pass a multidimensional array to a function with the following code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void printSize (char x[][3][3]) {
    	printf("size of x: %d\n", sizeof(x)); //4, why???
    	return;
    }
    
    int main(int argc, char **argv)
    {
    	int i,j,k;
    	char p[6][3][3];
    	for(i=0;i<6;i++) for(j=0;j<3;j++) for(k=0;k<3;k++) p[i][j][k] = (char)i;
    	printf("size of p: %d\n", sizeof(p)); //54, as expected
    	printSize(p);
    	return 0;
    }
    The code compiles without any errors or warnings, but when run the output is:

    Code:
    size of p: 54
    size of x: 4
    Can anyone tell me what is wrong with my code, and - more importantly - how to solve it?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It is your responsibility, when writing a function that can be used with arrays of any size, to also accept as a parameter the size of the array. Arrays are passed to functions as pointers to the first element, and the size information is lost at that time.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  4. #4
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    I don't know what you need your code for, but I recently did work with something like this.

    Instead of passing the size (I find that kind of annoying) I made space and inserted NULLs at the end of the memory. That way I could work with the memory without knowing the size (I could loop through it until I hit the NULL etc). This is called using a sentinel value.

    Sentinel value - Wikipedia, the free encyclopedia

    However, passing the size is probably the most common approach.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    Quote Originally Posted by \007 View Post
    However, passing the size is probably the most common approach.
    Oh, I dunno .....

    The C string functions such as strcpy(), strcat(), etc are fairly commonly used, and rely on a sentinel value.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Those are some very specialized examples. They relay on the fact that a string has to be null-terminated to count as one in C.

    In my, admittedly, limited experience, passing around size is the way that libraries do it. Thus I follow suit.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 2noob2banoob View Post
    HI,

    I am trying to pass a multidimensional array to a function with the following code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void printSize (char x[][3][3]) {
    	printf("size of x: %d\n", sizeof(x)); //4, why???
    	return;
    }
    
    int main(int argc, char **argv)
    {
    	int i,j,k;
    	char p[6][3][3];
    	for(i=0;i<6;i++) for(j=0;j<3;j++) for(k=0;k<3;k++) p[i][j][k] = (char)i;
    	printf("size of p: %d\n", sizeof(p)); //54, as expected
    	printSize(p);
    	return 0;
    }
    The code compiles without any errors or warnings, but when run the output is:

    Code:
    size of p: 54
    size of x: 4
    Can anyone tell me what is wrong with my code, and - more importantly - how to solve it?
    Because you passed in a pointer to an array and asked how big the pointer is.

    The solution is to hand your function the size of the array which is only detectable in main().

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Smile Problem solved

    So size information is not passed with the array, which actually makes sizeof meaningless. Thanks for that information, it helped me solve my problem.
    In fact the sizeof was debugging info I wanted after the following line unexpectedly returned:
    Code:
    if(sizeof(x)!=54) return NULL;
    You have learnt me that it was actually quite useless, right now I have created a struct in which the size is hard coded so the function can't receive invalidly sized arrays anymore.
    Problem solved

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    Quote Originally Posted by 2noob2banoob View Post
    So size information is not passed with the array, which actually makes sizeof meaningless.
    No. sizeof has a defined meaning. You made a mistake in assuming it has a different meaning than it actually does.

    Your actual mistake is based on three interrelated errors on your part;

    1) you forgot that a pointer and an array are actually not the same thing

    2) you forgot that, when an array is passed to a function, the function only receives a pointer, not the array (and not complete information about the array, either).

    3) you assumed that sizeof(a_pointer) gives the size of whatever a_pointer points at.

    Admittedly, a lot of people make these same mistakes. Quite a few texts about C make exactly the same errors you did.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

Tags for this Thread


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