Member Avatar for I_m_rude
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.

Recommended Answers

All 10 Replies

It will be "nitin". s can't be a null pointer because it's an array.

Member Avatar for I_m_rude

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.

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.

Member Avatar for I_m_rude

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

f(&i) has no effect on i.

Member Avatar for I_m_rude

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

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.

Member Avatar for I_m_rude

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 ?

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.

Member Avatar for I_m_rude

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.