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
)
const unsigned char *, a pointer to aconst unsigned char. To pass your arrayato it, you merely useaas an argument. An array argument will be automatically converted to a pointer to its first element, yielding anunsigned char *, and, for a properly declared function, that will be automatically converted to aconst unsigned char *.const unsigned char *, not an array. So simply passa. What problem are you actually having?kfrom non-const to const.