Difference between arrays and pointers in C

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

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

  • @Miguel-mv5yc
    @Miguel-mv5yc 3 года назад +24

    How can you have so little views, this content is the best I have found so far!

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

      Best content on youtube about c as far as i know

  • @imperatusmauser7096
    @imperatusmauser7096 3 года назад +5

    Hands down the best programming RUclips channel. I learnt a lot more than the Indian dudes that can't explain what an unsigned integer is.

    • @guitargeorge7255
      @guitargeorge7255 8 месяцев назад +2

      Racist

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

      @@guitargeorge7255 🤣🤣🤣

    • @dragancens
      @dragancens Месяц назад

      This guy talks on English lanquage. Normal one. That is the difference.

  • @noweare1
    @noweare1 2 года назад +13

    I think of an array as a constant pointer, you can not assign a different address to arr, but you can use it to loop through the array i.e. * (arr+3) say to access its members. A pointer is more versatile as you can assign different addresses to it to point to different arrays and you can index through an array by using *(p++). int a[] ={1,2,3}, b[3] and trying to assign b=a I get caught with that sometimes. have to use memcpy to do that : )

  • @sony1979iq
    @sony1979iq 3 года назад +6

    thank you for this series of c programming, very helpful and very easy explained with correct type of examples.. realy thx man

  • @bestieboots
    @bestieboots Месяц назад

    Your content is gold. And your voice is pleasant to listen to while learning.

  • @talalquleh9160
    @talalquleh9160 3 года назад +3

    great explanation👏🔥, thanks for the video

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

    Thanks, this cleared up a lot of confusion i had :)

  • @pitankar
    @pitankar 5 лет назад +6

    This is wonderful. Thanks for making these. I was also looking for tutorials on linker script, you know of any good resources on that?

    • @CodeVault
      @CodeVault  5 лет назад +2

      Sorry, I don't never really worked with linker scripts. I can't help you any more than just googling the subject and looking through the results.

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

    Thanks for this!! it was really insightful.. keep it up!

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

    you explain things to clearly. thank you !

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

    Thank you so much. That was one of my open question..

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

    So when you use malloc and after that your int *p will be equal with the address of the first 4 bytes( first int variable) aka p[0] so it's actually the same case like of an usually array? What I mean the whole block of memory you allocated it will have as his address,the address of p[0] ?

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

      I think this is what's confusing to you: the address of a block of memory (of however many bytes) is the address of the first byte in that block
      Of course the next integers in that array will be further away in memory so they will have a different address from the first one (since each int takes 4 bytes in memory)

  • @ar9iem
    @ar9iem 4 года назад +1

    Great explanation.

  • @itsmespiazzy9704
    @itsmespiazzy9704 3 года назад

    Question, what's the difference between passing ([][]) as a parameter instead of (**) for matrixes, for example.
    int foo(a[]);
    int foo(*a);
    with one I need to use as a good practice?
    do i need to always write foo ([]) if the passing parameter is a static allocated array?

    • @CodeVault
      @CodeVault  3 года назад +1

      arrays as function parameters are treated as pointers

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

      you can do either, I think most coders use foo(int* a) instead of foo(int[ ]) Thats what codeVault means when he says the array name decays to a pointer by using just the name as a parameter in a function call that takes a pointer

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

    What about when you need to dynamically allocate memory to a size not a constant (3 in your example). For instance, user inputs int sizeOfArray, but
    Int * arrp = malloc(sizeOfArray * (sizeof(int)) doesn't compile

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

      It should compile without any issues. Although you might have to call malloc after reading the sizeOfArray variable. Send me the code if it's still not compiling

  • @jld_86
    @jld_86 3 года назад

    Fantastic video. Great content!

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

    Why while intialization of a char array pointer memory allocated automaticly, but with int arrays we must allocate it manually

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

      Arrays are allocated automatically (regardless of type):
      char arr1[100]; // automatically allocated on the stack with 100 chars
      int arr2[100]; // automatically allocated on the stack with 100 integers
      Only when you use pointers and malloc it's different:
      char* arr3;
      arr3 = malloc(sizeof(char) * 100); // dynamic allocation, otherwise you usually get segmentation fault

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

    Wonderful job ! Thank you

  • @ed.peguillan.3
    @ed.peguillan.3 4 года назад +1

    EDIT: I see that you covered this here so nevermind!: ruclips.net/video/A8IkGIqZoLQ/видео.html
    This is a fantastic video, thank you for exploring this topic!
    I was just wondering if you might consider making a video in the future as a sort of addendum which covers this same concept, but also with the subtleties that come with strings? There are so many ways to declare strings in C. For example:
    char* readOnlyMemoryString = "spaghetti";
    char arrayDeclarationOnly[];
    char* dynamicString = malloc...;
    char sugarString[] = "test123";
    I got bitten by the first line at work recently because I did not realize that strings declared that way are placed in an area of memory that is neither in the stack nor the heap! I am finding string parsing to be very difficult. If you have already made a video on this topic, please disregard my comment! I am still making my way through your videos.

    • @CodeVault
      @CodeVault  4 года назад

      Here's a video specifically for that question (although quite old, please disregard the poor quality of the audio): ruclips.net/video/gTXRfETK3z4/видео.html

  • @BooHeeee
    @BooHeeee 3 года назад +1

    Good video, thank you

  • @Immerz
    @Immerz 3 года назад

    Thank you. I wondered why *array++ is invalid code. It kinda makes sense now why.

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

    Great 👍

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

    Merci beaucoup!

  • @wrosen03
    @wrosen03 3 года назад

    why does char *name = "Bob" work but
    char **names = {"Bob", "Bill", "Steve"} doesn't?

    • @CodeVault
      @CodeVault  3 года назад +1

      Because names is an array of strings not a pointer to string. You actually need to store those strings somewhere and char** names is just a pointer, it cannot store array elements.

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

    Why is p[0]=1 ? it shouldn't be *p[0]=1 since
    you are assigning a value and not an address?!

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

      p[0] is equivalent to *(p + 0)
      So your suggestion wouldn't work since *p[0] = 1; would be equivalent to **(p + 0) which would deallocate twice

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

      @@CodeVault Ty so much I need to learn better pointers and arrays.

  • @camygiuliani8758
    @camygiuliani8758 3 года назад

    Thank :)

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

    So there is no difference at all, they differ only with math of navigation, in loop with pointer you use pointer increment p + n, and if you use square bracket you just increment number in square brackets [i+n], i saw only this. The a both limited in stage of init, array[] limited with number in [], pointer array is limited with malloc, so no difference if you use [3] or malloc size int*3 in final.
    Yes, you can save memory in initialization, but who cares this in our days

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

      Basically yes. For most use cases there's no difference. Here are a couple more niche differences if you are curious:
      - A statically allocated array's size is fixed, once a number is given in those [] it can't be changed. In some compilers that number has to be a const. With a pointer, you can just use realloc to make the dynamically allocated array bigger
      - A statically allocated array has a maximum size limit due to limit of the stack memory (I think it's usually 1MB but could differ)
      - Arrays passed as parameters are actually considered pointers all the time (so even sizeof will return 8 bytes (or 4 on 32-bit)).
      - Multidimensional arrays are contiguous in memory, while you'd need to use two or more pairs of [] when dereferencing, the same implementation with pointers and dynamically allocated array would have to be using only one pair of [] when dereferencing since in both cases you always dereference once (regardless of the number of dimensions of that array)

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

      They are different. I challenge you to assign one array to another array. For example assign int A[3]={5,6,7} to B[3]={0}; so that B==A ?

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

      Ahh, my bad, that's another difference I missed. They behave like const pointers too

  • @mahadifamgate2686
    @mahadifamgate2686 3 года назад

    i can't understand why u have so less subscriber, your channel just a hidden PEARL , u need some publicity work.

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

    .....................free(p); ⛔
    ...p = NULL; free(p); 👍why?

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

      free(p);
      p = NULL;
      Would be the correct order. There is this video explaining the practice: code-vault.net/lesson/rh1tys8m13:1603733526917

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

      @@CodeVault thx u )