4

Possible Duplicate:
Sizeof an array in the C programming language?

I have code of the following form:

void arrLen(char* array){
    // print length
}

int main(){
    char bytes[16];
    arrLen(bytes);
}

What I need to do is get the length of the underlying array and print it out. The issue is that the array MAY CONTAIN NULL CHARACTERS, and therefore strlen would not work. is there any way to do this, or would I have to pass it as a char[] instead of a char*?

Community
  • 1
  • 1
ewok
  • 20,148
  • 51
  • 149
  • 254

6 Answers6

6

C style arrays do not carry length information with them.

You have several solutions avaliable :

pass the size of your array to your function :

void arrLen(char* array, std::size_t size)

use std::array if you have access to C++ 11 (or TR1 as mentionned in comments)

 std::array<char, 16> bytes;

If you don't have access to std::array, you can use boost::array. They are roughly equivalent to the standard one.

Regarding your comment

would I have to pass it as a char[] instead of a char*?

Arrays are always passed to functions as pointers, so void arrLen(char* array) and void arrLen(char[] array) are equivalent.

undu
  • 2,411
  • 3
  • 21
  • 28
3

or would I have to pass it as a char[] instead of a char*?

Changing the declaration of arrLen() to take a parameter of type char[] instead of char* would have no effect, as in this scenario they are equivalent.

A possible solution would be to use a template function:

#include <iostream>

template <typename T, int N>
void array_size(T (&a)[N])
{
    std::cout << N << "\n";
}
int main()
{
    array_size("hello");
    char bytes[16];
    array_size(bytes);
    return 0;
}

See http://ideone.com/l7GxDp for demo.

Alternatives to using an array:

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Considering that `std::array` is standard as of C++11, I'd prefer seeing a reference to that as well when mentioning boost::array. – Agentlien Jan 04 '13 at 14:55
  • @Agentlien, I know that but in a comment to the question the OP states that C++11 is not available. – hmjd Jan 04 '13 at 14:57
  • Sure, but I'd still add a comment, possibly within parentheses. As such: *boost::array (available as std::array in C++11* That would help people who come to this question later and do not have that restriction. – Agentlien Jan 04 '13 at 15:08
2

In the traditional way (C) one has to pass the size as an attribute of the function

void arrLen(char* array, int maxSize){
    // print length
}

int main(){
    char bytes[16];
    arrLen(bytes, 16);
}
Helio Santos
  • 6,606
  • 3
  • 25
  • 31
1

arrays in C/C++ aren't handled that way per say, you have to manually keep track of the length yourself.. change your function to something like void arrLen(char * array, int size) and specify the size whenever you call the function.

iKlsR
  • 2,642
  • 6
  • 27
  • 45
1

If you have a C++11, may I suggest that you use std::array, it keeps track of it size.

Caesar
  • 9,483
  • 8
  • 40
  • 66
1

Unfortunately, char[] and char* parameter types are equivalent.

You have two options:

Pass the length along with the pointer like one would in C:

void arrLen(char* array, size_t length)

or use a template:

template<size_t sz>
void arrLen(char (&array)[sz])
molbdnilo
  • 64,751
  • 3
  • 43
  • 82