开发者

access array content from a pointer returned from function

开发者 https://www.devze.com 2023-04-07 14:26 出处:网络
I tried to write a program which used a function to read the data, store in an array and then tried to return the array to main() for the sorting process.

I tried to write a program which used a function to read the data, store in an array and then tried to return the array to main() for the sorting process. Here's what I have

     #include <stdio.h>
     #include <stdlib.h>
     #define RESERVED_ARRAY_SIZE 100

     int* read_data_from_files()
      {
      int* arr = (in开发者_Go百科t*) malloc(sizeof(int) * 50);

      int arr_count;
    int status;
    int i,j ;

    arr_count =0;
    i=0;
    FILE *cfPtr;
    if ((cfPtr = fopen ("score.dat", "r"))== NULL)
     {
      printf("File cannot be opend\n");
      }
    else
     {         
       fscanf(cfPtr, "%d", &arr[i]);
       printf("%5d%5d\n", i, arr[i]);

       while (!feof (cfPtr))
       {                  
             ++i;   
             ++arr_count;
             status = fscanf(cfPtr, "%d", &arr[i]);

             if (status == EOF)
             {
                     break;
                     }

         else if (status != 1)
         {
              fprintf (stderr, "Error input\n");
              getchar();                  
              }

         if (arr_count >= RESERVED_ARRAY_SIZE)
         {
                         fprintf(stderr, " The number of array is over.\n");
                         getchar();                           
                         }

             printf("%5d%5d\n", i, arr[i]);               
       }
       fclose (cfPtr);  
       }  
       printf("arr_count=%d\n", arr_count);
    return arr;
    }

 int main()
 {
   int i;
   int arr_count;
   int* arr = (int*) malloc(sizeof(int) * 10);
   arr = read_data_from_files();
   arr_count = sizeof(arr)/sizeof(arr[0]);
   printf("this check the size of array%d", arr_count);
   for (i=0; i<9; i++)
   {
   printf("%d   ", i);
   printf("%d", *arr[i]);
   }
   selection_sort(arr, arr_count);

   getchar();
   return 0;
   }

My questions are 1. If functions cannot return 2 values, what shuold I do to pass the array size(the arr_count in my program) in the funcution to the main()? 2. If the function only returns the pointer of the array to the main(), can I figure out the size of the array in the main? what should I do ? Thans a lot for your assistants.


No, you cannot figure out the size automatically. The standard way to return multiple values is to pass a pointer to a variable in which you want to store the extra return value:

int* read_data_from_files(int *result_size)
{
   ...
   *result_size = arr_count;
   ...
   return arr;
}

int main()
{
  int arr_count;
  int *arr = read_data_from_files(&arr_count);
  ...
}


You could make a variable size in the main function, and have the function call pass the address to read_data_from_files() as an additonal argument. read_data_from_files() can then modify the value from inside the function, and you won't need to return it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号