简体   繁体   English

如何将结构数组元素作为参数传递给函数?

[英]How to pass a structure array element as argument to function?

#include<stdio.h>
struct Ques
{
  int a;
}Q[5];
void sort(int a[])
{
 printf("any sort technique...");
}
void main() 
{
 sort(Q.a);
}

So this is the sample code.所以这是示例代码。 I Want to access the whole struct element as array.我想以数组的形式访问整个结构元素。

You want this:你要这个:

#include<stdio.h>

struct Ques
{
  int a;
} Q[5];

void sort(struct Ques array[], int size)
{
   printf("any sort technique...");

   // just some demo
   for (int i = 0; i < size; i++)
     printf("array[%d].a = %d\n", i, array[i].a);
}

int main() 
{
 // put some data into Q
 sort(Q, 5);
}

sort needs two parameters: sort需要两个参数:

  • the pointer to the array to sort指向要排序的数组的指针
  • the size of the array (unless you only ever want to sort array of some fixed size数组的大小(除非您只想对某个固定大小的数组进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM