Reputation: 2124
I have a simple program which reads in a list of positions and velocities, though it is not compiling. I simply want to ask the user for the name of the position and velocity file and then output the array back in 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;
}
I do apologize, this is my first program.
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;
Upvotes: 0
Views: 861
Reputation: 5699
another problem is posfile and velfile's type should not be char, how could you let one char to hold a file's name?
you should define them like this:
char posfile[256],velfile[256];
and change the reading code to:
scanf( "%s", posfile );
and
scanf( "%s", velfile);
Added: you are changing your code, so we have to follow you:
you should pass pos,vel as reference parameters to the function and remove the their definition inside readinput, and you don't need to return anything,
void readinput (char *posfile, char *velfile,float** pos, float** vel)
Upvotes: 0
Reputation: 3039
The reported problem is in that you are trying to:
pos = readinput(posfile,velfile);
and
return pos;
you cannot return array as float and you cannot assign float to array.
except other issues with pointers in scanfs, you should change the
float readinput (char *posfile, char *velfile)
{
float pos[10000][3], vel[10000][3];
...
to
void readinput (char *posfile, char *velfile, float pos[][3], float vel[][3])
{
...
and drop the local variables....
Upvotes: 0
Reputation: 11
Should't
int readinput (char posfile, char velfile)
be
int readinput (char *posfile, char *velfile)
since I'm guessing you want a string, not just a character?
Upvotes: 0
Reputation: 49523
I'm not even sure where to start... you might want to invenst in a good C book.
In your main()
function:
char posfile,velfile;
printf( "What is the name of your positions file (x y z): " );
scanf( "%s", &posfile );
A char is a single character like 'a', if you want to do a string you need either a dynmically or statically allocated array of chars:
char posfile[100];
for example.
In your readinput()
function:
You're passing char
s (single characters) when you wanted to pass char *
(strings)
char posfile, char velfile
Your function returns an int
type, but you're trying to return float
types:
int readinput(char posfile, char velfile)
float pos[10000][3], vel[10000][3];
You can not return more than 1 values from a function in a return
statement:
return pos,vel;
You are trying to return local data from your function back to main:
float pos[10000][3], vel[10000][3];
...
return pos,vel;
That's going to give you UB, you need to make them global, or define them in your main and pass them.
Upvotes: 0
Reputation: 8073
A string in C is an array of char
s. You should change the arguments to readinput
from char
to char *
or char[]
:
int readinput(char * posfile, char * velfile)
Hint: next time tell why it isn't compiling.
Upvotes: 0
Reputation: 21460
you've got it wrong. Type char
has space for a single character only. You have to use a char *
to have a string there.
int readinput (char *posfile, char *velfile)
And in main
, make both posfile
and velfile
vectors:
char posfile,velfile;
And when reading their content, skip the &
:
scanf( "%s", velfile );
Upvotes: 1