简体   繁体   中英

how to find actual end of char array in c containing zeros in the middle

I am trying to find size of char array in c. There are zeros in between, so strlen does not give the right answer. An example scenario is below.

char buffData[256];
buffData[0] = 0x89;
buffData[1] = 0x32;
buffData[2] = 0x00;
buffData[3] = 0x02;
buffData[4] = 0x01;

How can I find the right length. ps: strlen would give 2 which is wrong

I want the answer 5 !

Even better than

int len = sizeof(buffData) / sizeof(char);

you can write more generally:

int len = sizeof(buffData) / sizeof(buffData[0]);

which will still work if you (or someone else) change the declaration of buffData , eg:

double buffData[256];

If you want to know how many actually elements are in the array at some moment in time, there are at least 2 options:

  1. keep track of elements count, each time you insert element increment the counter, decrement on element removal

     char buffData[256]; int buffData_n = 0; buffData[0] = 0x32; buffData_n++; 
  2. memset memory to zero, or zero-initialize it, so then you can count non-zero elements explicitly:

     int i = 0; for ( ; i < 256 && buffData[i]; i++); printf("There is %d elements in the array now\\n", i); 

If you want to allow for gaps filled by 0, and you can specify the size of a gap:

    int n = 0, i = 0;
    // gap size 2 max
    for ( ; i < 255; i++)
    {
        if (buffData[i]==0 && buffData[i+1]==0) break;
        if (buffData[i]) n++;
    }
    printf("There is %d elements in the array now\n", n);

Are you asking for the exact size of the array ? In this case, you can use the following:

int len = sizeof(buffData) / sizeof(char); //always 1 anyway

Note that this will not work if you pass buffData to a function as a pointer.

You can't if you don't store the size somewhere.

Otherwise how some function would know it reached the end?

For your example, let's say the array was zeroed before;

It would look like:

buffData[0] == 0x89;
buffData[1] == 0x32;
buffData[2] == 0x00;
buffData[3] == 0x02;
buffData[4] == 0x01;
buffData[5] == 0x00;
buffData[6] == 0x00;
...
buffData[255] == 0x00;
//out of buffer here
buffData[256] == 0x040;   // arbitrary values since the memory is not 
buffData[257] == 0x042;   // allocated or allocated to another variable

How would anyone know that it should stop at index 4 and not at 6, 2, 257 or any other number? Only you know that.

Now let's take another code:

char buffData[256];
buffData[0] = 0x89;
buffData[1] = 0x32;

You are expecting answer to, but in the memory could look like:

buffData[0] == 0x89;
buffData[1] == 0x32;
buffData[2] == 0x00;  // arbitrary values since not initialized
buffData[3] == 0x02;
buffData[4] == 0x01;

The memory could look exactly the same, but you're expecting different output. It's nondeterministic behavior.

You should add some assumptions, like no trailing zeroes with pre-zeroed array, counter or mark of some kind.

However sizeof can be used in order to determine the array length in your case.

in case it was defined with malloc , there is no way to know since sizeof will return the size of the pointer.

In C11 and C99 standard, strings represented by char arrays must be null terminated. So there is no way of knowing where one ends if there are 0s in between, as the only standard definition of an end of a string in c is the terminating 0. Store the length somewhere or ensure there are no 0s.

You could check the next element after the last one (after '\\0') to see if you find something there, something like this:

#include<stdio.h>

size_t size(char *s){
    size_t len = 0;

    while (s[len] != '\0'){
        len++;
    }

    while (s[len+1] != '\0'){
        len++;
    }

    return len;
}

int main(void){
    char buffData[256] = "ABC\0DE";

    size_t len = size(buffData);
    printf("The size is %zu\n",len);
    return 0;
}

Output:

The size is 5

It is just Theory anyway.

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