0

I know that this is a silly question. How do you convert an unsigned char array (with values) to a const unsigned char array?

Below is the code that I have tried. It does not make sense, but a const char does not allow you to change the values (as meant by the word const) but I need a const unsigned char array to pass to a function.

#include<stdio.h>
#include<stdint.h>
#include<stdlib.h>

int main()
{
    uint8_t a[] = {0, 10, 0, 0, 35, 45, 99, 100,88,67,34,23,56};
    unsigned char k[13];
    for(int i=0; i<sizeof(k)/sizeof(unsigned char); i++)
    {
        k[i] = a[i];
        printf("%X\t%d\n",k[i],k[i]);
    }

    // k = (const)k[]; -- cannot cast char array to const char array 

    return 0;
}

Please let me know what can be done, thanks. Below is the function declaration that k (const unsigned char *k) is to be passed.

int crypto_aead_encrypt(
    unsigned char *c, unsigned long long *clen,
    const unsigned char *m, unsigned long long mlen,
    const unsigned char *ad, unsigned long long adlen,
    const unsigned char *nsec,
    const unsigned char *npub,
    const unsigned char *k
) 
8
  • 1
    You can't convert non-const to const. Commented Aug 2, 2022 at 19:53
  • Edit the question to show the function declaration. A function that is said to take a “const unsigned char array” as an argument probably actually has a parameter whose type is const unsigned char *, a pointer to a const unsigned char. To pass your array a to it, you merely use a as an argument. An array argument will be automatically converted to a pointer to its first element, yielding an unsigned char *, and, for a properly declared function, that will be automatically converted to a const unsigned char *. Commented Aug 2, 2022 at 20:02
  • 1
    As suspected, the parameter is const unsigned char *, not an array. So simply pass a. What problem are you actually having? Commented Aug 2, 2022 at 20:46
  • @Barmar, you can absolutely cast a non-const to a const in C Commented Aug 2, 2022 at 20:58
  • @Elzaidir But you can't assign to an array, so you can't convert the arrayk from non-const to const. Commented Aug 2, 2022 at 20:59

1 Answer 1

1

const here is a qualifier meaning "this function will not alter this variable". It's a guarantee about the function's behavior, not about what you're passing in. It's ok to pass a non-const variable to a function taking a const.

For example, the following is fine. It's ok to pass a char * to const char *. main is allowed to alter the string, but foo is not.

void foo(const char *a) {
    puts(a);
}

int main()
{
    char *str = strdup("testing");
    foo(str);
}

However, the following is an error. It doesn't matter that str was not declared with const, foo promised not to modify its argument.

void foo(const char *a) {
    // error: assignment of read-only location '*a'
    a[0] = 'r';
    puts(a);
}

int main()
{
    char *str = strdup("testing");
    foo(str);
}

You should not go the other way. Passing a const to a non-const function is undefined. It might work, it might not.

// note: expected 'char *' but argument is of type 'const char *
void foo(char *a) {
    a[0] = 'r';
    puts(a);
}

int main()
{
    const char str[] = "testing";

    // warning: passing argument 1 of 'foo' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
    foo(str);
}
Sign up to request clarification or add additional context in comments.

Comments

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.