Iterating over an array using pointers

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ม.ค. 2025

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

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

    simple and neat explanation! Thank you!

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

    Awesome channel great work so far.

  • @lemmiix
    @lemmiix 8 หลายเดือนก่อน

    Great explanation!

  • @naboulsikhalid7763
    @naboulsikhalid7763 2 หลายเดือนก่อน

    thank you for making it simple to graspe.

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

    very neat explanation! Thank you!

  • @DanielGlabicki
    @DanielGlabicki 2 ปีที่แล้ว

    great explanation, mate. thanks

  • @mattwhite2011
    @mattwhite2011 11 หลายเดือนก่อน

    Love the vids brother

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

    Hi @CodeVault , can you please explain is that right to assign value to array of pointer ? Int*p[2]={10,20}; And if its right can you please explain how it work please???

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

      Well... not really. What you have there is an array of 2 pointers that point to the address 10 and 20 respectively. Those addresses are usually system reserved and will cause a segmentation fault if you ever want to access them.
      Although, you could do something like this:
      int x = 13;
      int y = 20;
      int* p[2] = {&x, &y};
      And p would have a pointer to both x and y

  • @NavyEagle1
    @NavyEagle1 2 ปีที่แล้ว

    So since an array decays to a pointer if i have for example this code am I saying to the compiler move 4 bytes using x?
    int arr[ ] = {0, 1};
    int x;
    //arr [ 0 ] == *(arr + 0)
    for(x=0;x

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

      Yes that's true in this case. For multi-dimensional arrays the compiler does a few extra calculations

    • @NavyEagle1
      @NavyEagle1 2 ปีที่แล้ว

      @@CodeVault Thank you a lot for your answer ❤

  • @Ok-Chance
    @Ok-Chance 3 ปีที่แล้ว

    Hi , thanks for sharing this , what if i want to get the index , how can i do it ?

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

      If iterating using pointers... You can actually get it using subtraction:
      int i = p - &arr[0];

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

    Thanks

  • @Stat1onary
    @Stat1onary 4 ปีที่แล้ว

    what if my array isnt integers? p = &arr[0] gives me Error (active) E0513 a value of type "char *" cannot be assigned to an entity of type "int *"

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

      You'll have to also change the type of the pointer to the type of the array

    • @Stat1onary
      @Stat1onary 4 ปีที่แล้ว

      @@CodeVault oh right cool thanks for the reply!

  • @Rezyia
    @Rezyia 3 ปีที่แล้ว

    Just to be sure, "&arr[0]" is equivalent to "&arr" right ?

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

      Given an array:
      int arr[16];
      &arr[0] is calculated as: address_of_array + 0 * (sizeof(arr)) = address_of_array + 0 * 64 = address_of_array
      &arr is calculated as: address_of_array
      BUT the type differs. This and the next video explains this concept in-depth: code-vault.net/course/ar67avx6hk:1610029043923/lesson/4rx6a7yjaz:1603820088963

    • @Rezyia
      @Rezyia 3 ปีที่แล้ว

      @@CodeVault Okay thanks :D

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

      Btw, add me on osu: osu.ppy.sh/users/2086138

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

    10 line i had an error warning because format-specifier %d accepts integers whereas what you are supplying in your code is a pointer to a memory location. The correct way is to use specifier %p, which will print the address of the memory location in hexadecimal format.(printf("%p
    %p",(void*)&arr[0],(void*)&arr[4]);) ,but i dont know how you didnt get any error maybe you are a hacker

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

    I eh like you!