【问题标题】:argument 2 has type int * and want to read a file参数 2 的类型为 int * 并且想要读取文件
【发布时间】:2014-10-13 19:30:54
【问题描述】:

读行时好像有错误

状态 10

如何将数字 10 保存为整数名称状态?最后我会再读三行%c %i,这样我就可以再保存三个变量。 fscanfstrtok 更易于使用。

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



//any?
///
/// main function
/// @author jw
/// @param 
/// @param 
/// @returns 
///
int main(int argc, char* argv[])
{

    FILE *fp;


    fp = fopen( argv[1], "r" );
    if( fp == NULL ) {
            //something went wrong
            perror( argv[1] );
            exit( 1 );
        }

        int states = 0;

        //print the first 3 lines using sscanf()
        // then capitalize on that matrix
        //
        fscanf(fp, "%i",&states  );
        printf("Number found is %i", &states);
    }

编译器输出:

 tokenize.c: In function 'main':
    tokenize.c:33:3: warning: format '%i' expects argument of type 'int', but argument 2 has type 'int *' [-Wformat=]
       printf("Number found is %i", &states);
       ^
    tokenize.c:14:14: warning: unused parameter 'argc' [-Wunused-parameter]
     int main(int argc, char* argv[])
                  ^
    gcc -ggdb -Wall -std=c99 -pedantic -Wextra -o tokenize tokenize.o

【问题讨论】:

  • 现在代码只打印“状态数为0”
  • 我读到的那一行应该是States 10,我不知道如何选择int 10所以我把代码改成了fscanf(fp, "%c%i, letter, &states); 和添加字符;
  • 这些 cmets 是什么意思?能否请您留在主题上。
  • 是的,我会在 2 分钟内更新问题。
  • 不,请不要那样做。你问了这个问题。请让我们留在主题上并处理您提出的问题。

标签: c linux int fopen scanf


【解决方案1】:

有问题的行是

printf("Number found is %i", &states);

编译器说:

警告:格式“%i”需要“int”类型的参数,但参数 2 的类型为“int *”

正如编译器所说,它希望你传递int,但你传递int*。你需要写:

printf("Number found is %i", states);

使用fscanf,您传递了变量的地址&amp;states,因为您需要fscanf 来修改变量。但是对于printf的调用,不会对变量进行任何修改,您必须传递变量的值states

【讨论】:

    【解决方案2】:

    在你的代码中,改变这个

    printf("Number found is %i", &states);
    

    printf("Number found is %i", states);
    

    printf() 接受整数变量,而不是指向整数变量的指针。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 1970-01-01
      • 2019-04-03
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多