最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

c++ cli - How do a specify an argument of "const MyClass *varName[]"? - Stack Overflow

matteradmin6PV0评论

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 *[]'"

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 *[]'"

Share Improve this question asked Nov 16, 2024 at 16:51 MineRMineR 2,20413 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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 };
Post a comment

comment list (0)

  1. No comments so far