0
main()
{

      char s[19];

      strcpy(s,"\0");

      if(s==NULL)
      printf("daniweb\n");
      else
      printf("nitin\n");

      return 0;
}

Can anyone tell me what is expected o/p of this and why ? help! thanks in advance.

2
Contributors
10
Replies
12
Views
5 Years
Discussion Span
Last Post by I_m_rude
Featured Replies
0

now, thanks for replying as i waiting for that. now, sir, what will be content of s ? isn't it two null characters ?

and secondly,

int f(int *a)
{
int b=5;
a=&b;
}
main()
{
int i;
printf("\n %d",i);
f(&i);
printf("\n %d",i);
}

what is o/p for this ? i am confused in this a little bit.

Edited by I_m_rude

0

isn't it two null characters ?

No, because strcpy() stops at a null character.

what is o/p for this ?

It's undefined in a big way. Both printf() statements are trying to print an uninitialized variable. f() doesn't do jack diddly to i, as a is a copy of the address of i. You're only modifying the copy, and the original remains unchanged.

0

will you please again explain your last line in the last post ? f() doesn't do jack diddly.....

0

but , it is not playing with value of i, rather it is playing with value of a. then why u saying that ?

0

but , it is not playing with value of i, rather it is playing with value of a.

Correct.

then why u saying that ?

Because changing where a points to has absolutely no effect on i. If you had dereferenced a and copied the value of b into it then you would have indirectly initialized i:

int f(int *a)
{
    int b = 5;
    *a = b;
}

But the f() you posted is completely nonsensical as it only works with the local pointer, does nothing else, and immediately loses all of those changes when the local objects are destroyed upon return.

0

james sir, tell me one thing, when i am storing address of local variable in a , then after that object destroys, then a will still have its adress na ?

0

when i am storing address of local variable in a , then after that object destroys, then a will still have its adress na ?

a is a local variable too, it's destroyed at the same time as b. So the answer is no, because a no longer exists.

Edited by deceptikon

0

ohh yes yes! damn good. okies i got it! sir , what is the difference between run time eror and semantic error ?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.