Pointer and Array Differences Explained In 2 Minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ต.ค. 2024

ความคิดเห็น • 19

  • @TheBuilder
    @TheBuilder  ปีที่แล้ว +24

    happy to see no one noticed the function definition takes an int array but I pass in a char array instead

    • @benherbst3620
      @benherbst3620 8 หลายเดือนก่อน +2

      😂

    • @clymbep3390
      @clymbep3390 5 หลายเดือนก่อน +2

      this language is driving me crazy

    • @James-l5s7k
      @James-l5s7k หลายเดือนก่อน

      @@clymbep3390 Read a book :D
      The C Programming language; 2nd ed.

  • @PhilippeCarphin
    @PhilippeCarphin ปีที่แล้ว +8

    Another interesting fact about arrays vs pointers is that the array symbol's location is the location of the first element of the array. I.E. printf("%p", &arr), printf("%p", &arr[0]) both print the same address.
    Whereas with a pointer, the pointer p lives somewhere and contains the address of somewhere else. So p[i] finds where p is, gets the address that is contained there, add i times the size of the type, and gives you the thing at that final location. Doing arr[i] finds where arr is, add i times the size of the type and gives you what is there (one less indirection).
    One thing you can do is declare a gobal array `int glb_arr[3] = {1,2,3};` in one file, and in a different file, do `extern int *glb_arr` and in that file try to print `glb_arr[1]` with the most likely result being a segfault.

    • @PhilippeCarphin
      @PhilippeCarphin ปีที่แล้ว

      Before you go for the segfault, you can do
      array.c:
      int glb_arr[2] = {0xdeadbeef, 0xbaadc0de};
      main.c:
      #include
      extern int *glb_arr;
      int main(void) {
      printf("the value of the pointer glb_arr is %p
      ", glb_arr);
      }
      $ gcc main.c array.c -o main && ./main
      The value of the pointer glb_arr is 0xbaadc0dedeadbeef

  • @hansdietrich83
    @hansdietrich83 8 หลายเดือนก่อน +1

    It's relativly simple if you understand that arrays are purely a compile time datastructure. Sizeof(array) is calculated at compile time as well.
    A function that takes an array as an argument could be called multiple times with different arrays, thats why the compiler can't precalculate the size inside the function.

  • @keit99
    @keit99 ปีที่แล้ว +2

    Also pretty sure an array lives in the stack not on the heap (like the bytes the pointer points to that you've malloc'd)

    • @TheBuilder
      @TheBuilder  ปีที่แล้ว +3

      if you use malloc that memory is allocated on the heap which is why you need to free it yourself

  • @duckyyy2682
    @duckyyy2682 ปีที่แล้ว +1

    thanks mate, very helpful!!!

  • @mhmadnwilaty7072
    @mhmadnwilaty7072 10 หลายเดือนก่อน

    You are the man
    Thanks a lot

    • @TheBuilder
      @TheBuilder  10 หลายเดือนก่อน

      Glad it helped

  • @Victor-ji9oc
    @Victor-ji9oc 2 หลายเดือนก่อน

    i dont understand
    help
    -dx12 or -d3d12 command line argument.
    what it mean

  • @georgecop9538
    @georgecop9538 ปีที่แล้ว +2

    it was interesting that because of pointer arithmetic (or other reason) a[3] and 3[a] are the same in C.

  • @sebscripts
    @sebscripts ปีที่แล้ว +1

    not first