1

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
6
  • 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 *. Commented Dec 13, 2012 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**. Commented Dec 13, 2012 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. Commented Dec 13, 2012 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. Commented Dec 13, 2012 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. Commented Dec 13, 2012 at 12:28

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Also to allow it to compile, len must be declared as const.
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.

5 Comments

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?
@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.
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.
@SteveJessop: I meant rather that using const int -- and just hoping that you remembered everything else you need -- doesn't ensure that. ;)
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 ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.