1

wondering how would I find the length of char array . for example

char buffer[20];
buffer[0] = 0x01;
buffer[1] = 0x02;
buffer[3] = 0x00;
buffer[4] = 0x04;
buffer[5] = 0x01;
buffer[6] = 0x02;
buffer[7] = 0x00;
buffer[8] = 0x04;

std::cout << "the len of array = "<< strlen(buffer) << std::endl;

o/p = the len of array = 3

expected o/p = 8

NOw issue is zeros can occur anywhere in the array of character elements.and I need true len i.e 8

M.M
  • 138,810
  • 21
  • 208
  • 365
samprat
  • 2,150
  • 8
  • 39
  • 73
  • 1
    `std::end(buffer) - std::begin(buffer);`. – juanchopanza Jul 01 '14 at 22:10
  • @juanchopanza , my god you were lightening fast. Thanks again mate for helping – samprat Jul 01 '14 at 22:11
  • @juanchopanza Where does `std::end(buffer)` actually go? Just read about the OP's expectations again. – πάντα ῥεῖ Jul 01 '14 at 22:11
  • @juanchopanza: That gives you the size of the buffer (20), not the number of characters that have been stored in it (9), which is what the OP appears to want. – Chris Dodd Jul 01 '14 at 22:13
  • @ChrisDodd OP is asking for the length of the array in two places. If they want something else, they should ask a different question, or clarify this one. – juanchopanza Jul 01 '14 at 22:14
  • @juanchopanza The question is about a misconception here :P ... – πάντα ῥεῖ Jul 01 '14 at 22:16
  • 1
    @juanchopanza: He does request an expected output of 8, which is admittedly non-sensical as he stores 9 characters in the buffer. – Chris Dodd Jul 01 '14 at 22:17
  • 1
    @ChrisDodd: He actually stores 8 characters, though not contiguously. – Benjamin Lindley Jul 01 '14 at 22:19
  • @juanchopanza, After reading comment I realised your sol provided above is not what i want. I wanted to know len of buffer rather than size of buffer – samprat Jul 01 '14 at 22:20
  • 1
    Your question actually makes no sense. You need to clarify what you want to do. – juanchopanza Jul 01 '14 at 22:21
  • 2
    @juanchopanza You and I know that you can't distinguish between data directly written to the array and the uninitialized data. The OP doesn't, so the question makes sense to him. – Roddy Jul 01 '14 at 22:24
  • @Roddy Not sure how one goes from "data stored in something" to "length of something" ("how big is my fridge" vs. "how many beers do I have in my fridge".) Anyway, looks like an XY problem. – juanchopanza Jul 01 '14 at 23:04

3 Answers3

3

Arrays in C (or C++) don't keep track of how much data has been stored in them. If you want to know the size of the stored data (as opposed to the size of the array), you'll need to track that yourself in another variable, or use a sentinel (such as NULL) that marks the end of the stored data.

Alternately, since you appear to be using C++ (despite the C tag), you can use a std::vector or std::string which tracks the size for you.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • thanks for reply. The issue is I want to copy structure into char * . now my structure contains sub_structure plus std::vector having sturctures as there element. Now I cant use memcpy directly as struct have vectors so That why I am copying element from my main structure to buffer array and then after 8th element i will use something like memcpy( &buffer[9], &main.struct.struct_in_vec[0], sizeof(struct_in_vec) bla bla – samprat Jul 01 '14 at 22:27
  • Note that the stored data does not seem to be contiguous, making the use of the term "length" in the question even more nonsensical. – juanchopanza Jul 01 '14 at 23:02
1

In C++ you would use a std::vector<char> or a std::string. Both store the length independently of the data so can hold zeros in them.

Beware that 'c' style literal strings are always zero terminated, so the following code gives you an empty string because the NUL terminates the string construction early.

std:string foo("\0Hello, world!");
Roddy
  • 66,617
  • 42
  • 165
  • 277
0

When you have an array, you can use sizeof to get the total memory used by the array.

char buffer[20];
// 
// ...
//

size_t size = sizeof(buffer); // This gives you total memory needed to hold buffer.
size_t length = sizeof(buffer)/sizeof(char); // In this case size and length will be
                                             // same since sizeof(char) is 1.

If you have an array of other types,

int buffer[20];
// 
// ...
//

size_t size = sizeof(buffer); // This gives you total memory needed to hold buffer.
size_t length = sizeof(buffer)/sizeof(int); // The length of the array.

There are pitfalls to be aware of using sizeof to get the memory used by an array. If you pass buffer to a function, you lose the ability to compute the length of the array.

void foo(char* buffer)
{
   size_t size = sizeof(buffer); // This gives you the size of the pointer
                                 // not the size of the array.
}

void bar()
{
   char buffer[20];

   // sizeof(buffer) is 20 here. But not in foo.

   foo(buffer);
}

If you need to be able to compute the length of the array at all times, std::vector<char> and std::string are better choices.

void foo(std::vector<char>& buffer)
{
   size_t size = buffer.size() // size is 20 after call from bar.
}

void bar()
{
   std::vector<char> buffer(20);
   size_t size = buffer.size() // size is 20.

   foo(buffer);
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270