I have two arrays, x and y, within a function.
I want to use these arrays in another function but as I understand it any function can only return 1 value.
How do I do it?!?!?!?!
Printable View
I have two arrays, x and y, within a function.
I want to use these arrays in another function but as I understand it any function can only return 1 value.
How do I do it?!?!?!?!
you can pass pointers to objects and let the function fill in the data
Code:void foo(int* a, int* b)
{
*a = 1;
*b = 2;
}
int main()
{
int a, b;
foo(&a, &b);
}
Or you could just search the forum, since this exact topic (and more than likely exact same thread title) get posted every single week.
Quzah.
Quote:
Originally Posted by Ancient Dragon
Don't really understand that - I'm rubbish, I know.
Say my code is of the form (at the moment):
Can you explain to me, without just writing code down, I need to understand please, how to get at these values x[i], y[i] so I can use all of their values later on in the program. Thank you.Code:void function_name();
{
BLAH BLAH BLAH
BLAH BLAH BLAH
BLAH BLAH BLAH
while(i < MAX_SIZE) // Checks that i will fit into the array space.
{
fs_ret = fscanf(fp,"%f%f", &x[i] , &y[i] );
// The above reads data from the file.
// 2 floating point numbers are read, x and y.
// Loop continues while 2 data pairs are read and i < 128.
if( fs_ret != 2)
break;
printf("%8.3f\t%8.3f\n",x[i],y[i]); // Prints the data for user.
i++; // Increments the number of pairs read.
BLAH BLAH BLAH
BLAH BLAH BLAH
BLAH BLAH BLAH
arrays are passed by reference, so when you pass an array to a
function and manipulate its elements, it manipulates the elements
of the original arrays. take this:
Code:#define SIZE 10
void call_the_function (int x [], int y []); /*function takes 2 arrays*/
int main (void)
{
int arrayX [SIZE];
int arrayY [SIZE];
.
.
.
.
call_the_function (arrayX, arrayY); /*passing arrays to function*/
.
. /*you could print arrayX and arrayY and you'll see that they were modified by the function*/
.
return 0;
}
void call_the_function (int x [], int y [])
{
.
.
.
while (i < SIZE) /*here, you manipulate the arrays locally as x & y, but you actually manipulate the parameter arrays, arrayX and arrayY*/
{
.
.
.
}
}
Hi Richie - Thanks for the advice. I tried it but couldn't get it to work at all, even though I now think I understand what I'm meant to be doing. As such, I've posted my whole code as it stands and would really appreciate it if you could look at it and see how I can change it to get the annoying x[] and y[] values out of the "read_data_from_file" function!!!
Arrrghhh!!
Cheers!
Code:/**********************************************************************************/
/* Description: This program will read x and y data values from a given text file */
/* and will calculate and plot the best straight line fit for these data. */
/* Author: Edward King */
/* Group: Yc */
/* Date: 10th March 2006 */
/**********************************************************************************/
#include <stdio.h>
#include <math.h>
#include <grx20.h>
#define MAX_SIZE 128
float x[MAX_SIZE];
float y[MAX_SIZE];
int read_data_from_file(void);
#define NO_FILE -1
int main(int argc, char *argv[])
{
int read_data_from_file(void)
/******************************************************************************/
/* Function Name: read_data_from_file */
/* Description: This function will read x and y data values from a text file. */
/******************************************************************************/
{
// OPENS A FILE:
int i;
FILE *fp;
char file_name[MAX_SIZE]; // Character array for the data filename.
int fs_ret;
// Asks the user to enter the name and location of the data file.
printf("Please enter location and filename of the datafile: ");
scanf("%s",file_name);
printf("\n");
fp = fopen(file_name, "r"); // Opens the file.
if(fp == NULL) // Checks for error in opening file.
{
printf("\nUnable to open file '%s'.\nCheck directory location and filename are correct.\n"
"Please close this program and start again.", file_name);
return NO_FILE; // Stops program if there is an error in opening the file.
}
// READS DATA PAIRS FROM THAT FILE:
i = 0; // Initialises data pair count.
while(i < MAX_SIZE) // Checks that i will fit into the array space.
{
fs_ret = fscanf(fp,"%f%f",&x[i],&y[i]);
// The above reads data from the file.
// 2 floating point numbers are read, x and y.
// Loop continues while 2 data pairs are read and i < 128.
if( fs_ret != 2)
break;
printf("%8.3f\t%8.3f\n",x[i],y[i]); // Prints the data for user.
i++; // Increments the number of pairs read.
} // End loop.
fclose(fp); // Closes the file.
return i; // Retains the number of pairs read.
} // END OF "read_data_from_file" FUNCTION.
int no_of_poi_read;
// USE FUNCTION "read_data_from_file" to return number of pairs read.
no_of_poi_read = read_data_from_file();
if( no_of_poi_read <1)
{
printf("\n\nNO DATA READ.\n\n"); //Defines error message.
}
else
{
//Prints number of pairs read.
printf("\n %d data pairs read from the file. \n\n", no_of_poi_read);
} // End IF statement.
system ("PAUSE");
} // END OF MAIN
you have defined function read_data_from_file() within function main() -- C does not allow nested functions. Move that function from main()
Code:int main()
{
// blabla
}
int read_data_from_file(...)
{
// blabla
}