Pass By Reference | C Programming Tutorial

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

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

  • @crklopotic
    @crklopotic 9 месяцев назад +6

    Pointers can be hard to learn and understand why or when to use them. This is a nice short video that clearly articulates all the basics in a very clear and easy to follow along example. Thanks for all the videos! Very well done.

  • @pietraderdetective8953
    @pietraderdetective8953 Год назад +10

    i absolutely love this C tutorial series.
    your channel is amazing! i need to pick up the pace, i still got 154 videos left to grind..

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

      I’m really glad that you’re enjoying the content. :-)

  • @philipphortnagl2486
    @philipphortnagl2486 Год назад +16

    This concept is hard to get for beginners, thanks a lot!

  • @SuperDamuho
    @SuperDamuho 8 месяцев назад +3

    I get it now. Pass by reference (pointer) is needed because the variables in functions are exclusive. It's like there's a wall between the function area and the main area. The only way we can connect those variables (in main and in function) is to manipulate the memory via addresses. Kinda like digging a hole underground.😄

    • @vvvawdad1767
      @vvvawdad1767 12 дней назад

      See thats the way i want to look at it but, functions can still access variables from the main file without pointers tho??
      Like for example:
      int calculate(int x){
      int y = x * 2;
      return y;
      Int main(){
      int x = 5;
      int z = calculate(x);
      printf(“%d”, z);
      }
      Like the calculate function still has access to the variable x even without using pointers. So I dont get it.

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

    Watched so many videos on this. First time I actually understood what it is. Good video.

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

    Passed by pointer. Got it!!!
    Great video, thanks!

  • @christopheanfry2425
    @christopheanfry2425 7 месяцев назад

    This is gold!!! Thank you for the clear explanation

  • @kierkegaard54
    @kierkegaard54 11 месяцев назад +1

    Clear as a whistle

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

    The Best channel so far

  • @muhisaleh4598
    @muhisaleh4598 2 года назад +3

    pass by pointer 💪thnx a lot, this channelis realy one of the best c channel ever :)

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

    Really well explained, especially giving a reason WHY passing by reference is beneficial. But I am getting confused about *a vs *a. Some times you referred to "*a" like: "a is a pointer" and other times you were saying: "Here we dereference the pointer a". Sounds like "*a" can be two totally different things?

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

      *a = *b
      *a is an address in memory.
      *b is the value stored at the address that b points to.
      So the same notation can mean two different things?
      Basically I'm totally confused.

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

      ⁠​⁠@@plica06it’s confusing. Basically the short hand is when you initialize or declare a pointer * is used, eg:
      Int *a, char *b, float *c
      Afterwards when you use these pointers * is used to dereference and refer to that actual value at the pointer address. Otherwise, without it you are assigning just the memory address of variables. So in this case:
      *a=*b the value at a is assigned the value at b
      a = &b pointer a now points to address of variable b

    • @user-zqrj9tt13
      @user-zqrj9tt13 Месяц назад +1

      & = address of...
      * = access to the value of that address.
      Using C's good practices, pointers are declared with *pYourVariable and the p stands for 'pointer'. Here is a hard-coded swap that can help you understand:
      ...hard-coded swap Function...
      (1) int swap(int *pX, *pY)
      (2) *pX = 10;
      (3) *pY = 5
      ...main...
      (1) int x = 5, y = 10;
      (2) printf(..x..y) //-> x = 5, y = 10
      (3) swap(&x, &y); let' say &x = 0x005 and &y = 0x010 are the addresses.
      (4) printf(..x..y) //-> x = 10, y = 5
      Starting from main:
      (1) declare 'x' equals to 5 and 'y' equals to 10
      (2) print the values of 'x' and y, that is 'x' = 5 and 'y' = 10 as in (1)
      (3) you pass as parameters the addresses of what is stored in 'x' and 'y' (with the &s).
      (3.1) swap receives the value of those addresses and then it creates the pointers. the value stored in pX is 0x005 and the value stored in pY is 0x010. with the dereference operator, you are accessing to the value of those addresses so *pX is 5 and *pY is 10.
      (3.2) and (3.3) a hard coded swap, the value of what is stored in the memory address contained in pX now is 10, same for pY.
      (4) print again the values of 'x' and y, but now 'x' = 10 and 'y' = 5

    • @user-zqrj9tt13
      @user-zqrj9tt13 Месяц назад +1

      Then, the swap function showed here should look like this:
      void swap (int *pX, int *pY) //receive the addresses and get the values inside
      {
      int temp;
      temp = *pX;
      *pX = *pY;
      *pY = temp;
      }
      int main()
      {
      int x = 5, y = 10;
      printf("'x' is %d and 'y' is %d.", x, y); //-> 'x' is 5 and 'y' is 10
      swap(&x, &y); //pass as parameters the addresses where those values are stored
      printf("'x' is %d and 'y' is %d.", x, y); //-> 'x' is 10 and 'y' is 5
      return 0;
      }

  • @Andy_B.
    @Andy_B. 8 месяцев назад

    still didnt understand why call by value didn't work...
    😆
    but I understood the call by reference mechanism,
    thanks!!

    • @guusziin6241
      @guusziin6241 7 месяцев назад

      because in C apart from global variables all variables only exist inside the scope of a function.
      in the example of the video x and y only exist inside the main function and when you pass their values to the swap function it will actually create a copy of them that only exist inside swap, and when the swap function ends the copy will be deallocated and will return to the main function where the actual x and y remained unchanged.
      so when you actually pass the addresses of x and y as an argument of swap it'll change their values for the whole program, because the actual value stored in the memory address of x and y will be changed other than a copy that only exists in the scope of swap

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

    Can you have (int &a, int &b) as function perameters? Im confused here? As we want an address to deference...?

    • @PortfolioCourses
      @PortfolioCourses  Год назад +3

      We can do that in C++ where reference variables are supported. But in C we can only achieve 'pass by reference' by using pointer parameters. When we have a parameter like int *a, the argument is going to be a pointer (memory address). :-)

  • @gamingwithronpubgm3615
    @gamingwithronpubgm3615 5 месяцев назад

    thx man I immediately understood everything after watching this video😁

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

    what IDE you're using sir?
    and thanks for the video.

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

      You're welcome! :-) In this video I am using Visual Studio Code as a text editor, and I am using the gcc compiler on the MacOS Terminal.

  • @love15.01
    @love15.01 2 года назад +1

    really good, thanks you!:)

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

    Got it ! thank you a lot !

  • @yan.yoeurn
    @yan.yoeurn Год назад

    thank you do much such a great video.

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

    if we think that & is inverse of *, & times * will produce 1. Therefore *&x = x. (i dont know is that interpretation whether correct or not)

  • @bougsqf4462
    @bougsqf4462 2 года назад +1

    cool man!
    thx

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

    why is it called passed by pointer and not passed by reference?

    • @PortfolioCourses
      @PortfolioCourses  Год назад +2

      Great question Bruno! :-) The term pass by reference is more accurate in languages like C++ that have reference variables (and reference parameters): ruclips.net/video/cxysUPZH65Y/видео.html. Reference variables are a different type of variable than a pointer, though the concepts are a bit similar too... references are a reference to the variable, the essentially "are" the variable they are referencing and give us access to that variable. Whereas pointers store a memory address, the memory address of the variable they are "pointing to", and we can access the variable they are "pointing to" by de-referencing the pointer. So in C, because we are using pointers, it's more accurate to say "pass by pointer". As a practical matter though if you say "pass by reference" when referring to "pass by pointer" people will know what you mean and the terms get used in a more casual manner like that in practice.

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

    Really thanks ❤

  • @vvvawdad1767
    @vvvawdad1767 12 дней назад

    Ahhhhh i still dont get it but it kinda helped

  • @alexmacomposer8131
    @alexmacomposer8131 5 месяцев назад

    Godsent