I made the following piece of code to convert char to a array of bits by the function intToBits:
typedef unsigned char poly8;
typedef unsigned long long poly8x64[8];
char* intToBits(unsigned k) {
int i;
char *nk = malloc(8);
for(i=7;i>=0;i--){
nk[i] = (k%2);
k = (int)(k/2);
printf("nk= %d \n", nk[i]);
}
return *nk;
}
void poly8_bitslice(poly8x64 r, const poly8 x[64])
{
//TODO
int i;
for(i=0;i<64;i++)
{
printf("x= %d \n", x[i]);
char* mem = intToBits(x[i]);
printf("bufer= %d \n", *mem);;
}
}
int main()
{
poly8 a[64], b[64], r[64];
poly8x64 va, vb, vt;
int i;
FILE *urandom = fopen("/dev/urandom","r");
for(i=0;i<64;i++)
{
a[i] = fgetc(urandom);
b[i] = fgetc(urandom);
}
poly8_bitslice(va, a);
poly8_bitslice(vb, b);
fclose(urandom);
return 0;
}
However, while printing I'm getting a segmentation failt error. I dont know what to do. Is the pointer incorrect? I just want to print the result of intToBits.
The culprit is this line in intToBits
:
return *nk;
It returns a char
instead of a char*
, which the compiler would inform you of if you turn on compiler warnings.
Change it to:
return nk;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.