jinal shah
jinal shah

Reputation: 122

Behavior of array

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?

Upvotes: 1

Views: 124

Answers (5)

kotlomoy
kotlomoy

Reputation: 1430

  • 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] )

Upvotes: 0

David Ranieri
David Ranieri

Reputation: 41017

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

Upvotes: 0

Scy
Scy

Reputation: 498

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

Upvotes: 1

Ayse
Ayse

Reputation: 2754

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

}

Upvotes: 0

Dayal rai
Dayal rai

Reputation: 6606

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.

Upvotes: 0

Related Questions