编程指针问题

时间:2013-11-25 20:04:03

标签: c function pointers project

我被分配做一个从文件中读取的项目,根据它输入的char是用以下值做的,所以如果它读取+它将添加接下来的两个数字或如果它是一个H它就会打印出说明。我将结果打印到另一个文件时遇到问题我将指针传递给每个函数,如下所示void add(int a, int b, FILE *print),并按如下方式调用它。add(a, b, printer),它在我尝试打印到文件之前完美运行。我得到的错误是......

在函数add:

error : incompatible type for argument 1 of fprintf
expected 'struct FILE *' but argument is of type 'FILE'

这也发生在我的所有功能上。

这是代码。谢谢!

#include <stdio.h>
#include <stdlib.h>

void add(int a, int b, FILE *print);
void add(int a, int b, FILE *print)
{
    int c;
    c = a + b;
    printf("%i + %i = %i\n\n", a, b, c);
    fprintf(*print, "%i + %i = %i\n\n", a, b, c);
}
void subtract(int a, int b, FILE *print);
void subtract(int a, int b, FILE *print)
{
    int c;
    c = a - b;
    printf("%i - %i = %i\n\n", a, b, c);
    fprintf(*print, "%i - %i = %i\n\n", a, b, c);
}
void multiply(int a, int b, FILE *print);
void multiply(int a, int b, FILE *print)
{
    int c;
    c = a * b;
    printf("%i * %i = %i\n\n", a, b, c);
    fprintf(*print, "%i * %i = %i\n\n", a, b, c);
}
void divide(int a, int b, FILE *print);
void divide(int a, int b, FILE *print)
{
    double c;
    c = (double)(a/b);
    printf("%i - %i = %.1lf\n\n", a, b, c);
    fprintf(*print, "%i - %i = %.1lf\n\n", a, b, c);
}
void help(FILE *print);
void help(FILE *print)
{
    printf("+ i j [Integer Add]\t\tAdds integers i and j and print out result\n\n");
    printf("* i j [Integer Multiply]\tMultiply integers i and j and print out result\n\n");
    printf("- i j [Integer Subtract ]\tSubtract integer j from i and print out result\n\n");
    printf("/ i j [Integer Divide ]\t\tDivide integer i by j and print out result of integer    division\n\n");
    printf("H [Help ]\t\t\tPrint a short synopsis of all the available commands\n\n");
    printf("Q [Quit ]\t\t\tQuit\n\n");
    printf("\n\n");
    fprintf(*print, "+ i j [Integer Add]\t\tAdds integers i and j and print out result\n\n");
    fprintf(*print, "* i j [Integer Multiply]\tMultiply integers i and j and print out result\n\n");
    fprintf(*print, "- i j [Integer Subtract ]\tSubtract integer j from i and print out result\n\n");
    fprintf(*print, "/ i j [Integer Divide ]\t\tDivide integer i by j and print out result of integer division\n\n");
    fprintf(*print, "H [Help ]\t\t\tPrint a short synopsis of all the available commands\n\n");
    fprintf(*print, "Q [Quit ]\t\t\tQuit\n\n");
    fprintf(*print, "\n\n");
}
char quit(void);
char quit(void)
{
    char user;
    printf("Do you really want to quit?(y/n): ");
    scanf(" %c", &user);
    return user;
}

int main()
{

char trigger;
char user;
char fname;
char lname;
int a;
int b;

FILE *myPointer;
FILE *printer;
myPointer = fopen("CommandsProj1.dat", "r");
printer = fopen("results.dat", "w");

if (myPointer == NULL)
{
    printf("error opening the file");
    fprintf(printer, "error opening the file");
}
else
{
    fscanf(myPointer, " %c%c", &fname, &lname);
    printf("Initials %c%c\n\n\n", fname, lname);

    while(user != 'y')
    {
        fscanf(myPointer, " %c", &trigger);
        switch(trigger)
        {
            case '+':
                    fscanf(myPointer, "%i%i", &a,&b);
                    add(a,b,myPointer);
                    break;
            case '-':
                    fscanf(myPointer, "%i%i", &a, &b);
                    subtract(a,b,myPointer);
                    break;
            case '/':
                    fscanf(myPointer, "%i%i", &a,&b);
                    divide(a,b,myPointer);
                    break;
            case '*':
                    fscanf(myPointer, "%i%i", &a,&b);
                    multiply(a,b,myPointer);
                    break;
            case 'H':
                    help(myPointer);
                    break;
            case 'Q':
                    user = quit();
        }
    }
}

return 0;
}

4 个答案:

答案 0 :(得分:2)

您要取消引用FILE *fprintf。这不是必需的(甚至是错误的),因为fprintf需要一个指针。将打印行更改为:

