【问题标题】:Passing Pointer Array Structure to function将指针数组结构传递给函数
【发布时间】:2019-05-03 01:11:38
【问题描述】:

我想将一些数组结构传递给一个函数,以便我可以按年龄重新排序。不知道怎么打。

   typedef structure {
    char name[100];
    int age;
    }person;

    person function ( How do I pass them? ) 
{
reordering;
}
    int main(){

    printf("How many ppl?");
     ...
    person *v[n]; ( I decided to not use dynamic allocation for simplicity of example
    for(i=0;i<n;i++)
    {
    // Names and age reading using arrays
    v[i].name
    v[i].age
    }

      function ( ??? );

我想按年龄重新排序。如何将我的数组作为指针传递给将根据人们的年龄重新排序的函数?

【问题讨论】:

    标签: c arrays function pointers


    【解决方案1】:

    例如

       void function(type* array)
       {
         // codes
       }
    
       void func_call()
       {
         type array_to_pass[];
         function(array_to_pass);
       }
    

    这将起作用,因为数组将衰减为指向类型的指针。 请注意,您不再知道数组的大小。您可能需要传入数组的大小。

    【讨论】:

      猜你喜欢
      • 2021-11-02
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多