How to get the sizeof of an array in the following case:
I have written the C code as shown below,
#define SOB1 10
#define SOB2 20
char Buffer_1[SOB1];
char Buffer_2[SOB2];
char * CommandArray[2] = {Buffer_1,Buffer_2};
How to get indirect size Buffer_1
and Buffer_2
through CommandArray
? More precisely, I have to know the SOB1 or SOB2 value based on the indexchar * CommandArray[2]
source to share
In this case, you cannot do sizeof
, as the metadata of the array was lost when you started accessing it with a pointer. You will need to use sizeof(Buffer_1)
or sizeof(Buffer_2)
.
Another option (if you don't have access to Buffer_1
and Buffer_2
) would be to store a second size variable equal #define
for each buffer and use that. Since the array does not contain a string, you cannot check \0
or the like either, so you need to be very careful about buffer overflows when using them (another reason for storing a variable size).
source to share