0

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]

Ziezi
  • 6,375
  • 3
  • 39
  • 49
Hairi
  • 3,318
  • 2
  • 29
  • 68
  • 4
    `p_int` is the name of an array and cannot be modified. If you did `int ** p_int;` and dynamically allocated the array, you could do `p_int++;`. And `p_int[++i]` isn't at all equivalent to `p_int++`. `p_int[++i]` modifies `i` but leaves `p_int` alone. `p_int++` attempts to modify `p_int` itself. – lurker Dec 19 '15 at 17:50

3 Answers3

3

In your code, p_int++ and p_int[++i] are not the same. The first one attempt to modify the p_int itself, while the second one does not.

Array names are not modifiable lvalues and cannot be used as the operand for ++.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Array-Subscripts The array subscript expression A[i] is defined as being identical to the expression (*((A)+(i))). This means that many uses of an array name are equivalent to a pointer expression. I – Andre Apr 04 '16 at 15:15
2

Arrays can't be modified, but its elements can. p_int is an array and it can't be an operand of increment/decrement operator or the left operand of = operator.
Arrays are non-modifiable lvalue.

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

No. Arrays are not pointers. If p_int were a pointer in that case too p_int++ would not be equivalent to p_int[++i].

Declare a pointer that points to array p_int and then you can modify it

int * p_int[5];
int **ptr = p_int;  
ptr++;  //This will work
haccks
  • 104,019
  • 25
  • 176
  • 264
1

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.....
Andre
  • 172
  • 2
  • 8