Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 2 months ago.
What use the Malloc Function ?
I want use malloc, but I have an error : Error: A value of type "void *" cannot be assigned to an entity of type "mesure *" in Why ? Thank
My program :
DigitalOut led1(LED1); DigitalOut led2(LED2); SPI dac(p11, p12, p13); mosi, miso, sclk DigitalOut cs1(p21), cs2(p22), cs3(p27), cs4(p25), csadc(p26); clk(p30); int val = 0, signe, gain, adc=0, numdac=0, i=0, j=0, nb=0;
struct mesure { int val; int signe; int gain; struct mesure *next; }*head, *tail, *read;
typedef struct mesure m;
FILE * fp ;
int main() { fp=fopen("/sd/louise/DAC.txt","r"); if (fp==NULL) { printf("not file \n"); } fseek(fp, 0, SEEK_SET);
cs1 = 1; cs2 = 1; cs3 = 1; cs4 = 1;
dac.format(16,1); dac.frequency(10000000);
while (fscanf(fp, "%d\t%d\t%d", &val, &signe, &gain)!=EOF) {
if (i==0) { head=malloc(sizeof(m)); head->val=val; head->signe=signe; head->gain=gain; head->next=NULL; tail=head; i=1; } else { i++; tail->next=malloc(sizeof(m)); tail=tail->next; tail->val=val; tail->signe=signe; tail->gain=gain; } nb++; } printf("%d ", nb); printf("%d ", val); wait(2);
}
Question relating to:
1 Answer
10 years, 2 months ago.
Hi Louise, You need to convert the data type before assign it .
head= (m*) malloc(sizeof(m));
Put your code within
posted by Erik - 25 Jun 2014<<code>> and <</code>>
on seperate lines to make it readable.