0

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;
4
  • 4
    If you say it is not compiling, you should post the errors rather than make people audit your entire code looking for what might be wrong. Commented May 8, 2013 at 17:37
  • For starters, your charfile and varfile are chars instead of char[] or char*. Strings in C are arrays of characters, a char in C is a single ascii byte. Commented May 8, 2013 at 17:38
  • Functions in C return at most one value (they can return no value -- or even fail to return)! Commented May 8, 2013 at 17:39
  • readinput needs to return a float Commented May 8, 2013 at 17:40

6 Answers 6

1

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 );
Sign up to request clarification or add additional context in comments.

Comments

0

A string in C is an array of chars. 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.

Comments

0

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 chars (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.

Comments

0

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?

Comments

0

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....

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.