简体   繁体   中英

Allocating memory for an array of struct i get an error

the code is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20

typedef struct word{

    char word[20];
    int occurrance;
} word;

int array_word_creator(word *array, FILE *fp);
void initialize(word array[], int max);
void comparator(word array[], int max, FILE *fp);
void printer(word array[], int max);





int main(int argc, char *argv[])
{
    FILE *f_sent, *f_words;
    word *array;
    int arr_lenght=0;
    if(argc!=3)
    {
        printf("Wrong argument number, please use NAME FILE1 FILE2;\n");
        exit(EXIT_FAILURE);
    }
    if((f_sent=fopen(argv[1], "r"))==NULL||(f_words=fopen(argv[1], "r"))==NULL)
    {
        printf("Can't find or open the files, please check if the name is correct\n");
        exit(EXIT_FAILURE);
    }
    arr_lenght=array_word_creator(array, f_words);
    comparator(array, arr_lenght ,f_sent);
    printer(array, arr_lenght);

    return 0;
}

int array_word_creator(word *array, FILE *fp)
{
    int n,i=0;
    fscanf(fp,"%d",&n);
    *array= malloc(n*sizeof(word));
    while(fscanf(fp,"%s", array[i].word)!=EOF)
    {
        i++;
    }
    initialize(array,n);

    return n;
}

void initialize(word array[], int max)
{
    int i;
    for(i=0;i<max;i++)
    {
        array[i].occurrance=0;
    }
}

void comparator(word array[], int max, FILE *fp)
{
    char word[MAX];
    int i;
    while(fscanf(fp,"%s", word)!=EOF)
    {
        for(i=0;i<max;i++)
        {
            if(strcmp(word, array[i].word)==0)
            {
                array[i].occurrance++;
            }
        }
    }
}

void printer(word array[], int max)
{
    int i;
    for(i=0;i<max;i++)
    {
        if(array[i].occurrance>0)
        {
            printf("The word '%s' occurs %d times\n", array[i].word, array[i].occurrance);
        }
    }
}

And the compiler says me:

C:\Users\Matteo\Google Drive\Programming\C\lab3\es1\main.c|47|error: incompatible types when assigning to type 'word' from type 'void *'|

I just studied memory allocation so i'm having some trouble with it, especially with structures. If possible, plase link me also some good docs about this subject.

thank you!

In main word *array is a pointer to a structure of type word.

You then pass array, which does not point to anything, to the function array_word_creator.

You then try to assign the pointer returned by malloc to where array is pointing, but it doesn't point anywhere yet, and even if it did, it would be pointing to a word (since it is a word *), so it can't store a pointer, hence the compiler error.

If you want to set the array pointer in main to the result of malloc, you have to pass a pointer to the pointer. int array_word_creator(word **array, FILE *fp) , then you would call it by doing array_word_creator(&array, .... ) , the your *array = malloc will work.

You want this:

...
arr_lenght = array_word_creator(&array, f_words);
...


int array_word_creator(word **array, FILE *fp)
{
  int n, i = 0;
  fscanf(fp, "%d", &n);
  *array = malloc(n * sizeof(word));
  while (fscanf(fp, "%19s", (*array)[i].word) != EOF)
  {
    i++;
  }
  initialize(*array, n);

  return n;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM