1

I'm trying to change array's variables address by swapping the address, not the value. I have a function that swap the numbers in the array: a[0]=a[9] , a[9]=a[0], a1=a[8] , a[8]=a1, a[2]=a[7] , a[7]=a[2], ec`. If you look at the image, for example in the array arr[0] = 33, address is 00...E8. I want to swap it with the last number to be arr[0] = 10 with address of 00..0C. now I have arr[0] = 10, address 00...E8, i switched the value and not the address.

how can I implement this?

#include <stdio.h>
#include <stdlib.h>
#include<time.h>


void invert(int arr[], int size)
{
    int n;
    int temp;
    for (n = 0; n < size / 2; n++)
    {
        temp = arr[n];
        arr[n] = arr[size - 1 - n];
        arr[size - 1 - n] = temp;
    }
    printf("after invertion");
    printf("\n");
    for (n = 0; n < 10; n++)
    {
        printf(" %d ", arr[n]);
        printf(" %p\n", &arr[n]);
    }

}
void main()
{
    int arr[10];
    int i;

    srand(time(NULL));
    for (i = 0; i < 10; i++)
    {
        arr[i] = rand() % 50;
    }

    printf("before invertion");
    printf("\n");
    for (i = 0; i < 10; i++)
    {
        printf(" %d ", arr[i]);
        printf(" %p\n", &arr[i]);
    }
    printf("\n");
    invert(arr, 10);
}

First image

6
  • 1
    You can't "swap" the address... it's the address of that array element. Commented Mar 7, 2022 at 0:33
  • why not? i can access it by &, no? Commented Mar 7, 2022 at 0:34
  • 2
    That's right, it's the address of that array element. Swapping it with something isn't a valid concept: you have swapped the array element's content... What are you actually trying to achieve? Commented Mar 7, 2022 at 0:36
  • 1
    @user17712660 If you change the numbers on the front of your home, did the building move? The address of a variable is where it is in memory. That's - where it is. You can change the value stored there. Commented Mar 7, 2022 at 0:40
  • @WeatherVane I try to swap the address but know i understand that i can't. well at least the function works. Commented Mar 7, 2022 at 0:48

1 Answer 1

0

You cannot swap the address of array elements. The array elements are all stored in sequence in memory. If an integer takes 4 bytes in memory, for an array of size 10, a total of 40 contiguous bytes are taken in memory. The array a points to the address of the beginning of these 40 bytes and when you index it, the index is multiplied by sizeof(int) = 4. Therefore, the address of a[1] equals the address of a + 4, and the address of a[9] equals the address of a + 9 * 4.

As you have shown in the image, the addresses of the array elements are all contiguous and differ by just 4 bytes. You are able to say for example a = a + 20 which causes a[0] to point to a[5], but then the rest of the array elements are also shifted and will point to after that as explained above. Technically, you can only change where the beginning of the array points to, and you are not able to change where the middle of the array points to.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.