Hi all.
I have an array of structures:
Code:
struct word
{
char *word;
int wordcount;
} array[ARRAYSIZE];
I have a function sort() that will use qsort() to sort the pointers in the structure. I want to pass the array to the function. What should the prototype for the function and the function call look like?
I have create the function prototype as:
Code:
void sort(struct word array);
And tried to pass with:
And the compiler is giving the error message: Incompatible type for argument 1 of 'sort'.
I know the problem is that I need to include the reference to the pointer as a parameter in the function prototype and as an argument in the function call. But I don't know how to implement this. My reference book and googling has not provided any insights.
I've tried with no success, probably because the parameters in the function prototype is wrong:
Help? Thank you.
Code:
// wordcount.c: count the number of occurrences of every word in a text file
// 4 functions: openfile, countwords, sort, display
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FILENAMELENGTH 30
#define ARRAYSIZE 1000
#define SUCCESS 1
#define FAILURE 0
FILE *file;
int arraycount = 0;
struct word
{
char *word;
int wordcount;
} array[ARRAYSIZE];
int openfile(char *filename);
void countwords(void);
void sort(struct word array);
//void display(char *array);
int main(void)
{
int result;
char filename[FILENAMELENGTH];
do
{
printf("Enter file to open: \n");
fgets(filename, FILENAMELENGTH, stdin);
filename[strlen(filename) - 1] = '\0';
} while((result = openfile(filename)) == FAILURE);
countwords();
sort(array);
// display(filename);
return 0;
}
int openfile(char *filename)
{
if((file = fopen(filename, "r")) == NULL)
return FAILURE;
else
{
printf("%s opened successfully\n", filename);
return SUCCESS;
}
}
void countwords(void)
{
#define WORDLENGTH 30
char buffer[WORDLENGTH];
int flag,c, count, buffercount = 0;//declared a variable flag
for(count = 0; count < ARRAYSIZE; count++) // initialise array member wordcount
{
array[count].wordcount = 0;
}
while((c = fgetc(file)) != EOF)
{
if((c != '.') && (c != ',') && (c != '!') && (c != ' ') && (c != ':') && (c != '(') && (c != ')') && (c !=
'-') && (c !='\n'))//check for enter in order to read the last word properly(without the '\n' considered to be part of the word)
{
buffer[buffercount] = c;
buffercount++;
continue;
}
else
{
buffer[buffercount] = '\0'; // if c is a space or punctuation, add terminator to create string
if(arraycount == 0) // add first word to first array
{
array[arraycount].word = malloc((buffercount) * sizeof(char)); // allocate memory for string
strncpy(array[arraycount].word, buffer, buffercount);
array[arraycount].wordcount++; // increment word counter
arraycount++; // increment the word array to accept new word
buffercount = 0; // reset buffer to accept new string
continue;
}
flag=0;//flag set to zero,before every loop for checking about duplicates
for(count = 0; count < arraycount; count++)
{
if((strcmp(buffer, array[count].word)) == 0)
{
array[count].wordcount++; // increment word counter
buffercount = 0; // reset buffer to accept new string
flag=1;// we have a duplicate,set flag to true
break;
}
}
if(!flag) //flag is 0,so new word occured; add new word to array.
{
array[arraycount].word = malloc((buffercount) * sizeof(char)); // alocate memory
strncpy(array[arraycount].word, buffer, buffercount);
array[arraycount].wordcount++; // increment word counter
arraycount++; // increment the word array to accept new word
buffercount = 0; // reset buffer to accept new string
}
}
}
fclose(file);
}
void sort(struct word array)
{
qsort(array, arraycount, sizeof(array[0]), strcmp)
}
void display(void)
{
for(count = 0; count < arraycount; count++)
{
printf("%s %d\n", array[count].word, array[count].wordcount);
free(array[count].word);
}
}