简体   繁体   English

如何循环并打印 c 中的结构数组?

[英]How do I loop and print through an array of structs in c?

I am writing a function separate from main that reads in an array of structs and prints each array element's struct elements.我正在编写一个与 main 分开的函数,该函数读取结构数组并打印每个数组元素的结构元素。 My problem however is the condition statement for looping though this array to print each element.然而,我的问题是循环这个数组以打印每个元素的条件语句。 sizeof(a)/sizeof(a[]) just doesnt work in the function because it is equal to zero within the prototype. sizeof(a)/sizeof(a[]) 在函数中不起作用,因为它在原型中等于 0。 Is there something i am missing?有什么我想念的吗? I've tried pointer arithmetic and it does not work either.我试过指针算法,但它也不起作用。

void printNames( person a[]){

    int i;

    for(i = 0;i<sizeof(a)/sizeof(a[0]);i++)
        printf("Name: %s | Age: %d\n", a[i].name, a[i].age);

}

int main(int argc, char *argv[]){

    if (argc == 1 || argc%2 == 0){
        printf("Invalid arguments.\n Usage: %s name1 age1 name2 age2 ...", argv[0]);
        return 0;
    }

    printf("You have entered %d person(s) into the program.\n", argc/2);
    person people[argc/2];

    int i;

    for (i = 0; i<argc/2;i++){
        strcpy(people[i].name, argv[i*2+1]);
        people[i].age = atoi(argv[i*2+2]);

    if(people[i].age <= 0){
        printf("Invalid age <= 0. Try again.");
        return 0;
    }


}

a becomes a pointer when passed as parameter to a function, so it cannot figure out the size of the array just from having this pointer, you have to inform the function about its size:当作为参数传递给函数时, a变成了一个指针,所以它不能仅仅通过这个指针来确定数组的大小,你必须通知函数它的大小:

void printNames(person *a, size_t num)
{
    int i;
    for (i = 0; i < num; i++)
        printf("Name: %s | Age: %d\n", a[i].name, a[i].age);
}

Then call printNames from main like this:然后像这样从main调用printNames

printNames(people, sizeof(people)/sizeof(*people));

In C, array parameters are decayed into pointers.在 C 中,数组参数衰减为指针。 So, the expression sizeof(a)/sizeof(a[0]) becomes sizeof(int *)/sizeof(int) which results in 1 (assuming the size of int and int * is 4) and hence the for loop within the printNames() is executed only once regardless of the size of the array.因此,表达式sizeof(a)/sizeof(a[0])sizeof(int *)/sizeof(int)其结果在1(假设的尺寸intint *为4),因此for内环路无论数组的大小如何, printNames()都只执行一次。

Therefore, sizeof shouldn't be used to obtain the number of elements in these cases.因此,在这些情况下,不应使用sizeof来获取元素的数量。 Instead, we can pass an additional parameter for array size to printNames() .相反,我们可以将数组大小的附加参数传递给printNames()

Consider the following C program:考虑以下 C 程序:

#include<stdio.h>
void func(int array[])  
{
  int i;   
  int arraySize = sizeof(array)/sizeof(array[0]); /* arraySize <-- 1<-- 4/4 */
  for (i = 0; i < arraySize; i++) 
  {  
    array[i] = i; 
  }
}

int main()
{
  int i;  
  int array[4] = {0, 0 ,0, 0};
  func(array);

  for(i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
    printf(" %d " ,arr[i]);

  getchar();  
  return 0;
}    

Output: 0 0 0 0 on a Intel Architecture-32 machine.输出:0 0 0 0 在Intel Architecture-32机器上。

So the corrected program is:所以修正后的程序是:

#include<stdio.h>
void func(int array[], size_t arraySize)  
{
  int i;   
  for (i = 0; i < arraySize; i++) 
  {  
    array[i] = i;  
  }
}

int main()
{
  int i;  
  int array[4] = {0, 0 ,0, 0};
  func(array, 4);

  for(i = 0; i < sizeof(array)/sizeof(array[0]); i++)
    printf(" %d ", array[i]);

  getchar();  
  return 0;
}    

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

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