Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does 'p2=p1+(!!p3)' mean? 1

Status
Not open for further replies.

pjw001

Technical User
Aug 12, 2002
297
GB
The fields are defined as

char *p1,*p2,p3;

What does the statement

p2=p1+(!!p3);

do?

The program does work but I cannot find any documentation that defines the meaning of '!!'.

Thanks in anticipation.
 
Hi,

p2=p1+(!!p3);

In this statement you are incrementing the address contain in p1 by 1 or 0 based on the initial value in p3 and finally storing in p2.

For example p3 contains other that '\0' it means !p3 is non zero and !!p3 is again equal to 1, so statement will store the address contain in p1 plus 1 in p2.

Now other example p3 contains '\0' it means !p3 is equal to 1 and !!p3 is equal to 0, so statement will do nothing but copy the p1 in p2.

Just let me know if you have any doubt.


 
Thank you very much.

I had actually considered the concept of the ! signs effectively reversing their effect in some way, but decided that this was too silly to persue!

Bring back cobol, all is forgiven.


 
This example will help you to get the meaning.

#include <stdio.h>

void main()
{
char *p1 = &quot;AMAR DEEP GAUR&quot;;
char *p2 = NULL;
char p3;

p3 = '\0';
p2=p1+(!!p3);

printf(&quot;when p3 is null p2 is pointing to the character '%c'\n&quot;, *p2);

p3 = 'A';
p2=p1+(!!p3);

printf(&quot;when p3 is not null p2 is pointing to the chargacter '%c'\n&quot;, *p2);
}


Output is

when p3 is null p2 is pointing to the character 'A'
when p3 is not null p2 is pointing to the chargacter 'M'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top