I am having trouble compiling this program, I have just about everything written up but the compiler errors are keeping me stuck at a standstill. In the WRITEARRAY and READARRAY function are where the main compile errors are giving me subscript is not of integral type and this is making no sense to me. Please help with any advice that you could give.

The second error I am seeing is that on line 39 at open=OPENFILEWRITES; i get a cannot convert from float (_cdecl) () to a 'float'

#include <stdio.h>
#define Max 4
#define MAX 6


void main(void)
{
    FILE *openfilewrite ;
    float open, write, read;
    float i, a,b,c,j;
    float arr[Max][MAX], arr1[Max][MAX];
    float INITIALIZE(float ), MULTIPLY(float, int ), OPENFILEWRITES(), OPENFILEREAD(), WRITEARRAY(float), READARRAY();

    printf("Please enter the integer");
    scanf("%f", a);

    arr[Max][MAX]=INITIALIZE(arr[Max][MAX]);

    arr1[Max][MAX]=MULTIPLY(arr[Max][MAX],a);

    open=OPENFILEWRITES;

    write=WRITEARRAY(arr[Max][MAX]);

    read=READARRAY(arr[Max][MAX]);

}




float INITIALIZE(float x)
{
    float i=0, j=0;

    for(i=0; i<4; i++)
    {
        for(j=0; j<6; j++)
        {
            x=(i+j);
        }
    }   
    return(x);
}

float MULTIPLY(float x,int a)
{
    float i=0, j=0;

    for(i=0; i<Max; i++)
    {
        for(j=0; j<MAX; j++)
        {
            x= a*(i+j);
        }
    }
    return(x);
}

float OPENFILEWRITES()
{
    FILE *openfilewrite ;

    if((openfilewrite = fopen("outfile.nums", "w")) == NULL) 
    {                           /*beginning of the if statement*/
    printf("ERROR: file %s can not be opened!\n", "outfile.nums");
    exit();
    }   
}

float WRITEARRAY(float x[Max][MAX])
{
    FILE *openfilewrite ;
    float i=0, j=0;

    for(i=0; i<Max; i++)
    {
        for(j=0; j<MAX; j++)
        {
            fprintf(openfilewrite,"%f", x[i][j]);
        }
    }
}

float OPENFILEREAD()
{
    FILE *openfilewrite ;

    if((openfilewrite = fopen("outfile.nums", "r")) == NULL) 
    {                           /*beginning of the if statement*/
    printf("ERROR: file %s can not be opened!\n", "outfile.nums");
    exit();
    }
}

float READARRAY(float x[Max][MAX])
{
    FILE *openfilewrite ;

    float i=0, j=0;
    for(i=0; i<Max; i++)
    {
        for(j=0; j<MAX; j++)
        {
            fscanf(openfilewrite,"%f", x[i][j]);
        }
    }

}

Dani AI

Generated

Several independent issues are responsible for the compile failures and undefined behaviour. The most immediate compile error comes from using non-integral loop counters as array indices and from using an expression like arr[Max][MAX] where the intent appears to be passing the whole 2D array. Another class of errors comes from treating functions like values (assigning a function name instead of calling it), using uninitialised FILE * objects for fprintf/fscanf, and declaring non-void functions that never return. These points expand on the hints from and and show where signature- and usage-level fixes are needed.

Practical fixes at the function/signature level: use integral types for indices (int or size_t); pass the array by name (e.g., arr) rather than an out-of-range element; open files with fopen and return the FILE * (or accept a FILE * parameter in reader/writer functions); call functions with parentheses; and either make I/O helpers return FILE */void correctly or provide the correct return values. A compact pattern:

FILE *open_file_write(const char *name)
{
    FILE *fp = fopen(name, "w");
    if (!fp) { perror(name); exit(EXIT_FAILURE); }
    return fp;
}

void write_array(FILE *fp, const float arr[Max][MAX])
{
    for (int i = 0; i < Max; ++i)
        for (int j = 0; j < MAX; ++j)
            fprintf(fp, "%f\n", arr[i][j]);
}

Other important details: scanf/fscanf require pointers (e.g. &var or &arr[i][j]) and their return values should be checked; arr[Max][MAX] indexes past the end (valid indices are 0..Max-1 and 0..MAX-1); include <stdlib.h> to use exit(EXIT_FAILURE); prefer int main(void) returning 0; and enable compiler warnings (gcc -Wall -Wextra) to catch many mistakes early.

Checklist: change float indices to integral types; pass arrays by name; open files and propagate the FILE *; call functions with parentheses; use & for scanf/fscanf; make non-void functions return values or be void; compile with warnings. Applying these corrections will address the diagnostics already pointed out by and and remove the undefined behaviour from uninitialised file pointers.

Recommended Answers

All 4 Replies

Correction the errors are at lines 21, 71, 80, and 105

The problem is fairly straightforward, and is exactly what the error message says: in both of the functions, you declare i and j as type float, which is not an integral type - that is to say, it isn't a type which holds only integer values, such as int, short, unsigned int, long, and the like. You cannot use a floating-point number as an index, for reasons that should be tolerably clear (i.e., you can't have a fractional index to an array). If you simply change the declarations to type int, the problem should resolve itself.

The error on line 21 is that you forgot the () to call the function. So you're trying to assign a function to a double variable.

The error on line 105 is that the argument to scanf needs to be a pointer, not a double. Also you're using openfilewrite uninitialized, so it's UB.

PS: The last three of your functions don't contain a return statement even though they're declared to have a non-void return value.

ok thank you that did fix that problem. Sometimes it takes another set of eyes to see the obvious. What about the second issue i had with the conversion of a float(_cdecl)() to a 'float', if you could help explain what the problem with that is i would be grateful

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.