I'm trying to get my head around pointers in C and the notes i have aren't making any sense to me at the moment. I have a function that reads data from a text file, the file contains numbers arranged in a sort of able, four values separated by spaces followed by a table of 12 rows and two columns. I want to read the first four numbers into variables then the table into an array. The problem I'm having is getting the four variables out of the function, my notes suggest using pointers to get multiple variables out of a function. I'm trying to use this to read a value from the file into the location of the variable. fscanf(inf,"%f",&variable); but when i read the variable outside the function it's empty... This is driving me up the wall and I would really appreciate someone giving me a simple example of how to use a pointer to get a value out of a function. Thanks in advance Moriquendi
Where do you define variable? Sounds more like a scope issue. If you'd read out the file inside a function foo(), then define the array to hold the values outside the function and pass a pointer or a reference into foo().
the variable is defined outside the function, straight after int main. My main program goes like this. Int main float variable, var2,var3,var4, etc Call function (0) /* no input as data comes from text file Printf "variable" end The function goes like this: Code: float ReadData(){ FILE *inf; float input,coordinates [20][2]; int x,y; /* open file for input */ inf=fopen("rivets.txt","r"); // read values from datafile into pointers for use elsewhere fscanf(inf,"%f",&rivnum); fscanf(inf,"%f",&force); fscanf(inf,"%f",&offset); fscanf(inf,"%f",&angle); printf ("%f\n",rivnum); //loop to write data into each row for(x=0;x<20;x++) { //loop to write data into each column for(y=0;y<2;y++) { // read data into coordiantes array. fscanf(inf,"%f",&coordinates [x][y]); } } /* close the file */ fclose(inf); } printf("\n"); return (0); } Any help is much appreciated. Moriquendi
I think you're going about this slightly the wrong way. I can't really elaborate right now, since I'm not so good at writing correct code off the top of my head and I don't have a C compiler installed here at home.
I'm sure I am going about it the wrong way, I'm used to assembly language where things are much more simple, at least I find it much more simple... Moriquendi
You probably thought I'd forgotten about you. Well, I haven't... This should work as intended: Code: #include <stdio.h> int ReadData(); float rivnum, force, offset, angle; int main(int argc, char *argv[]) { int i = ReadData(); printf("%d\n", i); printf("%f\n", rivnum); printf("%f\n", force); printf("%f\n", offset); printf("%f\n", angle); return 0; } int ReadData() { FILE *inf; int i = 0; inf = fopen("rivets.txt","r"); i += fscanf(inf,"%f", &rivnum); i += fscanf(inf,"%f", &force); i += fscanf(inf,"%f", &offset); i += fscanf(inf,"%f", &angle); fclose(inf); return i; } To see the output you may have to direct it to a file e.g. prog.exe > output.txt. Note, that there must exist a file of the given name. Otherwise, *inf will be NULL and the program will crash. I hope, this helps you get on with your work.