-5

If I need to find out length of a array with repeated '\0' character, what should I do? strlen won't be of use as it will simply stop with '\0'. In that case what is the best solution? For example I have a buf; now I don't know the length. I need to find out the length so that I can read whole data in it.

EDIT:

unsigned char buf[4096];

This buf has '\0' character in it. But it occurs in between data.I need to read the data even with '\0' character. strlen won't solve the purpose. So what is the way? This is the part question from here : lzss decoding EOF character issue

Code is there. Please look at it.

Community
  • 1
  • 1
ninja.stop
  • 410
  • 1
  • 10
  • 24
  • how have you declared array? – Iłya Bursov May 14 '17 at 06:48
  • yes I did @Lashane – ninja.stop May 14 '17 at 06:51
  • If you really have an array, you can simply use `sizeof(array)/sizeof(array_element)`. However, I guess that is not really what you are asking. So please post some code so that we can see/understand what you are really asking. – Support Ukraine May 14 '17 at 06:52
  • *"I have a buf"* - until we *see* it, it doesn't exist. Your question is unclear, and without code and *exact* description of intent of said-same, we can't help. – WhozCraig May 14 '17 at 06:55
  • what is buf? is it a pointer passed to you by a function? is it an array you have declared? – CIsForCookies May 14 '17 at 06:55
  • @WhozCraig please see the edit – ninja.stop May 14 '17 at 06:59
  • 2
    length of your array is 4096 – Iłya Bursov May 14 '17 at 07:01
  • 3
    Your question isn't about the size of an array; it's about the length of the *content* within, and as it is *your* content with *your* usage requirements, only *you* can know the answer to that. The size of the actual array is obvious: 4096 elements. – WhozCraig May 14 '17 at 07:02
  • So I need to read the whole 4096 ? – ninja.stop May 14 '17 at 07:03
  • The problem is, the decode function writes garbage after the whole decoding is done. the memset of the buffer with '\0' before filling the actual data doesn't help here. – ninja.stop May 14 '17 at 07:06
  • So *how* do you fill the array? Some code on this would help. – alk May 14 '17 at 07:38
  • @alk please see this : http://stackoverflow.com/questions/43960603/lzss-decoding-eof-character-issue – ninja.stop May 14 '17 at 07:39
  • 2
    @ninja.stop look at 'encode(unc_data, compr_data, &payload_len);'. I wonder what that third parameter is for.................? – ThingyWotsit May 14 '17 at 07:41
  • See my comment on the question you linked. – alk May 14 '17 at 07:43
  • @ThingyWotsit this is for upto which byte I should write in file for encoded buf ie: outfile = fopen ("file2.lzss", "wb"); while (pw != payload_len) { if (fputc(compr_data[pw], outfile) == EOF) strerror(errno); pw++; } fclose(outfile); – ninja.stop May 14 '17 at 07:44
  • ^^ exactly. Now look at your title, and your comment reply..... – ThingyWotsit May 14 '17 at 07:48
  • The actual problem is binary buffer handling which I am still searching to read. Till now what I found is binary file handling. @ThingyWotsit if you have any content on this, please let me know – ninja.stop May 14 '17 at 08:02
  • I probably didn't get it but writing a binary file is actually very easy if you use [fwrite()](http://www.cplusplus.com/reference/cstdio/fwrite/). To support Windows properly, it is a good idea to `fopen()` the file with `"wb"`. (Otherwise, the function may replace silently `\n` by `\r\n` what's not intended if data is binary. The "b" in `"wb"` disables it. AFAIK it doesn't have any effect on Unix likes - it's just ignored.) – Scheff's Cat May 14 '17 at 08:50
  • @ninja.stop keep thinking about it: 'this is for upto which byte I should write in file for encoded buf' and 'find out length of a array'. Soon, I think, a lightbulb should come on:) – ThingyWotsit May 14 '17 at 08:53

1 Answers1

0

There are 3 possibilities to determine array size in my mind:

  1. The array is declared as array. The sizeof operator can be used. (Nice, it's comile-time resolved.)

  2. The array is passed as pointer. The size cannot be determined from type. It has to be provided another way.

  3. The array length can be determined by its contents. This is used for C strings but can be used for other types also. (Consider, that the end-marker consumes an element itself. Thus, the maximum length is one less than the capacity.)

Sample code test-array-size.c:

#include <stdio.h>

/* an array */
static int a[5] = { 0, 0, 0, 0, -1 };

/* a function */
void func(int a1[], int len1, int *a2)
{
  /* size of a1 is passed as len1 */
  printf("a1 has %d elements.\n", len1);
  /* len of a2 is determined with end marker */
  int len2;
  for (len2 = 0; a2[len2] >= 0; ++len2);
  printf("a2 has (at least) %d elements.\n", len2 + 1);
}

/* HOW IT DOES NOT WORK: */
void badFunc(int a3[5])
{
  int len = sizeof a3 / sizeof a3[0]; /* number of elements */
  printf("a3 seems to have %d elements.\n", len);
}

/* the main function */
int main()
{
  /* length of a can be determined by sizeof */
  int size = sizeof a; /* size in bytes */
  int len = sizeof a / sizeof a[0]; /* number of elements */
  printf("a has %d elements (consuming %d bytes).\n", len, size);
  /* Because this is compile-time computable it can be even used for
   * constants:
   */
  enum { Len = sizeof a / sizeof a[0] };
  func(a, Len, a);
  badFunc(a);
  /* done */
  return 0;
}

Sample session:

$ gcc -std=c11 -o test-array-size test-array-size.c 
test-array-size.c: In function 'badFunc':
test-array-size.c:19:20: warning: 'sizeof' on array function parameter 'a3' will return size of 'int *' [-Wsizeof-array-argument]
   int len = sizeof a3 / sizeof a3[0]; /* number of elements */
                    ^
test-array-size.c:17:18: note: declared here
 void badFunc(int a3[5])
                  ^

$ ./test-array-size.exe 
a has 5 elements (consuming 20 bytes).
a1 has 5 elements.
a2 has (at least) 5 elements.
a3 seems to have 1 elements.

$
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56