c program compiler trouble?

Hey, I'm trying to compile this program to print the row sums of a list of integers read from a file. Can anyone spot the problem? The error message says that nested functions are not allowed, and is saying expected declaration before end of input. Very confused..

void getAvg (FILE* fp,int *table, int *sum)

{

int i=0;

int j=0;

fp = fopen("/Users/*******/Desktop/*****.txt", "r");

if(fp == NULL)

{

printf("\nError: File did not open!");

exit(0);

}

for (j=0; j<MAX_ROWS; j++) {

for (i=0; i<MAX_COLS ; i++) {

sum =0;

if (!feof(fp)) {

fscanf(fp, "%d", table);

*sum += table;

table++;

}

printf("\t%d ", *sum);

}

printf("\n");

}

fclose(fp);

return ;

}

Comments

  • Here are some changes which made it compile ... am not sure what you want for MAX_ROWS and MAX_COLS ... change as necessary:

    #include <stdio.h>

    #include <stdlib.h>

    #define MAX_ROWS 1

    #define MAX_COLS 1

    void getAvg (FILE* fp,int *table, int *sum)

    {

    int i=0;

    int j=0;

    fp = fopen("/Users/*******/Desktop/*****.txt ", "r");

    if(fp == NULL)

    {

    printf("\nError: File did not open!");

    exit(0);

    }

    for (j=0; j<MAX_ROWS; j++) {

    for (i=0; i<MAX_COLS ; i++) {

    *sum = 0;

    if (!feof(fp)) {

    fscanf(fp, "%d", table);

    *sum += *table;

    (*table)++;

    }

    printf("\t%d ", *sum);

    }

    printf("\n");

    }

    fclose(fp);

    return ;

    }

  • You should show your whole code. I don't see a nested function definition here.

    You cant do this

    void func1()

    {

    void func2(int x))

    {

    printf("x is %d", x);

    }

    ...

    ...

    ...

    }

    main is also considered a function so this is also illegal

    int main()

    {

    void func3(float x)

    {

    printf("x is %f", x)

    }

    func3(3.145);

    }

Sign In or Register to comment.