Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API

From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
lauw278621h@Lensflare What do you mean?I still don't get it why in the real compiler DEV C,I'm not able to get the output after waiting for so long?Yet when using online compiler it work?
-
Lensflare1972721h@lauw278 will you come back to read the answers or will you just abandon the rant as you did with the other two?
-
Lensflare1972721h@lauw278 scanf is there to read the input from the command line.
You are supposed to enter a number for the size of the array, confirm with enter, then enter the numbers for the contents of the array and then enter the index to access the element.
Then you should see to output. -
AlgoRythm5003219hHow does this code compile? You’re defining an array by the size of a non-constant variable. Last I knew, that was a no-no in c, you’d need to malloc it
-
Lensflare1972719h@AlgoRythm lol I noticed it too but then I vaguely remembered reading about it like 10 years ago and dynamically sized stack arrays would be a new feature soon.
So I assumed it was that. 😄 -
lorentz1532019h@AlgoRythm I recall seeing that in working code and being really confused, I thought it was a non-standard extension in some compilers.
-
AlgoRythm5003218h@Lensflare @lorentz It's a variable-length-array (VLA) and it's mandatory in the c99 spec and optional from that point on.
GCC enables it by default, Windows compilers NEVER allowed it at all.
So I guess we know OP is using gcc, which is typical for college courses (free).
It's also just strange to me as someone that's familiar with c. I've never seen it really used much. Everyone is just ok with malloc because it's such a common operation. -
AlgoRythm5003218h@Lensflare @lorentz
More info (I will read this later today): https://stackoverflow.com/questions... -
Lensflare1972718h@AlgoRythm but iirc, malloc gives you a an array on the heap rather than the stack.
-
AlgoRythm5003218h@Lensflare Correct. I'm hardly sure it matters except for in very specific cases, which is what VLA is trying to solve.
Small?: Allocate a fixed size array on the stack.
Large?: Allocate dynamic memory from the heap.
Unknown?: You can try a VLA. -
Lensflare1972718h@AlgoRythm lemme just int small[sizeof(int)];
Edit: Ah shit, I wanted int.max but that‘s not how c++ works…
Lets see… ((unsigned int)-1)
That should do it -
AlgoRythm5003218h@Lensflare Lol, a good reason why c is one of the easiest languages to obfuscate. It just lets you do shit.
c++ is harder to obfuscate for one simple reason: you need to understand it to obfuscate it.
#include <stdio.h>
void getElement(int arr[], int size, int index) {
if(index >= 0 && index < size) {
printf("%d", arr[index]);
} else {
printf("Index out of bounds");
}
}
int main() {
int size, index;
// Read the size of the array
scanf("%d", &size);
int arr[size];
// Read array elements
for(int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Read the index to access
scanf("%d", &index);
getElement(arr, size, index);
return 0;
}
question