char X[3];
how to check if array X[position]
exist?
for example:
if (x[4] == True)
printf("Exists")
开发者_JAVA技巧else
printf("NONE")
Each position in a C array, within the array's bounds, always exists, so there's no need for a way to check a random position for existence. All you need to do is make sure that the index is inside the bounds.
If the array is declared with a static size, you can get the length of the array via sizeof
:
int array[30];
int length = sizeof(array) / sizeof(int);
// sizeof(array) returns the size in bytes, divide by element size to get element count
However if the array doesn't have a known length at compile time, you'll have to find it out in some other way. C functions dealing with arrays usually take in both a pointer to the first element and the size as separate arguments.
void do_something_to(int *items, int item_count);
You need to be especially careful when passing arrays to functions, since an array passed to a function becomes a "plain pointer" and the compiler loses all track of its size (sizeof will report the size of the pointer). IMHO it's least confusing to avoid array arguments altogether and just stick to pointers.
You can't do that. In C you have to store the size of the array yourself after it is declared.
C functions that deal with arrays usually have a int size
parameter that is used to specify the size of the array so the called function knows with how many elements it has to deal with.
You can't.
You'll just have to remember (possibly by passing an int
around) that the array is 3
long.
If the array declaration is visible, you can use "sizeof" and divide by the datatype size. Otherwise you're out of luck.
精彩评论