c - 警告 : assignment makes pointer from integer without a cast in struct

标签 c struct warnings

我正在编写一个程序,计算 PGM 图像的负值并将其保存在其他 PGM 图像中。我在行中收到此警告

 (argv[5]) = (imagem->l - 1);
 (argv[6]) = (imagem->c - 1);

我的代码(main.c):

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

#include "declarations.h" 

typedef struct  
{
  int c; 
  int l; 
  unsigned char **matrizPixels; 
} PGM;

/*command line:
./exec input output x0 y0 x1 y1
argv0  argv1   argv2 argv3 argv4 argv5 argv6*/

int main(int argc, char* argv[])
{
  PGM *imagem = (PGM*)malloc(sizeof(PGM));

  imagem = (PGM*) lePGM(argv[1]);

  /* if there isn't x0,y0,x1,y1... */
  if ( (!(argv[3])) && (!(argv[4])) && (!(argv[5])) && (!(argv[6])) )  
  {
    /* (x0,y0) will be (0,0) */
    (argv[3]) = 0;
    (argv[4]) = 0;

    /* (x1,y1) will be (line-1,colunm-1) */
    (argv[5]) = (imagem->l - 1);
    (argv[6]) = (imagem->c - 1);
  }

  NegativoRegiao(imagem, atoi(argv[3]), atoi(argv[4]), atoi(argv[5]), atoi(argv[6]) );

  salvaPGM(imagem, argv[2]);

  free (imagem); 

  return(0);
}

最佳答案

argv[n]char* 类型,但例如 imagem->lint 所以我会声明一些额外的变量并使用它们,而不是尝试重新使用 argv (特别是在您刚刚验证它不存在的情况下)

您可以检查argc以查看是否提供了足够的参数

通过检查malloc成功返回的值,可以使代码更加健壮

编辑:沿着这些思路的东西

int main(int argc, char* argv[]) {
int x0, y0, x1, y1;

PGM *imagem = malloc(sizeof(PGM));
if (imagem == NULL) {
  fprintf(stderr, "out of memory\n");
  exit(EXIT_FAILURE);
}

if (argc < 3) {
  fprintf(stderr, "missing commandline parameters\n");
  exit(EXIT_FAILURE);
}

imagem = (PGM*) lePGM(argv[1]);

if (argc < 7) {
  // print warning ?
  // set defaults
  x0 = y0 = 0;
  x1 = (imagem->l - 1);
  y1 = (imagem->c - 1);
}

NegativoRegiao(imagem, x0, y0, x1, y1);

salvaPGM(imagem, argv[2]);

free (imagem);

return EXIT_SUCCESS;
}

关于c - 警告 : assignment makes pointer from integer without a cast in struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18543519/

相关文章:

button - 如何在GVIM中显示标签页关闭按钮?

android - 删除原生 android 中的循环依赖

c - 有没有更有效的方法将文件解析为数组?

c - 如何获得 10 次方的 double 的尾数和指数?

c - C 中的 N 叉树

c - 指向结构的指针

python - MySQL备份-警告密码不安全

c - 如何将新项目插入到C中的链接列表中(在列表末尾)

c++ - 无法使用 InFile 将文件正确读入结构成员

php - Zend Studio IDE 中 "Assignment in condition"警告背后的基本原理是什么?