-sharpinclude <stdio.h>
-sharpinclude <malloc.h>
-sharpinclude <string.h>
typedef struct _Info1
{
int value;
char name[64];
}Info1,*pInfo1;
typedef struct _Info2
{
int value;
char *name;
}Info2,*pInfo2;
typedef struct _Info3
{
int value;
size_t length; //lengthname
char name[1];
}Info3,*pInfo3;
int main()
{
Info1 *pi1 = (Info1 *)malloc(sizeof(Info1));
if (pi1 == NULL)
{
goto err;
}
memset(pi1, 0,sizeof(Info1));
strcpy_s(pi1->name, 64, "lucy");
pi1->value = 78;
Info2 *pi2 = (Info2 *)malloc(sizeof(Info2));
if (pi2 == NULL)
{
goto err;
}
memset(pi2, 0, sizeof(Info2));
size_t len = sizeof("lucy") + 1;
pi2->value = 78;
pi2->name = (char *)malloc(len);
if (pi2->name == NULL)
{
goto err;
}
memset(pi2->name, 0, len);
strcpy_s(pi2->name, len, "lucy");
Info3 *pi3 = (Info3 *)malloc(sizeof(Info3)+strlen("lucy")+1-1);
if (pi3 == NULL)
{
goto err;
}
memset(pi3, 0, sizeof(Info3) + strlen("lucy") + 1 - 1);
pi3->value = 78;
pi3->length = strlen("lucy") + 1;
strcpy_s(pi3->name, pi3->length, "lucy");
printf("1:score:%d ,name:%s\n", pi1->value, pi1->name);
printf("2:score:%d ,name:%s\n", pi2->value, pi2->name);
printf("3:score:%d ,name:%s\n", pi3->value, pi3->name);
err:
if (pi1)
free(pi1);
if (pi2)
free(pi2);
if (pi2->name)
free(pi2->name);
if (pi3)
free(pi3);
return 0;
}
error indicates that pi2 and pi3 are not initialized.