c - 类型 "char (*)[16]"的参数与类型 "const char *"的参数不兼容

标签 c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <iostream>
#include <string>
#include "stdafx.h" 

#define MAXNAME 16

void main(int argc, char *argv[])
{
    int c, i, j, f;

    char lib[MAXNAME] = "lib\0       ";
    FILE *in, *out;
    char name[MAXNAME];

    /* open input file */
    i = 0;

    while ((name[i] = *argv[1]) != '\0') {
        i++;
        argv[1]++;
    }
    if ((in = fopen(&name, "r")) == NULL) {
        printf("Can't open file\n");
        goto end;
    }

    /* open output file. Same name as input but with .lib extension */
    while (name[i] != '.') i--;
    j = 0;
    i++;
    while (MAXNAME >= (i + j)) {
        name[i + j] = lib[j];
        j++;
    }

    if ((out = fopen(&name, "w")) == NULL) {
        printf("Can't open file\n");
        goto end;
    }

我正在尝试用 C 打开文件。我使用的是 Visual Studio 2015。当我编译之前的代码时,出现以下错误。

  1. char (*)[16] 类型的参数与以下参数不兼容 类型 const char *
  2. FILE *fopen(const char *,const char *): 无法将参数 1 从 char (*)[16] 转换为 const char *

我从评论中了解到,我试图将一个数量分配给另一个数量的两个数量中的一些数量格式不同,但是我无法修复我的错误。有人可以帮我吗?

最佳答案

您正在尝试传递指向 char name[16] 的指针,但 fopen 需要“const char *”。

您的解决方案应该直接在 fopen 中使用 argv[1]。

fopen(argv[1], "r");

关于c - 类型 "char (*)[16]"的参数与类型 "const char *"的参数不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37280062/

相关文章:

c - GtkBuilder没有实现widgets吗?

c - 通过 C 调用进入 Visual Studio 调试器

c++ - python和C之间共享配置文件格式?

c - 为什么结构的大小不等于其各个成员类型的大小之和?

C++:从 cURL 连接中提取 session token

c - 使用函数增加动态数组大小;无错误,下一个尺寸无效(快)

c++ - 使用 Visual Studio 2008 从 x86 编译 x64 dll

c - 在不使用 XDr 的情况下在同一主机上使用 RPC

c - 使用 # 和 ## 运算符的嵌套宏实现

c - Pthreads - 为什么 C 函数被声明为 void*?