I have a code as follows:
typedef struct Details {
char a[32];
char b[32];
char c[32];
} Details_t;
char *xyz(Details_t *pdetails) {
if ((NULL == pdetails->a) && (NULL == pdetails->b)) {
return NULL;
}
int len = 0;
char *newString = NULL;
len = strlen(a) + strlen(b);
newString = (char *)calloc(1, len + 3);
strcpy(newString, a);
strcat(newString, ";");
strcat(newString, b);
strcat(newString, ";");
return newString;
}
Now I am passing the address of this structure from main().
main() {
char *ret = NULL;
Details_t var;
memset((void *)&var, '\0', sizeof(Details_t));
strcpy(var.b, "EXAMPLE");
ret = xyz(&var);
printf("OUTPUT==%s\n", ret);
}
My problem is: I am not copying any value in member a and I Have memset() structure details with NULL so all the members which are not copied should be NULL. But in xyz function,the condition below gets failed.
if ((NULL == pdetails->a) && (NULL == pdetails->b))
and output which I get is below:
OUTPUT==;EXAMPLE;
Why this condition gets failed?
NULL==pdetails->ausestrlen(pdetails->a)==0a,b and care to show the general case and not what's in you code. Also,main()should be, at least, some variation ofreturnType main(){...}if notmain(void)generallyint main(void){...}'cause why not?