2
lauw278
22h

#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;
}

Comments
  • 3
  • 0
    Why there is no output as shown?
  • 0
    @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?
  • 2
    @lauw278 will you come back to read the answers or will you just abandon the rant as you did with the other two?
  • 3
    @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.
  • 3
    No sweet summer children. There is no \n. So nothing gets displayed.
  • 1
    How 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
  • 0
    @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. 😄
  • 0
    @AlgoRythm I recall seeing that in working code and being really confused, I thought it was a non-standard extension in some compilers.
  • 2
    @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.
  • 0
    @Lensflare @lorentz

    More info (I will read this later today): https://stackoverflow.com/questions...
  • 0
    @AlgoRythm but iirc, malloc gives you a an array on the heap rather than the stack.
  • 1
    @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.
  • 0
    @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
  • 1
    @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.
  • 1
    @AlgoRythm I used to do golf in C/C++.
    It was a hell of a fun! 😄
Add Comment