-2

I have written this code, which should move my array by 1:

// Declare and initialize Array
int temp;
int dim[5] = { 6, 7, 8, 9, 10 };


int array_size = sizeof(dim) / sizeof(dim[0]);
cout << "Array-Size: " << dim[array_size - 1] << endl;

// Move by 1 position
for (int i = 0; i < (array_size - 1); i++)
{
    temp = dim[array_size - 1];
    dim[array_size - 1] = dim[i];
    dim[i] = temp;
}

// Console Output
for (int i = 0; i < array_size; i++)
{
    cout << dim[i] << ' ';
}

Everything is working alright, but i want to change it to pointer notation. I've already tried a lot of things but when i change the array to pointer notation i get a weird output. Firstly is it even possible to change the code to pointer notation and secondly if so, how should i do it? I would be grateful for any advice. Thank you!

ideasia
  • 3
  • 6

1 Answers1

2

dim[i] is syntactic sugar for *(dim + i). You can replace all array notation with the equivalent pointer notation.

// Declare and initialize Array
int temp;
int dim[5] = { 6, 7, 8, 9, 10 };


int array_size = sizeof(dim) / sizeof(dim[0]);
cout << "Array-Size: " << *(dim + array_size - 1) << endl;

// Move by 1 position
for (int i = 0; i < (array_size - 1); i++)
{
    temp = *(dim + array_size - 1);
    *(dim + array_size - 1) = *(dim + i);
    *(dim + i) = temp;
}

// Console Output
for (int i = 0; i < array_size; i++)
{
    cout << *(dim + i) << ' ';
}

Using dynamic memory allocation:

// Declare and initialize Array
int temp;
int array_size = 5;
int *dim = new int[array_size]{ 6, 7, 8, 9, 10 };

cout << "Array-Size: " << *(dim + array_size - 1) << endl;

// Move by 1 position
for (int i = 0; i < (array_size - 1); i++)
{
    temp = *(dim + array_size - 1);
    *(dim + array_size - 1) = *(dim + i);
    *(dim + i) = temp;
}

// Console Output
for (int i = 0; i < array_size; i++)
{
    cout << *(dim + i) << ' ';
}
delete[] dim;

Using dynamic memory allocation and pointers in for loop:

// Declare and initialize Array
int temp;
int array_size = 5;
int *dim = new int[array_size]{ 6, 7, 8, 9, 10 };

cout << "Array-Size: " << *(dim + array_size - 1) << endl;

// Move by 1 position
for (int *ptr = dim; ptr < (dim + array_size - 1); ++ptr)
{
    temp = *(dim + array_size - 1);
    *(dim + array_size - 1) = *ptr;
    *ptr = temp;
}

// Console Output
for (int *ptr = dim; ptr < dim + array_size; ++ptr)
{
    cout << *ptr << ' ';
}
delete[] dim;
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • ok but what should i do if i want to change the array itself (to make it a pointer notation)? – ideasia Jun 21 '20 at 10:57
  • @ideasia I don't understand the question. What does it mean to make an array a pointer notation? – Thomas Sablik Jun 21 '20 at 11:00
  • i thought to make it in heap allocation, but i don't think thats needed – ideasia Jun 21 '20 at 11:06
  • @ideasia if you'd tried using dynamic allocation then surely it had been the size which caused an issue. Actually you cannot use sizeof on a pointer to get the length of the array pointed by it. Check these threads out : https://stackoverflow.com/questions/8269048/length-of-array-in-function-argument and https://stackoverflow.com/questions/5126353/get-the-length-of-dynamically-allocated-array-in-c – brc-dd Jun 21 '20 at 11:12
  • Using static memory allocation vs. dynamic memory allocation and using array notation vs. pointer notation are completely unrelated and two different questions. I can change my answer to dynamic memory allocation if it helps. – Thomas Sablik Jun 21 '20 at 11:18
  • To go further, you might then replace `int i` usage by "iterator"/pointer. – Jarod42 Jun 21 '20 at 11:21