简体   繁体   中英

How do you find the size of an array within an array in C?

I'm working on a leetcode problem where you have to iterate through an array that contains other arrays of various lengths. I'm trying to find a way to get the length of the array within the array so that I can iterate through that array.. So far I've tried things like:

size = sizeof(array) / sizeof(int), size = sizeof(array) / sizeof(array[i]), etc..

But each time I get an integer value that seems unrelated to the array itself. I remember doing something like this before, but I can't remember how I did it.. Does anyone know the best way to accomplish something like this?

tl;dr:

Say we are given an **array = [[0,1,2],[0],[]] How would I find the sizes of the individual arrays [0,1,2], [0], and [] within the given array of arrays? Thanks:)

To make my comment an answer:

With a ragged array like **array = [[0,1,2],[0],[]] (pseudo-code, but we can infer what it means) there's no way of retrieving the sizes of each sub-array since that information doesn't exist at runtime. **array is just a pointer to some memory of three addresses to some integers.

The most minimal solution would be to have pointers to slightly "smarter" objects such as

struct IntArray {
  unsigned int count;
  int *data;
}

but that might not be possible for your Leetcode problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM