RedMageKnight
RedMageKnight

Reputation: 187

C - Caesar Cipher Program - Subscripted Value is Neither Array nor Pointer

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;
}

Upvotes: 1

Views: 877

Answers (3)

Timothy Jones
Timothy Jones

Reputation: 22135

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).

Upvotes: 0

pmg
pmg

Reputation: 108938

    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 <--

Upvotes: 1

ouah
ouah

Reputation: 145859

            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.

Upvotes: 3

Related Questions