我无法在参数传递过程中发现此错误。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef char my_char;
void myfunc(const my_char** data)
{
printf ("%s\n", *data);
printf ("%s\n", *(data + 1));
}
int main(){
char **mydata;
mydata = malloc(sizeof(char*)*2);
mydata[0] = malloc(sizeof(char)*50);
mydata[1] = malloc(sizeof(char)*50);
memset(mydata[0],'\0',50);
memset(mydata[1],'\0',50);
strcpy (mydata[0], "Hello");
strcpy (mydata[1], "world");
myfunc((my_char**)mydata);
free (mydata[0]);
free (mydata[1]);
free (mydata);
return 0;
}
它正常工作。但是当我显式键入参数时发出警告。为什么会这样呢?显示的警告是:
warning: passing argument 1 of ‘myfunc’ from incompatible pointer type
据我所知,类型转换应该可以帮助编译器理解指针所保持的数据类型。但是这里根本没有帮助。
使用const
而类型转换数据类型。
myfunc((const my_char**)mydata);
您正在const
通过函数获得该值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句