How to pass arrays to functions in C

Sdílet
Vložit
  • čas přidán 3. 04. 2019
  • Passing arrays to functions is a simple task can get really confusing when the compiler tries to change the code for you or just doesn't behave the way you expect.
    Check out our Discord server: / discord

Komentáře • 30

  • @pointles2221
    @pointles2221 Před 8 měsíci +4

    OMG I FINALLY UNDERSTAND, THIS CONCEPT CAN BE SO HARD AND YOU MADE IT SO MUCH EASIER

  • @kipster-ll6po
    @kipster-ll6po Před rokem +3

    "You, my friend, are a POINTER." Love it!!!

  • @user-vg1qo5gi3l
    @user-vg1qo5gi3l Před 3 měsíci

    Thanks for explaining! Was looking for this for a while

  • @sufiyansadiq9962
    @sufiyansadiq9962 Před 2 lety +3

    this was exactly what i was looking for, thankyou so much for explaining this to me

  • @wahabfiles6260
    @wahabfiles6260 Před 4 lety +5

    Beautifully explained.

  • @pokemaster1359
    @pokemaster1359 Před 3 lety +2

    You are amazing! Thank you for the understanding!

  • @naboulsikhalid7763
    @naboulsikhalid7763 Před 5 měsíci

    you just point(put) your finger on the things that mostly matter. ARRAY POINTER. thank you for your effort

  • @juggles5474
    @juggles5474 Před rokem

    Awesome video, thank you!

  • @mehdirexon
    @mehdirexon Před 2 lety +1

    it was helpful thank you.please upload more video about pointers and hard concepts in c/c++.

  • @MuksEmmaN
    @MuksEmmaN Před 3 lety +1

    I love your videos please release more sir🙏

  • @We.viriya
    @We.viriya Před rokem

    Thx God. You help me! Love 🙏🏻❤

  • @rechardnabo7751
    @rechardnabo7751 Před 3 lety

    from the function the receive array as a pointer, how do u get the size or length of the array since it returns the 8 bytes

    • @CodeVault
      @CodeVault  Před 3 lety +2

      You can't. You have to store it separately.

  • @vectiphy
    @vectiphy Před 2 lety

    How do we create an Array of functions i.e an arrray consisting of functions parameters.
    is this possible?

    • @CodeVault
      @CodeVault  Před 2 lety

      You mean a function pointer array?:
      void fun1() { }
      void fun2() { }
      void fun3() { }
      void (*function_array[3])() = {fun1, fun2, fun3};

  • @numairdubas2688
    @numairdubas2688 Před 3 lety

    What about the argv array passed in the main function? Where does that point?

    • @CodeVault
      @CodeVault  Před 3 lety +1

      That's taken care of by the OS. It is allocated in more or less the same way you'd do it by calling a function yourself. As for where in memory it's stored... it depends, here's an answer from others that already did this research: stackoverflow.com/questions/26416678/where-does-the-os-store-argv-and-argc-when-a-child-process-is-executed

    • @numairdubas2688
      @numairdubas2688 Před 3 lety

      Thanks! Great video btw

  • @hersona6560
    @hersona6560 Před rokem

    thanks

  • @gonfreecs8174
    @gonfreecs8174 Před 3 lety

    and if i don't want to update the array values what should i do??

    • @CodeVault
      @CodeVault  Před 3 lety +1

      You could pass a copy then

    • @gonfreecs8174
      @gonfreecs8174 Před 3 lety

      @@CodeVault i pass the array, and the inside the function i copy it to a new array. But i mean thats the unique solution?

    • @CodeVault
      @CodeVault  Před 3 lety +3

      Yea, because if you don't want to modify you have to make a copy of it. If you pass an array to a function:
      int testFunction(int arr[]) { ... }
      That arr identifier would be treated as a pointer. So it won't ever automatically make a copy of your array on the stack.
      MAYBE, if you want it to be automatically copied you could have a struct that stores the array and pass that struct directly to that function:
      typedef structArray { int arr[50]; } structArray;
      int testFunction(structArray arr) { ... }
      That should copy the array automatically I think.

    • @gonfreecs8174
      @gonfreecs8174 Před 3 lety

      @@CodeVault that could be a good solution. i will try, thanks

  • @vytautaskuklys3829
    @vytautaskuklys3829 Před 3 lety

    How to change values of arrays in a function when they are passed like this - int** arr?

    • @CodeVault
      @CodeVault  Před 3 lety +2

      If it's a single dimensions array, you should pass it as int* and change it like any other array, if it's a multi-dimensional array then it's more complicated. There's this video on the topic: code-vault.net/lesson/a4mqix89a0:1610303947019

  • @loonathefoxgirl6375
    @loonathefoxgirl6375 Před rokem

    Pointers in disguise
    arr[n] is actually just a way to write *(arr +n). Thats why you can be cursed and do stuff like 3[arr] and it still works. Its just pointer math

  • @cristiandragusin3814
    @cristiandragusin3814 Před 2 lety

    I have an exam tomorrow, so best spent 5 minutes!

  • @QuimChaos
    @QuimChaos Před rokem

    what about multidimensional arrays? 8)

    • @CodeVault
      @CodeVault  Před rokem +1

      There is this video on the topic:
      code-vault.net/lesson/zgpd1zov1t:1603733527939