conversion from void to * to int*

#include <malloc.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>



#define K 3 /* constraint length */
#define TWOTOTHEM 4 /* 2^(K - 1) -- change as required */
#define PI 3.141592654 /* circumference of circle divided by diameter */

#define MSG_LEN 100000l /* how many bits in each test message */
#define DOENC /* test with convolutional encoding/Viterbi decoding */
#undef DONOENC /* test with no coding */
#define LOESN0 0.0 /* minimum Es/No at which to test */
#define HIESN0 3.5 /* maximum Es/No at which to test */
#define ESN0STEP 0.5 /* Es/No increment for test driver */


void cnv_encd(int g[2][K], long input_len, int *in_array, int *out_array) {

int m; /* K - 1 */
long t, tt; /* bit time, symbol time */
int j, k; /* loop variables */
int *unencoded_data; /* pointer to data array */
int shift_reg[K]; /* the encoder shift register */
int sr_head; /* index to the first elt in the sr */
int p, q; /* the upper and lower xor gate outputs */

m = K - 1;

/* allocate space for the zero-padded input data array */
unencoded_data = malloc( (input_len + m) * sizeof(int) );
if (unencoded_data == NULL) {
printf("\ncnv_encd.c: Can't allocate enough memory for unencoded data! ending");
exit(1);
}

/* read in the data and store it in the array */
for (t = 0; t < input_len; t++)
*(unencoded_data + t) = *(in_array + t);

/* zero-pad the end of the data */
for (t = 0; t < m; t++) {
*(unencoded_data + input_len + t) = 0;
}

/* Initialize the shift register */
for (j = 0; j < K; j++) {
shift_reg[j] = 0;
}

/* To try to speed things up a little, the shift register will be operated
as a circular buffer, so it needs at least a head pointer. It doesn't
need a tail pointer, though, since we won't be taking anything out of
it--we'll just be overwriting the oldest entry with the new data. */
sr_head = 0;

/* initialize the channel symbol output index */
tt = 0;

/* Now start the encoding process */
/* compute the upper and lower mod-two adder outputs, one bit at a time */
for (t = 0; t < input_len + m; t++) {
shift_reg[sr_head] = *( unencoded_data + t );
p = 0;
q = 0;
for (j = 0; j < K; j++) {
k = (j + sr_head) % K;
p ^= shift_reg[k] & g[0][j];
q ^= shift_reg[k] & g[1][j];
}

/* write the upper and lower xor gate outputs as channel symbols */
*(out_array + tt) = p;
tt = tt + 1;
*(out_array + tt) = q;
tt = tt + 1;


sr_head -= 1; /* equivalent to shifting everything right one place */
if (sr_head < 0) /* but make sure we adjust pointer modulo K */
sr_head = m;

}

/* free the dynamically allocated array */
free(unencoded_data);

}


When i compile the above program, i get the error saying cannot convert from 'void *' to 'int *' for this:
unencoded_data = malloc( (input_len + m) * sizeof(int) );

Thanks
Last edited on
Try explicit cast.
malloc return void pointer.

int* p = (int*) malloc( size );
should do

ps. if you plan to use c++ read about new keyword
Thanks but please can you explain where you suggest i place the explicit cast
instead of:
unencoded_data = malloc( (input_len + m) * sizeof(int) );
do:
unencoded_data = (int *) malloc( (input_len + m) * sizeof(int) );

This is the explicit cast as illustrated by @tath. Also, C is evil.
Topic archived. No new replies allowed.