Why would you come back (0 * ap ++)?

This is K & RC, and here is the whole code: http://v6shell.org/history/if.c

Take a look at this feature:

char *nxtarg() { if (ap>ac) return(0*ap++); return(av[ap++]); } 

1.Question: Why return (0 * ap ++)? Ok, you want to return 0 and increase ap by 1. But why so? This is faster than if (ap>ac) {ap++; return 0;} if (ap>ac) {ap++; return 0;} ?

2.Question: The return type of nxtarg should be char * , why can you return 0, an integer?

+6
source share
2 answers

This is a small trick to compress the increment into a statement that returns zero. This is logically equivalent to conditional

 if (ap > ac) { ap++; return 0; } 

or even better with a comma :

 return (ap++, (char *)0); // Thanks, Jonathan Leffler 

Note that since zero in your example is not a compile-time constant, the expression requires the cast to conform to the standard:

 if (ap>ac) return (char*)(0*ap++); 

As for returning an integer zero, it is considered equal to a NULL pointer when used in the context of a pointer.

+7
source

The ++ operator will increment the value and return the value before it is incremented. If the function was to return a pointer, you can return zero or NULL to indicate a NULL pointer.

+2
source

Source: https://habr.com/ru/post/980406/


All Articles