getting size of int/char array

This is a discussion on getting size of int/char array within the C Programming forums, part of the General Programming Boards category; How can I get the size of a multidimensional int/char array? Code: int array[80][80]= {{1,2,3},{4,5,6,7},{8,9,10,11,12}}; If I have that as ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    25

    getting size of int/char array

    How can I get the size of a multidimensional int/char array?

    Code:
    int array[80][80]= {{1,2,3},{4,5,6,7},{8,9,10,11,12}};
    If I have that as example.

    How can I get it to return the actual size of the array, the inner arrays (not the outer)?

    Using the normal sizeOf will return just the entire size of the array as I specified [80][80] but not the actual size of the arrays.
    Code:
    int myLength = sizeof(array)/sizeof(int);

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,624
    If you mean that, in your case, you want to compute the size of the array with elements 1,2,3 as 3 rather than 80, then you can't.

    The compiler does not keep track of such information for you. You will need to keep track of the sizes manually (i.e. store the value 3 in some variable you can use).
    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, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    25
    Oh ok, than I think I know a good way to do it.

    Can you btw generate or make them dynamically? After compilation, while it is running. Or is that also not possible?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C99 allows for variable length arrays: https://en.wikipedia.org/wiki/Variable-length_array


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,624
    And, if you don't want to use VLAs (eg your compiler is C89 compliant rather than C99 compliant) use dynamic memory allocation (malloc(), free(), etc). It takes a bit more work than one-dimensional arrays but, if you remember that a 2-dimensional array is an array of 1-dimensional arrays, it is not too hard.
    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, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (char) array initialization, size etc.
    By cnewbie1 in forum C Programming
    Replies: 6
    Last Post: 06-21-2011, 07:08 AM
  2. Replies: 17
    Last Post: 10-31-2007, 09:43 AM
  3. dynamically setting size of 2d char array
    By waxydock in forum C Programming
    Replies: 4
    Last Post: 05-13-2007, 11:58 PM
  4. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 08:23 PM
  5. char array size question, please help!
    By Ash1981 in forum C Programming
    Replies: 4
    Last Post: 01-29-2006, 02:30 AM

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