Page 178 - C-Language
P. 178

#include <stdio.h>

         void modify(int* v) {
             printf("modify 1: %d\n", *v); /* 0 is printed */
             *v = 42;
             printf("modify 2: %d\n", *v); /* 42 is printed */
         }

         int main(void) {
             int v = 0;
             printf("main 1: %d\n", v); /* 0 is printed */
             modify(&v);
             printf("main 2: %d\n", v); /* 42 is printed */
             return 0;
         }


        However returning the address of a local variable to the callee results in undefined behaviour. See
        Dereferencing a pointer to variable beyond its lifetime.

        Order of function parameter execution


        The order of execution of parameters is undefined in C programming. Here it may execute from
        left to right or from right to left. The order depends on the implementation.


         #include <stdio.h>

         void function(int a, int b)
         {
             printf("%d %d\n", a, b);
         }

         int main(void)
         {
             int a = 1;
             function(a++, ++a);
             return 0;
         }



        Example of function returning struct containing values with error codes


        Most examples of a function returning a value involve providing a pointer as one of the arguments
        to allow the function to modify the value pointed to, similar to the following. The actual return value
        of the function is usually some type such as an int to indicate the status of the result, whether it
        worked or not.


         int func (int *pIvalue)
         {
             int iRetStatus = 0;             /* Default status is no change */

             if (*pIvalue > 10) {
                 *pIvalue = *pIvalue * 45;   /* Modify the value pointed to */
                 iRetStatus = 1;             /* indicate value was changed */
             }

             return iRetStatus;              /* Return an error code */



        https://riptutorial.com/                                                                             154
   173   174   175   176   177   178   179   180   181   182   183