Assuming the following:
- Your array is properly allocated (either on the stack or dynamically) to be at-least-10-elements wide.
- Your structure member
value is a valid char * or fixed-length char[n] buffer. - The string data references by the
value member of your structure is properly null-terminated. - The string data referenced by
tempVal is valid and properly null-terminated. - You're aware that
strcmp() compares case-sensitively - You're comment about 100 elements in the array and this code only checking the first ten (10) elements is intentional
My crystal ball tells me that ultimately you need to have your function actually return something.
int valCheck(char *tempVal) { int j; for(j=0;j<10;j++) { if(strcmp(array1[j].value, tempVal) == 0) { array1[j].count++; //increment its count return j; // <== return index of found element } } return -1; // <== return -1 per your description of failure. }
Note: Your compiler, equipped with decent warning-checks, would easily spot this lack-of-return code. Heed those warnings. Likewise, double-check everything in the bullet list at the top of this answer to make sure what you're going into this with is proper.
EDIT Updated to reflect sample dictionary build for OP.
The following is how I think this is likely to be called. Hope it helps you. I changed a few things:
- Made the dictionary a little bigger (256)
- Processes the command line to get the filename. Makes it easier for me to test.
- Only reports a summary at the end rather than per-word while processing.
I hope it helps.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> struct values { char value[64]; int count; }; // global array. struct values array1[256]; int n_words = 0; //check if value exists in array int valCheck(const char *tempVal) { int j; for(j=0;j<10;j++) { if(strcmp(array1[j].value, tempVal) == 0) { array1[j].count++; //increment its count return j; } } return -1; } int main(int argc, char *argv[]) { FILE * input = NULL; char tempVal[64]; int i=0; if (argc < 2) { printf("Must specify a filename.\n"); return EXIT_FAILURE; } // open file input = fopen(argv[1],"r"); if (!input) { perror("Failed to open file."); return EXIT_FAILURE; } // read all strings from the file one at a time. while (fscanf(input, "%64s", tempVal) == 1) { int i = valCheck(tempVal); if (i == -1) { if (n_words < sizeof(array1)/sizeof(array1[0])) { strcpy(array1[n_words].value, tempVal); array1[n_words].count = 1; i = n_words++; } else { // error. no more space in dictionary printf("No more space to add word: %s\n", tempVal); } } } fclose(input); // summary report for (i=0;i<n_words;++i) printf("WORD %i: %s, COUNT IS: %i\n", i, array1[i].value, array1[i].count); return EXIT_SUCCESS; }
Input
hello my name is dave hello I am dave hi
Output
WORD 0: hello, COUNT IS: 2 WORD 1: my, COUNT IS: 1 WORD 2: name, COUNT IS: 1 WORD 3: is, COUNT IS: 1 WORD 4: dave, COUNT IS: 2 WORD 5: I, COUNT IS: 1 WORD 6: am, COUNT IS: 1 WORD 7: hi, COUNT IS: 1
Homework
After all of that, I leave you to determine why the following code also works, but uses no temporary buffer to do so (sort of, anyway).
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> struct values { char value[64]; int count; }; // global array. #define MAX_WORDS 256 struct values array1[MAX_WORDS] = {{{0},0}}; int n_words = 0; //check if value exists in array int valCheck(const char *tempVal) { int j; for(j=0; j< n_words; j++) { if(strcmp(array1[j].value, tempVal) == 0) { array1[j].count++; //increment its count return j; } } return -1; } int main(int argc, char *argv[]) { FILE * input = NULL; int i=0; if (argc < 2) { printf("Must specify a filename.\n"); return EXIT_FAILURE; } // open file input = fopen(argv[1],"r"); if (!input) { perror("Failed to open file."); return EXIT_FAILURE; } // read all strings from the file one at a time. while (n_words < MAX_WORDS && fscanf(input, "%64s", array1[n_words].value) == 1) { if (valCheck(array1[n_words].value) == -1) { array1[n_words].count = 1; ++n_words; } } fclose(input); // summary report for (i=0;i<n_words;++i) printf("WORD %i: %s, COUNT IS: %i\n", i, array1[i].value, array1[i].count); return 0; }
The output is the same as before.
array1[]structure to the question.fprintf(stderr, "%d {%s,%s}\n", j, array1[j].value, tempVal);inside the loop will probably enlighten you.strcmpworks for C strings, not forstructs. It stops when it finds the "null terminator", which could be an arbitrary zero embedded in yourstruct.strcmp()I see no casts, and I do see a structure member denote (thevalueinarray1[j].value). Knowing whether this compiles vs. fails-at-runtime would probably narrow quite a bit down.