In C: How do you find the numbe开发者_如何学Cr of elements in an array of structs, after sending it to a function?
int main(void) {
myStruct array[] = { struct1, struct2, struct3, struct4, struct5, struct6 };
printf("%d\n", sizeof(array));
printf("%d\n", sizeof(array[0]));
f(array);
}
void f(myStruct* array) {
printf("%d\n", sizeof(array));
printf("%d\n", sizeof(array[0]));
}
For some reason the printf in main shows different results than the printf in f. My need is to know how many elements are in the array.
You can't.
You have to pass the size to the function, eg:
void f(myStruct* array, size_t siz);
Also notice that in f
array is a pointer, while in main
it is an array. Arrays and pointers are different things.
In f array
is a pointer, in main array
is an array.
You must pass that data as a separate parameter to the function. In C and C++ as soon as an array is passed to a function the array degenerates into a pointer. Pointers have no notion of how many elements are in the array they point to.
A common way to get the size is to declare the array and then immediately get the array element count by dividing the total size by the size of one element. Like this:
struct my_struct my_struct_array[] = {
{"data", 1, "this is data"},
{"more data", 2, "this is more data"},
{"yet more", 0, "and again more data"}
};
const size_t my_struct_array_count = sizeof(my_struct_array)/sizeof(my_struct_array[0]);
Note that in main(), array refers to the actual array and so sizeof() gives the required answer.
But when you pass it as function parameter,you are actually passing the address of the first element of the array which is stored in the pointer variable 'array'.
So now sizeof() gives the size of pointer variable which is why it differs from actual answer.
Possible solution can be to
1.Declare the array globally
2.Pass the array size as function parameter Hope it helps!
In the code above, function f() has no way of knowing how many elements were in your original array. It's a feature of the language and there's no way around it. You'll have to pass the length.
As the C reference says, you cannot do this unless either the last element is unique or you pass a count of array elements to the function.
you have to end the array with a special value and in called function you have to count up to that value that is how strlen() works it counts up to NULL '\0' value.
You can't tell number of elements in an array in C consistently. Specially if you pass the array around through pointers.
Usually, if you must use array size in a function, pass it as a parameter to it.
When you use sizeof
in main
, it's evaluating the array, and gives the size of the actual array.
When you use sizeof
in f
, you've passed the name of the array as an argument to a function, so it has decayed to a pointer, so sizeof
tells you about the size of a pointer.
Generally speaking, if you pass an array to a function, you need to either write the function to only work with one specific size of array, or explicitly pass the size of array for it to work with on a particular invocation.
You may use a format for your array. I am using string elements, it should work for struct.
#define NULL ""
#define SAME 0
static char *check[] = {
"des", "md5", "des3_ede", "rot13", "sha1", "sha224", "sha256",
"blowfish", "twofish", "serpent", "sha384", "sha512", "md4", "aes",
"cast6", "arc4", "michael_mic", "deflate", "crc32c", "tea", "xtea",
"khazad", "wp512", "wp384", "wp256", "tnepres", "xeta", "fcrypt",
"camellia", "seed", "salsa20", "rmd128", "rmd160", "rmd256", "rmd320",
"lzo", "cts", "zlib", NULL
}; // 38 items, excluding NULL
in main ( )
char **algo = check;
int numberOfAlgo = 0;
while (SAME != strcmp(algo[numberOfAlgo], NULL)) {
printf("Algo: %s \n", algo[numberOfAlgo++]);
}
printf("There are %d algos in the check list. \n", numberOfAlgo);
You should get the output:
Algo: des
:
:
Algo: zlib
There are 38 algos in the check list.
Alternatively, if you do not want to use the NULL , do this instead:
numberOfAlgo = 0;
while (*algo) {
printf("Algo: %s \n", *algo);
algo++; // go to the next item
numberOfAlgo++; // count the item
}
printf("There are %d algos in the check list. \n", numberOfAlgo);
As an example to your solution:
Given
struct contain {
char* a; //
int allowed; //
struct suit {
struct t {
char* option;
int count;
} t;
struct inner {
char* option;
int count;
} inner;
} suit;
};
// eg. initialized
struct contain structArrayToBeCheck[] = {
{
.a = "John",
.allowed = 1,
.suit = {
.t = {
.option = "ON",
.count = 7
},
.inner = {
.option = "OFF",
.count = 7
}
}
},
{
.a = "John",
.allowed = 1,
.suit = {
.t = {
.option = "ON",
.count = 7
},
.inner = {
.option = "OFF",
.count = 7
}
}
},
{
.a = "John",
.allowed = 1,
.suit = {
.t = {
.option = "ON",
.count = 7
},
.inner = {
.option = "OFF",
.count = 7
}
}
},
{
.a = "John",
.allowed = 1,
.suit = {
.t = {
.option = "ON",
.count = 7
},
.inner = {
.option = "OFF",
.count = 7
}
}
}
};
in main()
printf("Number of Struct within struct array: %d \n", sizeof(structArrayToBeCheck)/sizeof(struct contain));
gives you the correct answer.
int array[10]
int Number_of_elements = sizeof(array) / sizeof(array[0]);
printf("%d", Number_of_elements);
精彩评论