0

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?

5
  • 1
    void print(int arr[], int n){ and struct Person arr[] = {p1}; The code attempts to pass an array of Person structs... Not integers... Commented Sep 29, 2022 at 0:55
  • what could i replace int with then? char? Commented Sep 29, 2022 at 1:00
  • @Fe2O3 Or would it work if I passes arr[].age instead? because that is an int. Commented Sep 29, 2022 at 1:02
  • 1
    Your print function needs to take an array of structs because that's the kind of array you have. Commented Sep 29, 2022 at 1:12
  • 1
    And the printf function needs a format string that matches the thing you want to print. If you want to print the age you need %d because the age member of the struct is an int. Commented Sep 29, 2022 at 1:14

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm still getting an unknown typename after changing to this
@DevinBowen The code above works - here is a runabble version: onlinegdb.com/Y-BrUPnTUy
@DevinBowen You probably didn't notice in this VG sample code that the struct is declared ahead of any use of its name. You may have changed the print function, but not moved the declaration ahead of the print function. Just guessing...
I got it to work. the issue i had was I kept the struct below my function. thank you
@aleksa Coffee is on you next time... :-)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.