简体   繁体   English

将结构数组传递给 C 中的 function

[英]Passing an array of structs to a function in C

I have an assignment to finish and I am getting caught up on a pointer issue.我有一项任务要完成,但我陷入了指针问题。 I have an array of structs that I created and want to send it to a sort function. Before I send it to the sort function I want to send it to a print function to verify that the pointer is working.我有一个我创建的结构数组,想将它发送到 function 排序。在我将它发送到 function 排序之前,我想将它发送到打印 function 以验证指针是否正常工作。 Here is the code I have...这是我的代码......

void print(int arr[], int n) {
    int i;
    for (i = 0; i < n; i++) {
        printf("%s ", arr[i]);
    }
    printf("\n");
}

//------------------------------

struct Person {
    int age;
    char name[10];
};

//------------------------------

int main() {
    struct Person p1 = {26, "devin"};
    struct Person arr[] = {p1};

    int n = sizeof(arr) / sizeof(arr[0]);

    printf("--Original Array--\n");
    print(arr, n);

The error I am running into when I try to compile is我尝试编译时遇到的错误是

a1q4.c: In function ‘print’:
a1q4.c:24:18: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
   24 |         printf("%s ",arr[i]);
  |                 ~^   ~~~~~~
  |                  |      |
  |                  char * int
  |                 %d

I'm not sure how pointers work very well, I have tried using * in front of arr[i] but it won't work.我不确定指针如何工作得很好,我试过在arr[i]前面使用*但它不起作用。 Any ideas?有任何想法吗?

You could try something like this:你可以尝试这样的事情:

#include <stdio.h>

typedef struct Person
{
    int age;
    char name[10];
} Person;

void print(Person* arr, int n)
{
    for (int i = 0; i < n; ++i)
    {
        printf("%s ",arr[i].name);
    }
    printf("\n");
}

int main()
{
    Person p1 = {26, "devin"};
    Person arr[] = {p1};
    
    int n = sizeof(arr) / sizeof(arr[0]);
    
    printf("--Original Array--\n");
    print(arr, n);
}

Why not add another version with some commentary...为什么不添加带有一些评论的另一个版本...

#include <stdio.h>  // declarations AHEAD of use

typedef struct {  // same thing: declarations AHEAD of use
    char name[10]; // NB reversed order
    int age;
} Person_t; // a user defined 'datatype'

void print( Person_t p[], size_t n ) {
    for( size_t i = 0; i < n; i++ )
        printf( "#%d: %s %d\n", i, p[i].name, p[i].age );
}

int main() {
    Person_t arr[] = { // more people
        { "devin", 26 },
        { "foo", 42 },
        { "bar", 24 },
    };

    // int n = sizeof(arr) / sizeof(arr[0]);
    // parentheses only necessary for datatypes
    // not necessary for instances of data
    int n = sizeof arr/ sizeof arr[0];

    printf("--Original Array--\n");
    print( arr, n );

    return 0;
}
--Original Array--
#0: devin 26
#1: foo 42
#2: bar 24

Whether or not by accident, your print() function definition served as its own declaration .无论是否出于偶然,您的print() function定义都充当了它自己的声明 Had that function followed main() you would have required a "function prototype" (a declaration of the function's name and parameters and return type) for the compiler to be happy.如果 function跟在main()之后,您将需要一个“函数原型”(函数名称和参数以及返回类型的声明)以使编译器满意。 Same thing with user defined datatypes .用户定义的数据类型相同。 They must be declared ahead of any use of the type's name (ie: defining and instance or accessing one).它们必须在任何类型名称的使用之前声明(即:定义和实例或访问一个)。

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

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