Home > OS >  Passing an array of structs to a function in C
Passing an array of structs to a function in C

Time:09-29

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. 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. Any ideas?

CodePudding user response:

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);
}

CodePudding user response:

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. 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. 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).

  • Related