How to set a function's return value= to a variable?

This is a discussion on How to set a function's return value= to a variable? within the C Programming forums, part of the General Programming Boards category; Code: int get_corners(FILE *fp,double x_array[],double y_array[],int macro) { int i=0; int j=0; while(fscanf(fp,"%lf%lf",&x_array[i],&y_array[j])!=EOF) { i++; j++; } fclose(fp); int actual_size=j; ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    140

    How to set a function's return value= to a variable?

    Code:
    int get_corners(FILE *fp,double x_array[],double y_array[],int macro)
      {
      int i=0;
      int j=0;
     
      while(fscanf(fp,"%lf%lf",&x_array[i],&y_array[j])!=EOF)
      {
      i++;
      j++;
      }
      fclose(fp);
      int actual_size=j;
      printf("actual_size %d", actual_size);
      return actual_size;
    So basically i tested it and the return value is 7, which is correct.
    How would i be able to pass that value (7) to the next function?
    actual_size=7

    Code:
    void output_corners(FILE*foutput,double x_array[],double y_array[],int *coordinates)
      {
      int i=0;
      int j=0;
     
      for(i=0;i<actual_size;i++)             // But here it says actual_size is undeclared
      fprintf(foutput,"%1lf",*x_array);
     
      }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,217
    "actual_size" is declared in the "get_corners()" function, and only lives within the scope of that function. That's why it is saying that it's undeclared in the "output_corners()" function.

    The value returned from the "get_corners()" function simply needs to be passed to the "output_corners()" function.

    Code:
    int size;
    
    size = get_corners(...);
    
    output_corners(...,size);
    // be sure to change the argument list in the function
    // declaration and definition to match this additional variable

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Quote Originally Posted by Matticus View Post
    "actual_size" is declared in the "get_corners()" function, and only lives within the scope of that function. That's why it is saying that it's undeclared in the "output_corners()" function.

    The value returned from the "get_corners()" function simply needs to be passed to the "output_corners()" function.

    Code:
    int size;
    
    size = get_corners(...);
    
    output_corners(...,size);
    // be sure to change the argument list in the function
    // declaration and definition to match this additional variable
    Hey,
    Code:
    int actual_size=get_corners(fp,x_array,y_array,macro);
      printf("coordinates %d",actual_size);
    it prints out the memory addresses. why is that?
    Last edited by tmac619619; 11-06-2012 at 12:23 PM.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    140
    Thank you Matt. It works!

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,217
    No reason I can see in the code you've provided. Can you post more of the program?

    [edit] Never mind. You're welcome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable shows NAN after return from function call
    By CodeKate in forum C Programming
    Replies: 21
    Last Post: 11-18-2010, 10:58 AM
  2. Can a function return char variable?
    By mystic in forum C Programming
    Replies: 10
    Last Post: 07-13-2010, 01:05 AM
  3. Replies: 3
    Last Post: 10-13-2009, 08:49 AM
  4. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  5. Replies: 10
    Last Post: 09-27-2005, 12:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21