fprintf(print, ...);

答案 1 :(得分:2)

如果你在fprintf函数调用中更改所有* print以打印,那么一切都会好的。

完整工作代码

#include <stdio.h>
#include <stdlib.h>

void add(int a, int b, FILE *print);
void add(int a, int b, FILE *print)
{
    int c;
    c = a + b;
    printf("%i + %i = %i\n\n", a, b, c);
    fprintf(print, "%i + %i = %i\n\n", a, b, c);
}
void subtract(int a, int b, FILE *print);
void subtract(int a, int b, FILE *print)
{
    int c;
    c = a - b;
    printf("%i - %i = %i\n\n", a, b, c);
    fprintf(print, "%i - %i = %i\n\n", a, b, c);
}
void multiply(int a, int b, FILE *print);
void multiply(int a, int b, FILE *print)
{
    int c;
    c = a * b;
    printf("%i * %i = %i\n\n", a, b, c);
    fprintf(print, "%i * %i = %i\n\n", a, b, c);
}
void divide(int a, int b, FILE *print);
void divide(int a, int b, FILE *print)
{
    double c;
    c = (double)(a/b);
    printf("%i - %i = %.1lf\n\n", a, b, c);
    fprintf(print, "%i - %i = %.1lf\n\n", a, b, c);
}
void help(FILE *print);
void help(FILE *print)
{
    printf("+ i j [Integer Add]\t\tAdds integers i and j and print out result\n\n");
    printf("* i j [Integer Multiply]\tMultiply integers i and j and print out result\n\n");
    printf("- i j [Integer Subtract ]\tSubtract integer j from i and print out result\n\n");
    printf("/ i j [Integer Divide ]\t\tDivide integer i by j and print out result of integer    division\n\n");
    printf("H [Help ]\t\t\tPrint a short synopsis of all the available commands\n\n");
    printf("Q [Quit ]\t\t\tQuit\n\n");
    printf("\n\n");
    fprintf(print, "+ i j [Integer Add]\t\tAdds integers i and j and print out result\n\n");
    fprintf(print, "* i j [Integer Multiply]\tMultiply integers i and j and print out result\n\n");
    fprintf(print, "- i j [Integer Subtract ]\tSubtract integer j from i and print out result\n\n");
    fprintf(print, "/ i j [Integer Divide ]\t\tDivide integer i by j and print out result of integer division\n\n");
    fprintf(print, "H [Help ]\t\t\tPrint a short synopsis of all the available commands\n\n");
    fprintf(print, "Q [Quit ]\t\t\tQuit\n\n");
    fprintf(print, "\n\n");
}
char quit(void);
char quit(void)
{
    char user;
    printf("Do you really want to quit?(y/n): ");
    scanf(" %c", &user);
    return user;
}

int main()
{

char trigger;
char user;
char fname;
char lname;
int a;
int b;

FILE *myPointer;
FILE *printer;
myPointer = fopen("CommandsProj1.dat", "r");
printer = fopen("results.dat", "w");

if (myPointer == NULL)
{
    printf("error opening the file");
    fprintf(printer, "error opening the file");
}
else
{
    fscanf(myPointer, " %c%c", &fname, &lname);
    printf("Initials %c%c\n\n\n", fname, lname);

    while(user != 'y')
    {
        fscanf(myPointer, " %c", &trigger);
        switch(trigger)
        {
            case '+':
                    fscanf(myPointer, "%i%i", &a,&b);
                    add(a,b,myPointer);
                    break;
            case '-':
                    fscanf(myPointer, "%i%i", &a, &b);
                    subtract(a,b,myPointer);
                    break;
            case '/':
                    fscanf(myPointer, "%i%i", &a,&b);
                    divide(a,b,myPointer);
                    break;
            case '*':
                    fscanf(myPointer, "%i%i", &a,&b);
                    multiply(a,b,myPointer);
                    break;
            case 'H':
                    help(myPointer);
                    break;
            case 'Q':
                    user = quit();
        }
    }
}

return 0;
}

答案 2 :(得分:0)

我认为你必须用print替换* print,因为它是作为ppointer传递的

答案 3 :(得分:0)

问题在于fprintf(*print,...);

我认为您没有将定义和声明语句中的*与去引用语句中的*区分开来。

考虑以下代码段:

int a=10;
int *p =a;
printf("%d",*p);

int *p =a;中,这是一个定义语句,*在这里表示p是一个指针。

printf("%d",*p);中,*之前还有一个p,但在这里它是一个运算符,它得到p指向的值(此处a的值为10)。

所以在fprintf(...);中,第一个参数应该是一个指针,而不是指针指向的东西。然后你应该使用你传入的指针print