1

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?

2
  • instead of NULL==pdetails->a use strlen(pdetails->a)==0 Commented Jul 2, 2013 at 10:40
  • I hope a,b and c are to show the general case and not what's in you code. Also, main() should be, at least, some variation of returnType main(){...} if not main(void) generally int main(void){...} 'cause why not? Commented Jul 2, 2013 at 10:40

5 Answers 5

1

when this Details_t var; is executed, var.a and var.b own address. So var.a == NULL will return false.

Sign up to request clarification or add additional context in comments.

1 Comment

followup: the point is that the Details_t::a are ARRAYS not POINTERS. There's a real difference. Since the array is embedded inside the structure, it has an address almost always, and actually the IF condition is always true in your case. The memset will indeed make the whole memory zeroed out, but there are no pointers in the structure. After memset you will effectively have a structure with three arrays inside and the contents of those arrays will consist of 32 zeroed characters. You may exploit that! Hence, instead of pdetails->a != NULL just check pdetails->a[0] == NULL.
0

all the members which are not copied should be NULL

Thats not true, test this piece of code:

#include <stdio.h>
#include <string.h>

typedef struct Details{
    char a[32];
    char b[32];
    char c[32];
} Details_t;

int main(void)
{
    Details_t var, *pt;

    memset((void *)&var, '\0', sizeof(Details_t));
    printf("%s\n", var.a == NULL ? "null" : "not null"); /* output = not null */
    /* Also for a pointer */
    pt = &var;
    printf("%s\n", pt->a == NULL ? "null" : "not null"); /* output = not null */
    /* Maybe you want: */
    printf("%s\n", pt->a[0] == '\0' ? "empty" : "not empty"); /* output = empty */
    return 0;
}

Other minor problems:

int main(void) instead of main()

Don't cast calloc

_t suffix is reserved for POSIX

Comments

0

How are you able to compile with given Code, there should be

len = strlen(pdetails->a) + strlen(pdetails->b); rather than just accessing a and b which is not defined in function xyz.

Similar change needed in Line strcpy(newString,a); and strcpy(newString,b);

Now for your question comparing NULL==pdetails->a says checking address of a[0] is NULL or not which will never be TRUE.pdetails have got memory on stack so its elements too.

1 Comment

that was my writing mistake
0

Check out if this can help you :)

#include "stdafx.h"
#include<stdio.h>
typedef struct Details{
char a[32];
char b[32];
char c[32];
}Details_t;

char *xyz(Details_t *pdetails)
{
 if((strlen(pdetails->a)==0) && strlen(pdetails->a)==0)
 {
  return NULL;
 }
 int len = 0;
 char *newString = NULL;
  len = strlen(pdetails->a) + strlen(pdetails->a);
 newString  = (char *)calloc(1,len +3);
 strcpy(newString,pdetails->a);
 strcat(newString,";");
 strcat(newString,pdetails->a);
 strcat(newString,";");

 return newString;
}
//Now I am passing the address of this structure from main().

void 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);

}

Comments

0
  • Why this condition gets failed ?

Because a and b are not pointers. They are arrays. You can not assign NULL to them. You can not assign anything to array at all.

This condition should work for you:

if( 0 == pdetails->a[0] + pdetails->b[0] )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.