MineR
MineR

Reputation: 2204

How do a specify an argument of "const MyClass *varName[]"?

There's a func in a third party library which looks like this:

void MyFunc(const MyClass *varName[])

What is an example of what I can pass in as an argument to that function?

This doesn't work:

MyClass* b[1] = { a };

MyFunc(b)

Although intellisense says nothing, upon compilation, I get an error: "... cannot convert from 'MyClass *[1]' to 'const MyClass *[]'"

Upvotes: 0

Views: 29

Answers (1)

Eternal Dreamer
Eternal Dreamer

Reputation: 483

A (constant) pointer to an array of MyClass

#include <stdio.h>


typedef struct {
    int id;
    char *name;
} MyClass;

void print_person(const MyClass *p[]) {
    printf("%i: %s", p[0]->id, p[0]->name);
}

int main() {
    MyClass p[] = {
        { 1, "Alice" }
    };

    const MyClass *p2 = p;

    print_person(&p2);

    return 0;
}

And to answer your interrogation, if you do this ;

MyClass* b[1] = { a };

I don't know what's a, but it shouldn't possible to instantiate a pointer this way. Or maybe you wanted do something like this ;

MyClass a = { 1, "Alice" };
MyClass *p[1] = { &a };

Upvotes: 1

Related Questions