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

There's a func in a third party library which looks like this:void MyFunc(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 *[]'"

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745650740a4638248.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信