Question:

C typedef misunderstanding?

by  |  earlier

0 LIKES UnLike

Note, i have to use pure C to do this. If i use an int array as an element of data storage to pass around functions, it all works. But if i use defined type i get an error "incompatible types in assignment". Problem is located in line "freq[var] = 0;" in following code:

ratetable *Collectrate(FILE *f) {

// int* freq = (int*) malloc(CHAR * sizeof(int));

ratetable* freq = (ratetable*) malloc(sizeof(CHAR * sizeof(int)));

int var;

for (var = 0; var < sizeof(freq); ++var) {

freq[var] = 0;

}

while (!feof(f)) {

freq[getc(f)]++;

}

//memcpy (freq2,freq,CHAR);

//free(freq);

return freq;

}

...

and

#define CHAR 256

typedef int ratetable[CHAR];

 Tags:

   Report

2 ANSWERS


  1. The problem is that you are initializing a pointer to the ratetable type, which is already an array. That means that you should either have it as a single variable (you don&#039;t need dynamic allocation for this), or use the syntax:

    (*freq)[var] = 0;

    It think the former is cleaner, (and faster), but you can do what you want.


  2. Not certain as it has been 6+ years since I have coded but:

    when accessing a particular element in an array the variable type has to be integer.  freq[var]  var has to be int if it is a defined type the compile/link would have to have it defined in a .h file.

    You will probably get a better answer than this if you go to

    www.codeguru.com

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.