[英]pass string to function in C
我有一个简单的程序,可以读取位置和速度列表,尽管它没有编译。 我只想向用户询问位置和速度文件的名称,然后将数组输出回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
,同时posfile
和velfile
向量:
char posfile,velfile;
在阅读其内容时,请跳过&
:
scanf( "%s", velfile );
C中的字符串是char
的数组。 您应该将参数readinput
从char
更改为char *
或char[]
:
int readinput(char * posfile, char * velfile)
提示:下次告诉它为什么不编译。
我什至不知道从哪里开始……您可能想学习一本好书。
在您的main()
函数中:
char posfile,velfile;
printf( "What is the name of your positions file (x y z): " );
scanf( "%s", &posfile );
字符是单个字符,例如“ a”,如果要执行字符串,则需要动态或静态分配的字符数组:
char posfile[100];
例如。
在您的readinput()
函数中:
当您想传递char *
(字符串)时,您传递char
(单个字符)
char posfile, char velfile
您的函数返回一个int
类型,但您尝试返回的是float
类型:
int readinput(char posfile, char velfile)
float pos[10000][3], vel[10000][3];
您不能在return
语句中从一个函数返回多个值:
return pos,vel;
您正在尝试将本地数据从函数返回到main:
float pos[10000][3], vel[10000][3];
...
return pos,vel;
这将为您提供UB,您需要将它们设置为全局,或在主目录中对其进行定义并通过它们。
不应该
int readinput (char posfile, char velfile)
是
int readinput (char *posfile, char *velfile)
因为我猜你想要一个字符串,而不仅仅是一个字符?
另一个问题是posfile和velfile的类型不应该是char,如何让一个char来保存文件名?
您应该这样定义它们:
char posfile[256],velfile[256];
并将读取代码更改为:
scanf( "%s", posfile );
和
scanf( "%s", velfile);
补充:您正在更改代码,所以我们必须关注您:
您应该将pos,vel作为参考参数传递给函数,并在readinput中删除其定义,而无需返回任何内容,
void readinput (char *posfile, char *velfile,float** pos, float** vel)
报告的问题是您正在尝试:
pos = readinput(posfile,velfile);
和
return pos;
您不能将数组返回为float,也不能将float分配给array。
除了scanfs中指针的其他问题外,您应该更改
float readinput (char *posfile, char *velfile)
{
float pos[10000][3], vel[10000][3];
...
至
void readinput (char *posfile, char *velfile, float pos[][3], float vel[][3])
{
...
并删除局部变量...。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.