Search code examples
carrayscharintsigned

Changing value of an int array with a signed char pointer


I want to create a function to change the value of a void array index to the specified value.

I'm testing with a simple function that changes the value of the certain index using a char copy from the array (char is 1 byte). I use char because I can add one to the copy to point to the next index and alter the value.

The function is:

void
change_value(int ** ptrs, int pos, int val) {
  char * cp = *ptrs;
  /* Assign a copy of the array */
  unsigned long int iter = sizeof(int) / sizeof(char);
  /* Store the size for every index of the array */

  for (int i = 0; i < pos; i++)
    for (unsigned long int e = 0; e < iter; e++)
      cp++;
  /* Advance to the position */

  *cp = (signed char)val;
  /* Assign the value */
}

The problem comes when I try to change the value to a -1. I think the problem is because when assigning char to int the -1 is converted to a 255, so I added to the assignment the conversion to a signed char. But still with no results.

The main function is:

  arr[0] = 1;
  arr[1] = 2;
  arr[2] = 3;
  arr[3] = 4;
  arr[4] = 5;
  /* Create an array for values */

  change_value(&arr, 1, 0);
  /* Change value */
  for (int i = 0; i < 5; i++)
    printf("arr[%d] : %d\n", i, arr[i]);
  /* Show the array */

  printf("\n");

  change_value(&arr, 2, -1);
  /* Change value */
  for (int i = 0; i < 5; i++)
    printf("arr[%d] : %d\n", i, arr[i]);
  /* Show the array */

The output:

arr[0] : 1
arr[1] : 0
arr[2] : 3
arr[3] : 4
arr[4] : 5

arr[0] : 1
arr[1] : 0
arr[2] : 255
arr[3] : 4
arr[4] : 5

Solution

  • The problem is that you're not sign extending the value.

    int x = 0;
    *((signed char *)&x) = (signed char)-1;
    
    printf("%08x\n", -1);       // FFFFFFFF
    printf("%08x\n", 255);      // 000000FF
    printf("%08x\n", x);        // 000000FF
    

    Live demo.

    In other words, setting the value of an int to -1 requires modifying all of its bytes.

    But note that to do so you'd be relying on two implementation-defined behaviours, making this code non-portable: