Search code examples
c++arrayspointersimplicit-conversionfunction-declaration

Can we add an integer to an array in c++


#include <bits/stdc++.h> 
using namespace std; 

/*Prototype for utility functions */
void printArray(int arr[], int size); 
void swap(int arr[], int fi, int si, int d); 

void leftRotate(int arr[], int d, int n) 
{ 
    /* Return If number of elements to be rotated 
    is zero or equal to array size */
    if(d == 0 || d == n) 
        return; 
        
    /*If number of elements to be rotated 
    is exactly half of array size */
    if(n - d == d) 
    { 
        swap(arr, 0, n - d, d); 
        return; 
    } 
        
    /* If A is shorter*/        
    if(d < n - d) 
    { 
        swap(arr, 0, n - d, d); 
        leftRotate(arr, d, n - d);   
    } 
    else /* If B is shorter*/       
    { 
        swap(arr, 0, d, n - d); 
        leftRotate(arr + n - d, 2 * d - n, d); /*This is tricky*/
    } 
} 

/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size) 
{ 
    int i; 
    for(i = 0; i < size; i++) 
        cout << arr[i] << " "; 
    cout << endl; 
} 

/*This function swaps d elements starting at index fi 
with d elements starting at index si */
void swap(int arr[], int fi, int si, int d) 
{ 
    int i, temp; 
    for(i = 0; i < d; i++) 
    { 
        temp = arr[fi + i]; 
        arr[fi + i] = arr[si + i]; 
        arr[si + i] = temp; 
    } 
} 

// Driver Code 
int main() 
{ 
    int arr[] = {1, 2, 3, 4, 5, 6, 7}; 
    leftRotate(arr, 2, 7); 
    printArray(arr, 7); 
    return 0; 
} 

// This code is contributed by Rath Bhupendra 

I found this code on the geek for geeks website. The code is used to rotate the elements of an array. It is mentioned as block swap algorithm in the website, my questions are:

Can we add integers to an array in c++ as given in the else part of the left rotate function while passing the arguments (arr+n-d)?

How can we add integers to an array?

I tried adding an integer to an array in an online compiler and it didn't work. But the above code works perfectly giving the desired output 34567.

The link to the website is https://www.geeksforgeeks.org/block-swap-algorithm-for-array-rotation/.


Solution

  • Can we add integers to an array in c++ as given in the else part of the left rotate function while passing the arguments (arr+n-d)?

    How can we add integers to an array?

    The answer is you can't, and that's not what's happening here.

    int arr[] argument decays to a pointer to the first element of the array. It's the same as having int* arr so what you are doing in arr + n - d is simple pointer arithmetic.

    The pointer will be moved n - d positions relative to the position it's at before the expression is evaluated.

    Supposing the result of n - d is 4, and arr is pointing to the beginning of the array passed as an argument, that is to &arr[0] (in array notation) or arr + 0 (in pointer notation), which is where it's pointing to in its inicial state, you'll have arr + 4 or &arr[4], after the evaluation, the expression provides access to the address of index 4 (the 5th element of the array). To access the value within that address you'd use *(arr + 4) or arr[4].

    On a side note I wouldn't advise the use of geeksforgeeks.com to learn C++, or any other language, for that matter, this should be done by reading a good book.