简体   繁体   English

尝试在函数中传递地址时收到警告

[英]Getting warning when trying to pass the address in a function

I get the warning: 我得到警告:

expected 'const CHAR *' but argument is of type 'CHAR (*)[18]'

Why is the compiler giving me this warning? 为什么编译器会向我发出此警告? This is the line I am using to call to the api: 这是我用来调用api的行:

AG_AS(g_AgMgrCtx.audio_device.Profile_Type,&g_AgMgrCtx.SDevAddr);

The declaration of the function is 该函数的声明是

AG_AS(AG_DType d_type , CHAR *addr);
char SDevAddr[18];

What is the problem with passing the address? 通过地址有什么问题? When I remove & the warning goes away. 当我删除&警告消失。

What does the warning actually mean? 该警告实际上是什么意思?

That means your call should be (without the & ): 这意味着您的呼叫应该是(不带& ):

   AG_AS(g_AgMgrCtx.audio_device.Profile_Type, g_AgMgrCtx.SDevAddr);

g_AgMgrCtx.SDevAddr is an array of 18 chars. g_AgMgrCtx.SDevAddr是18个字符的数组。 So when you pass it to a function, it gets converted into a pointer to its first element (thus matching the type that AG_AS() expects). 因此,当您将其传递给函数时,它将转换为指向其第一个元素的指针(因此与AG_AS()期望的类型匹配)。

Relevant: What is array decaying? 相关: 什么是阵列衰减?

When you have an object like this 当你有一个这样的对象

T obj;

where T is some type then expression &obj has type T * that is it is a pointer to an object of type T . 其中T是某种类型,则表达式&obj类型为T * ,即它是指向T类型的对象的指针。

This array declaration 此数组声明

char SDevAddr[18];

can be transformed to the form shown above using a typedef. 可以使用typedef转换为上面显示的形式。 For example 例如

typedef char T[18];
T SDevAddr; 

In this case expression &SDevAddr has type T * as it has been pointed above. 在这种情况下,表达式&SDevAddr类型为T * ,如上所述。 however in this case T in turn an alias for the type char[18] . 但是,在这种情况下, T继而为char[18]类型的别名。 Thus &SDevAddr is a pointer to an object of type char[18] Its type looks like char ( * )[18] . 因此, &SDevAddr是指向类型为char[18]的对象的指针。其类型类似于char ( * )[18]

However as it is seen from the function declaration 但是,从函数声明中可以看出

AG_AS(AG_DType d_type , CHAR *addr);

the second parameter has type char * . 第二个参数的类型为char * If you will pass as an argument the array itself like SDevAddr then it will be implicitly converted to pointer to its first element and will have type char * that is required for the function call. 如果将像SDevAddr这样的数组本身作为参数传递,则它将隐式转换为指向其第一个元素的指针,并且将具有char *类型,这是函数调用所必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM