tags:

views:

365

answers:

3

Hi

Lets say I have an array numbers that contains the following values:

int numbers = [12, 511, 337, 254];

Now, I would like to scale those numbers into single byte values and store them in a char array

char numbersscaled;  

for(i=0; i<4; i++) {  
    numbersscaled[i] = numbers[i]/2;  
}

Finally, I would like to write those values as a binary file as follows:

filebin = fopen("results.bin", "wb");  

if(file==NULL) {   
    printf("Error\n");  
    return 1;  
}  

fwrite(numbersscaled, sizeof(numbersscaled[0]),
         numbersscaled/numbersscaled[0], filebin);  

fclose(filebin);

Unfortunately, when trying to compile this program gcc does not like the fwrite command:

error: invalid operands to binary / (have ‘char *’ and ‘int’)

Anyone an idea what I am missing? Thanks!

+3  A: 

Yes, you're missing sizeof.

sizeof(numbersscaled)/sizeof(numbersscaled[0])

Note that there are many typos in your question (missing [], etc), making it hard to detect the real problem. Please fix it.

avakar
thanks, that is it. Sorry it is early in the morning, so completly awake yet ;)
Good, should get better as you drowse back to the normal wakeful sleepiness :-)
nik
A: 

numbersscaled should be array of chars not char.

fwrite(numbersscaled, sizeof(numbersscaled[0]), 4, filebin);
Ahmed Said
A: 

Something is not right with,

numbersscaled/numbersscaled[0]

Isn't it char numberscaled[4]? tho you have declared it as a char in your question.

You should read the fwrite man page again.

nik