char array function call

unsigned char str_1[] = {0x50,0x60,0x0,0x70,0x0,0x95,0x0,0x14,0x0,0x35,0x54,0x0};

i want to pass the str_1 to some functions.

if i pass the function , and check the sizeof(str_1) is mismatch.
any one have idea?
Last edited on
Without seeing your code it's impossible to be sure, but you probably didn't pass the array, but passed a pointer instead
This prints the same size:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
template<class T>
void print_size(const T& a) {
    std::cout << "sizeof a in print_size: " << sizeof a << '\n';
}
int main() {
    unsigned char str_1[] = {0x50,0x60,0x0,0x70,0x0,0x95,0x0,0x14,0x0,0x35,0x54,0x0};
    std::cout << "sizeof str_l in main: " << sizeof str_1 << '\n';
    print_size(str_1);
}


Why do you use a C style array instead of std::vector<unsigned char> in the first place, though? It's considerably simpler to do using C++ concepts:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <vector>
void print_size(const std::vector<unsigned char>& a) {
    std::cout << "size in print_size: " << a.size() << '\n';
}
int main() {
    std::vector<unsigned char> str_1 = {0x50,0x60,0x0,0x70,0x0,0x95,0x0,0x14,0x0,0x35,0x54,0x0};
    std::cout << "size in main: " << str_1.size() << '\n';
    print_size(str_1);
}
Last edited on
Maybe like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
void some_func(unsigned char str[], unsigned int size)
{
  for (unsigned i = 0; i < size; i++)
  {
    printf("%d ", str[i]);
  }
}

int main()
{
  unsigned char str_1[] = { 0x50,0x60,0x0,0x70,0x0,0x95,0x0,0x14,0x0,0x35,0x54,0x0 };
  some_func(str_1, sizeof(str_1));
}


OUTPUT
80 96 0 112 0 149 0 20 0 53 84 0 

@thomas
thaks for easy way to achive
@cubbi
thanks. you give good suggestion. i ll keep in mind

Kuluoz.
Last edited on
Topic archived. No new replies allowed.