#include <stdio.h>

int
main()
{
  int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  int i = 3;
  printf("%d\n", i[arr]);
  return 0;
}

If you index an integer with an array in C, it gives you the same result as when you index an array with an integer. This is because array indexing is equivalent to pointer arithmetic. i’th member of arr equals to arr plus i equals to i plus arr equals to arr’th member of i.

  • grue@lemmy.world
    link
    fedilink
    English
    arrow-up
    13
    ·
    6 days ago

    How is that not a bug in the compiler’s type checking? If the code had been i[0] instead, it would have (correctly) given a subscripted value is neither array nor pointer nor vector error. IMO it should have still done that no matter if the thing inside the subscript was an array, because the type that matters to decide if the subscript operator is even valid to use in the first place is the type of the token to the left of it, not the token inside it!

    • Sivecano@lemmy.dbzer0.com
      link
      fedilink
      English
      arrow-up
      7
      ·
      6 days ago

      Subscript operator (in C) is just syntac sugar. i[0] fails because you’re dereferencing an integer not because the operator isn’t defined. (also, arrays are just pointers)

  • riwo@lemmy.blahaj.zone
    link
    fedilink
    English
    arrow-up
    6
    ·
    6 days ago

    hewwo!

    u have terminal in nvim? how u open terminal ?w? i always have hard time :< so i open new terminal window instead

    • VegOwOtenks@lemmy.world
      link
      fedilink
      English
      arrow-up
      8
      ·
      6 days ago

      You can use :ter to open a new terminal buffer in nvim. The window/space management shortcuts for terminal buffers are different, so you may have to look them up.

    • rtxn@lemmy.world
      link
      fedilink
      English
      arrow-up
      6
      ·
      6 days ago

      :ter opens a terminal buffer. It uses the same modal controls as other vim buffers, so you’ll have to enter insert mode to type. Neovim + Nvchad has its own nvchad.term module that can be customized in the config. You can also press Ctrl+Z while in normal mode to suspend Vim and return to the parent shell, and the fg command to reopen the suspended process.