gpt4 book ai didi

c - 将字符串传递给 C 中的函数

转载 作者:行者123 更新时间:2023-11-30 15:49:04 24 4
gpt4 key购买 nike

我有一个简单的程序,它读取位置和速度的列表,尽管它没有编译。我只是想询问用户位置和速度文件的名称,然后在 main() 中输出数组。

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

#define loop(idx,last) for (idx = 0; idx < last ; idx++)

float readinput (char *posfile, char *velfile);


int main (void)
{
char posfile[100],velfile[100];
float pos[10000][3], vel[10000][3];
printf( "What is the name of your positions file (x y z): " );
scanf( "%s", &posfile );
printf( "What is the name of your velocity file (vx vy vz): " );
scanf( "%s", &velfile );
pos = readinput(posfile,velfile);
return 0;
}

float readinput (char *posfile, char *velfile)
{
float pos[10000][3], vel[10000][3];
float x,y,z;
float vx,vy,vz;
int i;
char line[256];

FILE *files;

files = fopen(posfile, "r");
loop(i,10000){
fscanf(files, "\n%f %f %f\t", &x, &y, &z);
pos[i][0] = x;
pos[i][1] = y;
pos[i][2] = z;
printf("\n%f %f %f\t",x,y,z);
}
fclose(files);

files = fopen(velfile, "r");
loop(i,10000){
fscanf(files, "\n%f %f %f\t", &vx, &vy, &vz);
vel[i][0] = vx;
vel[i][1] = vy;
vel[i][2] = vz;
printf("\n%f %f %f\t",vx,vy,vz);
}
fclose(files);
return pos;
}

我很抱歉,这是我的第一个节目。

  main.c: In function 'main':
main.c:18:5: error: incompatible types when assigning to type 'float[10000][3]' from type 'float'
pos = readinput(posfile,velfile);
^
main.c: In function 'readinput':
main.c:51:1: error: incompatible types when returning type 'float (*)[3]' but 'float' was expected
return pos;

最佳答案

你搞错了。 char 类型仅包含单个字符的空间。您必须使用 char * 才能在其中包含字符串。

int readinput (char *posfile, char *velfile)

并在main中,创建posfilevelfile vector :

char posfile,velfile;

阅读其内容时,请跳过&:

scanf( "%s", velfile );

关于c - 将字符串传递给 C 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16446819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com