这是有效/正确的代码,还是违反标准——可能会调用未定义的行为(UB)?
void *--> 具体来说,是to char **in的转换foo()。<--
它确实有效,但我们知道“有效”还不够好。
以及任何其他错误,请撕掉它并发布更正的代码(如果您愿意的话)。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
void foo(void *vp)
{
char **dp = (char **)vp;
int i;
for(i=0; i<SIZE; i++)
{
printf("%s", dp[i]);
}
}
int main()
{
char **dp;
int i;
dp = malloc(SIZE * sizeof(char *));
for(i=0; i<SIZE; i++)
{
dp[i]= malloc(20);
sprintf(dp[i],"Now: %d\n",i);
}
foo(dp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
void foo(void *vp)
{
char …Run Code Online (Sandbox Code Playgroud)