Passing an Array to a Function | C Programming Tutorial

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии •

  • @danielcarpio4302
    @danielcarpio4302 2 месяца назад

    This is genuinely the best C language tutorial channel in RUclips.

  • @mikehibbett3301
    @mikehibbett3301 9 месяцев назад +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

  • @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. :-)

  • @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)

  • @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.

  • @john.darksoul
    @john.darksoul 9 месяцев назад

    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.

  • @LeeHongYee99
    @LeeHongYee99 10 месяцев назад

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

  • @hazemba2669
    @hazemba2669 2 года назад +2

    Thank you for this amazing explanation! 🤩

  • @a.v7998
    @a.v7998 11 месяцев назад

    Brillian tutorials!

  • @yunustan7502
    @yunustan7502 Год назад +1

    Nice and clear explanation. Thank you.

  • @flywittzbeats4008
    @flywittzbeats4008 Год назад

    Incredible stuff. Thank you so 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: ruclips.net/video/Qp3WatLL_Hc/видео.html. And this video on pointers too, char * is a pointer type variable: ruclips.net/video/2GDiXG5RfNE/видео.html. :-)

  • @mikehibbett3301
    @mikehibbett3301 9 месяцев назад

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

  • @ddlc7022
    @ddlc7022 6 месяцев назад

    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 6 месяцев назад

      Is it Code::Blocks ?

  • @hyperphenomenal4360
    @hyperphenomenal4360 Год назад

    extremely useful thank you so much

  • @heshamgad-yh9vs
    @heshamgad-yh9vs Год назад

    Dude, you are awesome

  • @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!!! 😀

  • @mahmoudelhanafy89
    @mahmoudelhanafy89 Год назад

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

  • @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). :-)

  • @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. :-)

  • @emtnan1733
    @emtnan1733 2 года назад

    Perfect 👍🏻

  • @NikitaSafronov-y6i
    @NikitaSafronov-y6i 2 года назад

    Excellent =)

  • @raziamim9510
    @raziamim9510 3 месяца назад

    That was soooo hard to understand.