0

I have a character pointer of hex values like as follows. a= {0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x00}; I have to find the length of the above array. But strlen gives 2 and sizeof gives 4(char pointer size). How can I find the length? I receive the character array from a socket.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Nayan
  • 49
  • 6
  • 2
    What marks the end of the array? – Ed Heal Nov 05 '16 at 14:59
  • 2
    You're probably getting the number of bytes read as a return value from the read-like function. – krzaq Nov 05 '16 at 15:00
  • 1
    If you receive it from a socket you know how many bytes you have received. Just iterate over them and count the 0s. – Matthias247 Nov 05 '16 at 15:00
  • @Matthias247: "count the 0s": what for ? –  Nov 05 '16 at 15:08
  • It depends on the problem. For example, if you know that there are only positive values, you can mark the end of array with -1 or something and then make a function to find the length. – zuko32 Nov 05 '16 at 15:08
  • @zuko32: I doubt that the OP has control over what he *receives*. –  Nov 05 '16 at 15:09
  • You can't find the size of the array that the pointer is pointing to. More info in [this SO post](http://stackoverflow.com/questions/492384/how-to-find-the-sizeofa-pointer-pointing-to-an-array). –  Nov 05 '16 at 15:10
  • @YvesDaoust: Sorry, I though that was the question and not finding the length. If the length is required - he should already know how much he has received. – Matthias247 Nov 05 '16 at 15:10
  • 2
    Show real code. `a = {0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x00};` is incomplete, but the size of the "array" is clearly 7. – Pete Becker Nov 05 '16 at 15:11
  • @Matthias247: no worries. Actually, the "length of the array" cannot be revealed by the content, even with a reserved end delimiter (unless known to be the last array element.) –  Nov 05 '16 at 15:13
  • I know. But he should nevertheless know it. E.g. from the return value of the `read` call to the socket. – Matthias247 Nov 05 '16 at 15:16
  • How do you declare the array? – Galik Nov 05 '16 at 15:38
  • I don't understand, why do you add the `C++` tag if you are asking about the C language (per your title)? – Thomas Matthews Nov 05 '16 at 18:57

2 Answers2

5

How can I find the length?

You can't "find" the length of a buffer using just the pointer. Either you must know the length beforehand, or the buffer must be terminated by some value. As you have described, the buffer is not value-terminated, so you must know the length.

I receive the character array from a socket.

If you used ssize_t read(int fd, void *buf, size_t count); to read from the socket, then you "find" the length of received data from the value that the function returns.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

The answer must be, "it depends".

When you have an array and you're not sure how many elements it has, in general there are two ways to handle the situation:

  1. Have a second variable holding the count.
  2. Use a special "sentinel" value to mark the end.

In C, a string is (by definition) an array of characters, with the special value '\0' (the "null character") as a sentinel to mark its end. So, by definition, it is impossible for a string in C to contain a null character.

Since your array is of arbitrary bytes, any of which might be a 0, your array is not a string in the C sense. So it's likely that the right way to keep track of the length is with a second variable holding the count. SInce you said that your array was read from a socket, you need to capture the return value of the read, recv, or recvfrom call you used, since that tells you how many character were read.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103