Sign in to follow this  

Need to use the data in this array from another function, in C

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to read in 10 decimal numbers from a file into an array. I've got this. Now I need to use this array in other functions. I believe I need to use pointers, but I don't quite get them.
/*****************************/
int FILL_PAY_RATES (int temp){
/*****************************/

double paytype[10];

int i = 1;

FILE* pay; 
pay = fopen("payrates.txt", "r"); 
if (pay == NULL){ /* can't find file */
    printf("Cannot open payrates.txt");
  }
  else { /*file opened */
    for(i = 0; i < 10; i++){ 
    fscanf(pay, "%lf\n", &paytype[i]);
    printf("%.2lf\n", paytype[i]);
    }
  } /*eof else - file opened*/

fclose(pay); 

return (0);
}
/*****************************/

Share this post


Link to post
Share on other sites
If you are working with C or C++ I would look into getting a book on beginning C or C++. Sam's teach yourself C in 21 days is fine for beginners and is easily explained. There will be a TON of other ppl who like other books so take with it what u will. I only say this because pointers are a HUGE part of C and C++.

I also beg to ask, why C and not C++? Are u doing programming for ICs? or software?

I suck at searching, I dont posses the google strength. But anywho here is a link not sure how good it is...

http://home.netcom.com/~tjensen/ptr/pointers.htm

EDIT: Gamedev link

http://www.gamedev.net/reference/articles/article1697.asp

Share this post


Link to post
Share on other sites
I'm using C (not C++) because thats what our professor is teaching first semester in college. This is for an assignment and I've read several google links about pointers, but they seem to behave differently when I attempt to apply them to double arrays.

If some one could help my with an explanation of what I'm specifically trying to do, that would really help me out.

Share this post


Link to post
Share on other sites
You could change your function so it gets an array to fill.... like this.

(i removed the temp parameter since you don't seem to be using it).


/*****************************/
int FILL_PAY_RATES (double paytype[10]){
/*****************************/

int i = 1;

(code here remains the same)

return (0);
}
/*****************************/



And in your main or other function that needs these numbers just do:


int i;
double numbersHere[10];

FILL_PAY_RATES(numbersHere);

for(i=0; i < 10; i++)
printf("%.2lf ", numbersHere[i]);

Share this post


Link to post
Share on other sites
Quote:
Original post by desdemian
You could change your function so it gets an array to fill.... like this.

(i removed the temp parameter since you don't seem to be using it).


/*****************************/
int FILL_PAY_RATES (double paytype[10]){
/*****************************/

int i = 1;

(code here remains the same)

return (0);
}
/*****************************/



And in your main or other function that needs these numbers just do:


int i;
double numbersHere[10];

FILL_PAY_RATES(numbersHere);

for(i=0; i < 10; i++)
printf("%.2lf ", numbersHere[i]);



Thanks a bunch, that worked.

Share this post


Link to post
Share on other sites
Well you have to realize that if you want the pointer, then access it like so:

int *intpointer = null;
int setint = 8;
intpointer = &setint;//sets intpointer's pointer location to setints. if either of the 2 change, they will contain the same value. like so...

setint = 7;//now both setint and intpointer's value will be 7.

unless,

intpointer = null; // now its not set to anything again.


if you want to access what its pointing to then use:

int newint = *intpointer;// this sets the newint to the value of the pointer by dereferencing it. So now newint's value is the same as intpointer, but their memory locations are different, so they can each be changed seperatly.






Share this post


Link to post
Share on other sites

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this