N
nzanella
Hello,
I just thought I would share the following observation with the rest
of the group. The sizeof operator seems to act differently according
to whether the number of elements in the array is known. Hence when
passing arrays to functions the number of elements in the array must
always be passed as an argument. If STL is available then people can
just use vectors of course. Anyways, I guess this stuff is pretty
standard. Well, have a nice day,
Neil
#include <cstdio>
void hello(const char *foo[]) {
printf("%u\n", sizeof(foo)/sizeof(char));
}
int main() {
const char *foo[8];
const char *bar[] = { "aa", "bb" };
printf("%u\n", sizeof(foo)/sizeof(char));
//BTW sizeof(char) is always going to be one
printf("%u\n", sizeof(bar)/sizeof(char));
//BTW sizeof(char) is always going to be one
hello(foo);
}
Output:
$ ./a.out
32
8
4
I just thought I would share the following observation with the rest
of the group. The sizeof operator seems to act differently according
to whether the number of elements in the array is known. Hence when
passing arrays to functions the number of elements in the array must
always be passed as an argument. If STL is available then people can
just use vectors of course. Anyways, I guess this stuff is pretty
standard. Well, have a nice day,
Neil
#include <cstdio>
void hello(const char *foo[]) {
printf("%u\n", sizeof(foo)/sizeof(char));
}
int main() {
const char *foo[8];
const char *bar[] = { "aa", "bb" };
printf("%u\n", sizeof(foo)/sizeof(char));
//BTW sizeof(char) is always going to be one
printf("%u\n", sizeof(bar)/sizeof(char));
//BTW sizeof(char) is always going to be one
hello(foo);
}
Output:
$ ./a.out
32
8
4