0

Can somebody help me out with below error. Shall I cast len before I try to pass buf?

int len=2;
unsigned char tmp[len + 1];
unsigned char * buf = &tmp;

The error is:

error: cannot convert 'unsigned char (*)[(((unsigned int)((int)len)) + 1)]' to 'unsigned char*' in assignment
James M
  • 18,506
  • 3
  • 48
  • 56
  • 1
    array internal representation is as a pointer. That is in your case type of tmp is unsigned char * and type of buf is unsigned char *.So type miss match you try to assign char ** to char *. – rajesh6115 Dec 13 '12 at 11:55
  • 2
    @Rajesh: **Arrays are _not_ pointers.** The type of `tmp` is `unsigned char[3]`. Nothing else. There is no `unsigned char**` here anywhere, and certainly no `char**`. – Lightness Races in Orbit Dec 13 '12 at 12:01
  • @Lightness Races in Orbit:sorry for forgetting to add constant pointer (pointer value can not be changed) which points to a fixed memory location only. But I explain it only for type miss match Nothing else. – rajesh6115 Dec 13 '12 at 12:10
  • @Rajesh: The type mismatch is between `unsigned char*` (pointer to an `unsigned char`) and `unsigned char(*)[3]` (pointer to an array of three `unsigned chars`), not between `unsigned char*` and `unsigned char**`. Note that I _do_ ignore the VLA component and pretend that the array dimension is fixed at `3`, as it's supposed to be. – Lightness Races in Orbit Dec 13 '12 at 12:12
  • @LightnessRacesinOrbit : I don't for c++ I say all above things(type mismatch according to error highlighted ) . And error come only for the use of '&' address of operator only. This discussion can go to a bit longer but user may divert and confuse also. and I provide the simplest answer to the problem. You can ans this in a longer way. – rajesh6115 Dec 13 '12 at 12:28
  • @Rajesh: I choose to answer it in the correct way. – Lightness Races in Orbit Dec 13 '12 at 12:39

2 Answers2

3

If you just want a pointer to the array, use

unsigned char * buf = tmp;

By the way, the way you declare tmp makes it a variable-length array (VLA). Technically, these are not allowed in C++, although many compilers support VLAs as an extension.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

The problem is not len. The problem is that you are trying to take the address of the array, rather than the address of the first element of the array; the types simply don't match.

Instead:

unsigned char* buf = &tmp[0];

Also, the array name conveniently (FSVO "conveniently") decays to &tmp[0] anyway, so you could write simply:

unsigned char* buf = tmp;

In addition you are presently not using a constant value for the array's dimension — ensure that len is made const and initialised with a constant value. In C++11, this'd better be constexpr to really ensure that you're not accidentally attempting to use GCC VLAs.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Ooh, what case does C++11 break so that you have to add `constexpr` rather than just having a `const` int with a visible initializer that's an ICE? – Steve Jessop Dec 13 '12 at 12:43
  • @SteveJessop: I'm not aware of any, but surely `constexpr` is preferable by its nature? If you always ensure you have a visible initializer then great, but asking the OP to remember to do this seems like a bit of a reach. No offence, OP. – Lightness Races in Orbit Dec 13 '12 at 13:25
  • ah, I was concentrating on "really ensure" rather than "better". I thought you meant that `const int` in C++11 doesn't really ensure that. AFAIK you're right that `constexpr` is a better way of expressing it in C++11. – Steve Jessop Dec 13 '12 at 13:26
  • @SteveJessop: I meant rather that using `const int` -- and just hoping that you remembered everything else you need -- doesn't ensure that. ;) – Lightness Races in Orbit Dec 13 '12 at 13:27
  • Yes, it makes a bigger difference with `const int a = foo();` where `foo()` may or may not be a `constexpr` function. `constexpr int a = foo();` checks that it is. With `const int a = 4;` I'm reasonably confident I can verify by eye that `4` is a constant ;-) – Steve Jessop Dec 13 '12 at 13:28