Passing an Array to a Function | C Programming Tutorial

Поділитися
Вставка
  • Опубліковано 14 гру 2024

КОМЕНТАРІ • 51

  • @danielcarpio4302
    @danielcarpio4302 2 місяці тому

    This is genuinely the best C language tutorial channel in UA-cam.

  • @SketchupGuru
    @SketchupGuru 2 роки тому +15

    Very useful video btw..
    Whenever I get stuck with the cs50 course, i come running to your videos, gain clarity and then rewatch the cs50 content to cement the idea..
    If i ever make a how to learn code video, im going to include your channel in it

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +2

      Thank you very much, I'm so glad to hear these videos help to clarify what is in the CS50 content. :-)

  • @mikehibbett3301
    @mikehibbett3301 10 місяців тому +4

    I'm lucky enough to have come to C from assembler, so I intuitively know about local variables being on the stack, and parameters passed through registers. I hope modern teachers make this clear. Funny enough, I learned this as a hobbyist as a kid before going to university to learn computer science

  • @Bayanii1772
    @Bayanii1772 Рік тому +12

    this channel is underrated

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      Thank you for the support Bayan! :-)

    • @Bayanii1772
      @Bayanii1772 Рік тому +3

      @@PortfolioCourses Update, I got an A+ on my C programming course. Honestly, your C programming examples playlist helped me a lot with saving time and getting straight to the point, unfortunately lots of free sources tend to lack quality.

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +2

      @@Bayanii1772 That's awesome, congratulations!!!! 😀🥳 I'm so glad to hear the playlist was able to help you out, thank you for taking the time to share this with me!

    • @lucasgroves137
      @lucasgroves137 Рік тому

      🎯

    • @lucasgroves137
      @lucasgroves137 Рік тому

      @@Bayanii1772 Lol, that's generous. In this age of garbage, nothing is more common than describing products/services/content/anything at all as _excellent_ when they are consistently mediocre or worse. It's mind-blowing.

  • @naboulsikhalid7763
    @naboulsikhalid7763 Рік тому +1

    Huge appreciation, I feel the effort to explain the topic, because you know it's confusing and it needs more attention and extra explanation. Thank you a lot. If I meet in this life: I will give you a BIG HUG

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      You're welcome Naboulsi, and thank you for the kind feedback! :-)

  • @Brad_Script
    @Brad_Script 5 місяців тому

    another way to prove it is with the sizeof operator, if you use sizeof on an actual array it will return the number of bytes of the array, if you use sizeof on an array that is a function parameter, it will return 4 or 8 (size of a pointer)

  • @hazemba2669
    @hazemba2669 2 роки тому +2

    Thank you for this amazing explanation! 🤩

  • @john.darksoul
    @john.darksoul 10 місяців тому

    If you have to pass an array to a function, it would be best (if you're not using a struct wrapper) to use
    foo(size_t size, T array[static size]);
    to communicate your intentions to whoever is reading your code, as well as to provide the compiler additional info.
    For passing a pointer to a single value, use
    foo(T ptr[static 1]);
    which will allow the compiler to issue warnings if NULL is passed to this function.
    Iirc, 'T ptr[static N]' means that ptr points to at least N valid objects of type T.

    • @xCwieCHRISx
      @xCwieCHRISx 9 місяців тому

      If u pass a array as an argument or return an array, it is basically the same as passing/returning a pointer. I personally never use array as arguments or return value, I just use pointer. I never saw someone use "T array[size]" as an argument, but "T *array" and "size_t size" as additional argument is pretty common.
      Just use arrays for allocating an array on that stack. For the rest treat array as a pointer, except for indexing maybe (arr[i] is more simple than *(arr+i) ). Passing arrays around is just confusing in my opinion, when it basically behaves like a pointer.

  • @yunustan7502
    @yunustan7502 Рік тому +1

    Nice and clear explanation. Thank you.

  • @a.v7998
    @a.v7998 Рік тому

    Brillian tutorials!

  • @LeeHongYee99
    @LeeHongYee99 10 місяців тому

    So effectively, array as argument in function is really passed by reference.

  • @flywittzbeats4008
    @flywittzbeats4008 Рік тому

    Incredible stuff. Thank you so so much!!!

  • @ddlc7022
    @ddlc7022 7 місяців тому

    What is the editor you are using ? I can see the name is Code ? But when I look for Code for Mac, I only get Visual Studio Code results

    • @ddlc7022
      @ddlc7022 7 місяців тому

      Is it Code::Blocks ?

  • @SketchupGuru
    @SketchupGuru 2 роки тому

    I'm assuming it's going to be the same for strings. We pass memory addresses aka pointers in string as well?

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      Yes that's correct, what's really being passed is a pointer (i.e memory address). :-)

  • @hyperphenomenal4360
    @hyperphenomenal4360 Рік тому

    extremely useful thank you so much

  • @Acksakal
    @Acksakal Рік тому

    Good day, Mr. Teacher.
    I got 2 questions: in your video example with arrays how can I a star pointer "*" work without an ampersand "&"? This is the second time I've seen it.
    First one was this:
    int main()
    {
    char str[] = "All the world's a stage, and all the men and women merely players";
    int size = strlen(str); //determining string's length
    printf("Old line:
    ");

    //print the first word
    char sep [10]=" ";
    char* words= strtok (str,sep); //why is star required here?
    printf("The first word is: %s
    ", words);
    return 0;
    }
    Why do we need to use "*" after char and how come does it work w/o an ampersand? Hope you'll help =)

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      This video might help to explain the difference: ua-cam.com/video/Qp3WatLL_Hc/v-deo.html. And this video on pointers too, char * is a pointer type variable: ua-cam.com/video/2GDiXG5RfNE/v-deo.html. :-)

  • @heshamgad-yh9vs
    @heshamgad-yh9vs Рік тому

    Dude, you are awesome

  • @SketchupGuru
    @SketchupGuru 2 роки тому

    What if you return x; in the add function?
    Wouldn't that be carried to the main function

    • @PortfolioCourses
      @PortfolioCourses  2 роки тому +1

      Yes, we could return a value to the main function with a return statement, and then perhaps assign the value to the variable a. :-)

  • @mikehibbett3301
    @mikehibbett3301 10 місяців тому

    And of course that pointer is to a variable on the stack, so it wont be around for long :)

  • @mahmoudelhanafy89
    @mahmoudelhanafy89 Рік тому

    what if i want to make operations at all array elements. should i use for in the function ???

  • @franciscojusto4744
    @franciscojusto4744 Рік тому

    so i have a question. when i print out the array by itself by using the %p specifier i get a memory address being pointed by the array. and when i use the %p in conjuction with &array i get the memory address of where the array variable is stored. then i use the %p for array[0] and i get yet another address. i thought the address pointed by array would be the first element of the array but it is not? this raises the question of why is there an intermediate address between the value stored in array (address) and the first element of array (address). sorry for the long question! Love your videos

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      Can you show me the code that you're using to print out these values so I can be sure what is going on? You can just paste it in a comment/reply. :-)

    • @franciscojusto4744
      @franciscojusto4744 Рік тому

      @@PortfolioCourses
      int array[] = {5};
      printf("address of array: 0x%p
      ", &array);
      printf("address pointed by array: 0x%p
      ", array);
      printf("array[0] address: 0x%p
      ", &array[0]);
      so i must have made a mistake somewhere before because now its working as i expected it to work! sorry for the confusion!! 😅 thank you for the quick reply!

    • @PortfolioCourses
      @PortfolioCourses  Рік тому +1

      No worries, glad you got it sorted out! :-)

    • @franciscojusto4744
      @franciscojusto4744 Рік тому

      @@PortfolioCourses I just wanted to say thank you for all your effort towards your long list of comprehensive videos on C!
      I have been whatching them for a while now and i find it more useful to me to come here everytime i need a refresher or i nneed to understand something, rather than going on a wild search for answers.
      This last weekend when i posted my question i was studying various topics for an interview that took place monday.
      I based my study mostly on your videos because YES, they are that good! :)
      I'm happy to say that you helped me to better prepare myself for the interview and that i got a job position in said company for C in aeronautics.
      Again, I think mostly anyone who comes across your channel and your tutorials on C agrees with me when i say they are very well made and they are a great source of information and learning.
      So thank you dearly for all your work!

    • @PortfolioCourses
      @PortfolioCourses  Рік тому

      @@franciscojusto4744 WAHOO congratulations Francisco, I'm so happy to hear that!!! 😀

  • @NikitaSafronov-y6i
    @NikitaSafronov-y6i 2 роки тому

    Excellent =)

  • @emtnan1733
    @emtnan1733 2 роки тому

    Perfect 👍🏻

  • @raziamim9510
    @raziamim9510 4 місяці тому

    That was soooo hard to understand.