Search code examples
carrayspointerspostfix-operator

Pointer arithmetic isn't clear to me


I have code where I want to increment a pointer. But the compiler don't like my expression. Here is the code:

int * p_int[5];
p_int++;

Compiler gives an error:

lvalue required as increment operand p_int++;

I thought that p_int++; would be equivalent to p_int[++i]


Solution

  • you should use an array pointer

    int arr_int[5] = {4,5,6,7,8};
    int (*p_int)[5];
    p_int = &arr_int;
    p_int++; // p_int++; jumps forward 5 places in memory. (5 x sizeof(int))
    // to iterate over the array elements do for(int i =0;i<5;i++){ (*p_int)[i];}
    

    You looking probably for this:

             int ar[5] = {2,3,4,6,7}; // an int array
             int * p; // a int pointer
             p = ar;  // attach the array to pointer, an array is always an array of pointers so p = &ar;  gives a error.
             for(i=0;i<5;i++){printf("%d\n",*p++);} // dereference the *p and add 1 to the address the p holds as value.
    

    .

     #include <stdlib.h>
     #include <stdio.h>
    
     int main(){
            int i = 0;
    
             char arr[3][10] = { // double dimensional array.
                              { "Hello"},
                              { "Welcome"},
                              { "Good bye."},
                              };
    
    
              char (*ptr)[10];   // array pointer.
              ptr = arr;    // assign array to pointer.
              for( ; i < 3 ; i++ ){
                                // print memory address, and array value.
                                  printf("%p : %s \n", (*ptr), (*ptr));
                                  // jump to next array = current memory address + 9.
                                  ptr++;
                        }
              printf(" ======================= \n");
              char second_arr[8] = { 'W','e','l','c','o','m','e'};
              char (*second_ptr)[8];   // array pointer.
              second_ptr = &second_arr;    // assign array to pointer.
    
              printf("memory address: %p   txt: %s \n", (*second_ptr), (*second_ptr));
    
              printf(" ======================= \n");
    
              for(i = 0 ; i < 7 ; i++ ){
                                // print memory address, and array value.
                                   printf("%p : %c \n", (*second_ptr), (*second_ptr)[i]);
                                   // jump to next array = current memory address + 9.
                        }
              return 0;
    };
    

    second example. // * Typedef a array pointer * //

      int i = 0, ERROR = 1, CRASH = 5, GOOD = 6, BUG = 8;
      char succes_text[3][60] = {
                                  {"Awesome performance detected !!\n"},
                                  {"Your system and program are performing a expected.\n"},
                                   {"No problems detected, proceeding next task.\n"}
                                                };
        char error_text[3][60] = {
                                    { "Undefined error detected, call the help-desk.\n"},
                                    { "Warning, bad algorithmic behavior.\n"},
                                    { "Program manager found a bug, save your work.\n"}
                                                };
    
        typedef char (*SUCCES_TEXT_TYPE)[60];
        SUCCES_TEXT_TYPE SUCCES_TEXT = succes_text;
    
        typedef char (*ERROR_TEXT_TYPE)[60];
        ERROR_TEXT_TYPE ERROR_TEXT = error_text;
    
        char * testfunc(int i, SUCCES_TEXT_TYPE s_txt, ERROR_TEXT_TYPE e_txt){
                                        if(i == ERROR){ return (*e_txt);}
                                        if(i == CRASH){ e_txt += 1; return (*e_txt);}
                                        if(i == BUG){   e_txt += 2; return (*e_txt);}
                                        if(i == GOOD){ return (*s_txt);}
                                        return "";
                                        }
    
         int main(){
    
               for(;i < 10; i++){
                                  printf("%s",testfunc(i, SUCCES_TEXT, ERROR_TEXT));
                      }
              return 0;
      };
        // END SECOND EXAMPLE.....