[Solved] C programming … Why “return” statement give only one value in this code? [closed]


You’re passing array to the function, which is actually passing a pointer to the first element of the array. This means that changes to the array in the function also will appear in the calling function since it’s the same array.

You don’t need to return anything. Just print the contents of the array in main.

Also, an array of size 4 has indexes from 0 to 3. Right now you’re writing past the end of the array. You need to change your function to prevent that. You can’t just write past the end of the array to make it bigger. The size is fixed.

void append(int array[]){
      int  position, c, s, len=4, value=25; 
      position=1;

      for (c = len - 2; c >= position-1; c--){
          array[c+1] = array[c];
      }
      array[position-1] = value;
}

int main() {
    int kar[]= {5, 8, 555, 99};

    append(kar);

    int c;
    for (c = 0; c < 4; c++){
        printf("%d ", kar[c]);
    }       
    return 0;
}

5

solved C programming … Why “return” statement give only one value in this code? [closed]