0

I have a declaration of unsigned char * for the sake of an encryption key:

unsigned char *key = (unsigned char *)"0123456789012345";

I want to make it so that the key is all 0 (not the ASCII character ‘0’).


I'm a bit rusty with C, so I'm declaring it like this:

unsigned char *iv = (unsigned char *){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

Which is giving me warnings, so how can I do this correctly?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
bli00
  • 2,215
  • 2
  • 19
  • 46
  • or `unsigned char *iv = (unsigned char []){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};` but it would be very strange code. – Stargateur Apr 16 '18 at 18:22
  • I wonder why those questions are still asked ... and answered by users with gold badge who should know better... – Jean-François Fabre Apr 16 '18 at 18:40
  • Is the unfriendly attitude necessary? @Jean-François Fabre If it's duplicated then simply refer me to it – bli00 Apr 16 '18 at 18:45
  • The unfriendly attitude is not particularly directed to you. You could have googled it, but you don't have C gold badge, so that's excusable. – Jean-François Fabre Apr 16 '18 at 18:46
  • How about `const unsigned char *key = (const unsigned char *)"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";` or the like? Do you want to changed `key` later in code? – chux - Reinstate Monica Apr 16 '18 at 18:51
  • @Jean-FrançoisFabre I'm searching for the past 10 minutes I didn't find good duplicate for this question cause OP use a pointer and miss his component literal initialization with an array... I think actually this question don't have duplicate. The duplication you find don't answer this question. – Stargateur Apr 16 '18 at 18:56
  • but the link I provided solves the question the same way the answers below solve it. So if it's not a duplicate, the answers below are moot. – Jean-François Fabre Apr 16 '18 at 18:58
  • @Jean-FrançoisFabre There are bad answers in my opinion except Vlad answer, that mention compound literal. – Stargateur Apr 16 '18 at 19:00
  • yes, the wierd but valid syntax is something new. OP didn't ask that much. – Jean-François Fabre Apr 16 '18 at 19:03

3 Answers3

4

You could just write

unsigned char iv[16] = { 0 };

As for this declaration

unsigned char *iv = (unsigned char *){0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

that tries to use a compound literal then its valid record will look like it is shown in the demonstrative program

#include <stdio.h>

int main(void) 
{
    enum { N = 16 };
    unsigned char *iv = ( unsigned char[N] ){ 0 };

    for ( size_t i = 0; i < N; i++ ) printf( "%d ", iv[i] );
    putchar( '\n' );

    return 0;
}

Its output

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

You have to allocate memory for storage:

unsigned char iv [16];
memset (iv, 0, sizeof iv);

Alternatively:

unsigned char *iv = calloc (16);  // allocates and initializes to NUL
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • I believe `calloc` is also safer since the key won't be allocated at the same relative offset to some other variable. But note that if `calloc` is used, then `free` must also be used in most cases to avoid a memory leak. – Jeff Learman Apr 16 '18 at 18:44
-1

I would recommend using the memset() command.

memset(iv, 0, sizeof(*iv))

Edit: my mistake, accidentally left out the star for iv