1

I have to write a program for my C programming class that takes a text file outlining the Letter Frequency, and then use that information to take another text file input that is encrypted and decrypt it using that letter frequency. At first, I didn't really know how to start... so I decided to start throwing all of my thoughts into a main function and just trying to get it to work from there, but I feel like I'm doing this in such a convoluted manner and now I'm at the point where I just wanted to test to see if used the concept of structures right by outputting an array of structures on screen with all of my information, but I'm getting an error: Line 43 --> Subscripted Value is Neither Array nor Pointer.

I've never seen such an error before and I'm not really sure what it means... I was hoping I could show you what I have so far and perhaps someone could explain this error to me and give me some advice on how I should proceed in writing this code (advice and explanations please as it's detrimental that I learn the material ^_^).

Here is my code:

struct keyFreq
{
    char letter;
    float freq;
};

int main()
{
    FILE *fin;
    char freqname[20];
    char derp;
    char temp[6];
    int spacecounter = 0;
    printf("What is the name of the frequency file? ");
    scanf("%s", freqname);
    fin = fopen(freqname, "r");
    struct keyFreq k[25];
    while(!feof(fin)) {
        fscanf(fin, "%c", &derp);
        int i;
        for(i = 0; i < 26; ++i) {
            if((isalpha(derp)) && k[i].letter == NULL) {
                k[i].letter = derp;
                break;
            }
            if((isadigit(derp)) || derp == '.') {
                int j;
                for(j = 0; j < 7; ++j) {
                    if(temp[j] == -1)
                        temp[j] = derp;
                }
                break;
            }
            if((isspace(derp)) && (k[i].freq == '\0') && (spacecounter >= 2)) {
                double now;
                int k;
                now = atof(temp);
                for(k = 0; k < 7; ++k)
                    temp[k] = -1;
                k[i].freq = now; //Problematic Line <--
                spacecounter = 0;
                break;
            }
            if((isspace(derp)) && spacecounter < 2)
                spacecounter = spacecounter + 1;
            }
        }
        return 0;
}
Veger
  • 37,240
  • 11
  • 105
  • 116
RedMageKnight
  • 187
  • 2
  • 12

3 Answers3

3
            int k;
            now = atof(temp);
            for(k = 0; k < 7; ++k)
                temp[k] = -1;
            k[i].freq = now; //Problematic Line <--

k is redeclared in the first line of this code snippet: int k;

It shadows your initial declaration of k object: struct keyFreq k[25];. To fix this, use two different names in the two variable declarations.

ouah
  • 142,963
  • 15
  • 272
  • 331
1
    struct keyFreq k[25];
                int k;
                now = atof(temp);
                for(k = 0; k < 7; ++k)
                    temp[k] = -1;
                k[i].freq = now; //Problematic Line <--

The last k is not an array, it is the int you have just defined 4 lines previously.
Objects of type int cannot be used as arrays.

I see you are using C99 (// comments, definitions and code intermixed), so try limiting the definition of the temporary k to the for loop

    struct keyFreq k[25];
                now = atof(temp);
                for(int k = 0; k < 7; ++k)
                    temp[k] = -1;
                k[i].freq = now; //Problematic Line <--
pmg
  • 106,608
  • 13
  • 126
  • 198
  • Regarding the c99 comments, a lot of c89 compilers support `//` style comments (as a default extension) without supporting a declaration in the first clause of the `for` loop. – ouah Feb 05 '12 at 22:27
  • I was going to ask (I am more familiar with Java), can I declare the int k in the for loop within C like you can in Java then? My professor always declared it earlier, so I thought it was not supported in the C environment. – RedMageKnight Feb 05 '12 at 22:34
  • The declaration inside the `for` loop with a scope limited to the loop body was introduced with the 1999 revision of the language. Compilers that didn't (yet) adopt that revision do not accept the construct (unless they have it as an extension). I don't know if MSVC accepts it or not --- I think MSVC is not a C99 compiler. gcc accepts most of the 1999 revision of the language. – pmg Feb 05 '12 at 23:12
0

The problem is this:

 int k;

Which hides the earlier definition of

 struct keyFreq k[25];

Change int k;to something else, or rename your original array.

In general, it's not good practice to use single letter variable names for variables that aren't iterators. Otherwise things like this might happen (and the code is harder to read).

Timothy Jones
  • 21,495
  • 6
  • 60
  • 90