1
lauw278
6d

#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
  • 5
  • 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?
  • 4
    @lauw278 will you come back to read the answers or will you just abandon the rant as you did with the other two?
  • 4
    @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.
  • 2
    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
  • 1
    @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. 😄
  • 1
    @AlgoRythm I recall seeing that in working code and being really confused, I thought it was a non-standard extension in some compilers.
  • 3
    @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.
  • 2
  • 1
    @AlgoRythm but iirc, malloc gives you a an array on the heap rather than the stack.
  • 2
    @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.
  • 1
    @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
  • 2
    @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.
  • 2
    @AlgoRythm I used to do golf in C/C++.
    It was a hell of a fun! 😄
  • 1
    @Lensflare You're meant to code on land ;P
  • 1
    @BordedDev sometimes the ball landed in water though
  • 2
    Whoever is teaching that course needs to take up an alternative career, maybe as a subsistence farmer or a market porter.
  • 2
    Something about this guy pisses me off.
    @lauw278 At least say something in response for fucks sake.
  • 3
    @Lensflare He can't reply, he's waiting for a blocking scanf call.

    Waiting for so long.
  • 0
    @Lensflare Well I did try to type in the output,but results shows that there is no output
  • 1
    @lauw278

    2 (enter)

    8 (enter)
    9 (enter)

    1 (enter)

    output: 9

    ?
  • 0
    It seems like in my DEV C++ compiler,it doesn't work
  • 1
    @lauw278 Dev-C++ is not a compiler, it’s an IDE that can use any compiler.

    Maybe it’s because of the variable length array that we were talking about in the comments.
  • 0
    Oh I see,thanks
  • 0
    Oh I see,thanks
  • 2
    I've got a feeling this guy is training to be a UX specialist on the team that looks after the azure management portal.
  • 2
    @donkulator god damnit, thats hilarious 🤣
Add Comment