这更多的是出于好奇,而不是我希望或需要它的工作方式。但当我做一些模特儿想测试一些东西的时候,我最终得到了这样的结果.不知道为什么不像我预料的那样起作用。
typedef struct {
char *a;
char *b;
char *c;
}mystruct;
void init_chars (char *arg)
{
arg = malloc (sizeof (char)*10);
arg = "0123456789";
printf ("%s\n", arg);
}
int main ()
{
mystruct *msp = malloc (sizeof (mystruct));
init_chars (msp->a);
init_chars (msp->b);
init_chars (msp->c);
printf ("%s, %s, %s\n", msp->a, msp->b, msp->c);
return 0;
}指纹..。
0123456789
0123456789
0123456789
(null),(null),(null)发布于 2015-08-26 05:56:50
typedef struct {
char *a;
char *b;
char *c;
}mystruct;
void init_chars (char **arg)
{
*arg = malloc (sizeof (char)*11);
if(*arg == NULL)
{
printf("malloc failed!");
exit(-1);
}
strcpy(*arg,"0123456789");
printf ("%s\n", *arg);
}
int main ()
{
mystruct *msp = malloc (sizeof (mystruct));
if(msp == NULL)
{
printf("malloc failed");
exit(-1);
}
init_chars (&msp->a);
init_chars (&msp->b);
init_chars (&msp->c);
printf ("%s, %s, %s\n", msp->a, msp->b, msp->c);
free(msp->a);
free(msp->b);
free(msp->c);
free(msp)
return 0;
}产出:
0123456789
0123456789
0123456789
0123456789,0123456789,0123456789
与您的代码进行比较,您将了解为什么
https://stackoverflow.com/questions/32218726
复制相似问题