Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Array index related question (in C++)

Status
Not open for further replies.

mahaju

Full Member level 2
Joined
Mar 17, 2007
Messages
125
Helped
7
Reputation
14
Reaction score
2
Trophy points
1,298
Activity points
2,252
Hi
is this code legal?

Code:
.
.
.
    uint e;
    int i, j;
    e = (unsigned int) ceil( (NO_OF_BITS + 1)/NO_OF_BITS_IN_WORD ) ;

    uint M[e*NO_OF_BITS_IN_WORD],
		Y[e*NO_OF_BITS_IN_WORD], temp_Y_to_store_group[NO_OF_BITS_IN_WORD],
		S[e*NO_OF_BITS_IN_WORD], X[NO_OF_BITS], C[2]={0,0};
.
.
.

let me explain why I am asking this

I am sure something like this used to be illegal when I was beginning to study C++

Code:
int main(){
    int i,j;
    cout<<"Number of elements: "; cin>>i;
    int arr[i];
    for(j=0;j<i;j++){
        cout<<"element no "<<j<<" >> "; cin>>arr[j];
    }
    for(j=0;j<i;j++)
        cout<<"You entered"<<endl<<"a["<<j<<"] = "<<arr[j]<<endl;
    return 0;
}

We used Turbo C++, and the reason for it being illegal is that array index should be a constant positive integer and in this code the variable i get's it's after only at runtime. One of the reasons why we had to study calloc/malloc and new/delete. But now when I run it in gcc it seems to be completely legal and compiles easily. What is up with that? Have I already become that old? I would guess it is due to some sort of features added into the compiler but I don't know for sure?

However, this is my main question. In my first code, NO_OF_BITS and NO_OF_BITS_IN_WORD are constants defined using #define. So during each run of the program the value of e should be fixed. However, the variable e gets it's value from a function. Does this make it same as getting it from a cin? I need to use e to define array size later on, so is this legal? gcc seems to compile it anyway so I just need to make sure.

by the way, uint is typedef unsigned int
 

However, the variable e gets it's value from a function. Does this make it same as getting it from a cin?
No. ceil() is apparently available for constant (compile time) calculations with your compiler. But I'm not sure, if it's an assured C++ feature, it may be implementation dependant.
 

Apparently this is a feature built into the latest C++ standard (since C99 I think but I'm not sure)
It compiles, but it seems it does not allow initialization of the array variable

---------- Post added at 13:41 ---------- Previous post was at 13:41 ----------

first one is correct. You may also use below code.

int* array_int, nTotalCount = 10, sbig, big, i;
printf ( "\n/********************** Second Biggest
Number ********************/" );
array_int = ( int* ) malloc ( nTotalCount * sizeof
( int ) );

for ( i = 0; i < nTotalCount; i++ )
array_int = rand() % 100;



**broken link removed**


By the way, that is C and not C++
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top