Karish Karish
Karish Karish

Reputation: 269

assignment operator and conditional statements

I have hard time understanding the following code buffer[i] = arr ? arr[i] : 0;. Does this mean that if arr contain any thing then its equal to buffer[i] and if it doesn't it equals to 0?

#include <iostream>
using namespace std;
int main ()
{
    int arr[5]={11,22,33,44,55};
    int * buffer;
    buffer = new int [5];

    for(int i=0;i<5;i++){
        buffer[i] = arr ? arr[i] : 0;//true/falls
        cout<<buffer[i]<<",";
    }
    cout<<endl;

    int arr2[5]={};
    int * buffer2;
    buffer2 = new int [5];

    for(int i=0;i<5;i++){
        buffer2[i] = arr2 ? arr2[i] : 0;//true/falls
        cout<<buffer2[i]<<",";
    }
    cout<<endl;
}

Upvotes: 0

Views: 104

Answers (1)

James
James

Reputation: 9278

It seems the code may have been ported from when arr was allocated dynamically. Now it's on the stack so arr can never be NULL and so the check is useless

Upvotes: 9

Related Questions