Difference between arrays and pointers in C

Sdílet
Vložit

Komentáře • 46

  • @Miguel-mv5yc
    @Miguel-mv5yc Před 3 lety +22

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

    • @Alpharabius99
      @Alpharabius99 Před rokem

      Best content on youtube about c as far as i know

  • @noweare1
    @noweare1 Před 2 lety +12

    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 : )

  • @imperatusmauser7096
    @imperatusmauser7096 Před 3 lety +4

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

  • @sony1979iq
    @sony1979iq Před 3 lety +6

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

  • @talalquleh9160
    @talalquleh9160 Před 3 lety +3

    great explanation👏🔥, thanks for the video

  • @missrudy8553
    @missrudy8553 Před 2 lety

    you explain things to clearly. thank you !

  • @jorgealejandroleon9373

    Fantastic video. Great content!

  • @diskoboi3342
    @diskoboi3342 Před 3 lety +2

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

  • @rie9950
    @rie9950 Před 4 lety +1

    Great explanation.

  • @iamqaasim
    @iamqaasim Před 2 lety

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

  • @matteopisati9966
    @matteopisati9966 Před 2 lety

    Wonderful job ! Thank you

  • @navidnouri151
    @navidnouri151 Před 2 lety

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

  • @BooHeeee
    @BooHeeee Před 3 lety +1

    Good video, thank you

  • @pitankar
    @pitankar Před 4 lety +5

    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  Před 4 lety +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.

  • @itsmespiazzy9704
    @itsmespiazzy9704 Před 3 lety

    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  Před 3 lety +1

      arrays as function parameters are treated as pointers

    • @noweare1
      @noweare1 Před 2 lety

      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

  • @CosmJJ
    @CosmJJ Před rokem

    Merci beaucoup!

  • @monadeem8792
    @monadeem8792 Před rokem

    Great 👍

  • @Immerz
    @Immerz Před 3 lety

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

  • @StealOfApproval
    @StealOfApproval Před rokem

    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  Před rokem

      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

  • @edward3105
    @edward3105 Před rokem

    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  Před rokem +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)

  • @camygiuliani8758
    @camygiuliani8758 Před 2 lety

    Thank :)

  • @pawsdev
    @pawsdev Před 2 lety

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

    • @CodeVault
      @CodeVault  Před 2 lety

      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

  • @Yankee14ed
    @Yankee14ed Před 4 lety +1

    EDIT: I see that you covered this here so nevermind!: czcams.com/video/A8IkGIqZoLQ/video.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  Před 4 lety

      Here's a video specifically for that question (although quite old, please disregard the poor quality of the audio): czcams.com/video/gTXRfETK3z4/video.html

  • @wrosen03
    @wrosen03 Před 2 lety

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

    • @CodeVault
      @CodeVault  Před 2 lety +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.

  • @pawsdev
    @pawsdev Před 2 lety

    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  Před 2 lety +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 Před 2 lety

      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  Před 2 lety

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

  • @edward3105
    @edward3105 Před 2 lety

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

    • @CodeVault
      @CodeVault  Před 2 lety

      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 Před 2 lety

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

  • @lemmenmin7676
    @lemmenmin7676 Před rokem

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

    • @CodeVault
      @CodeVault  Před rokem +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 Před rokem

      @@CodeVault thx u )

  • @mahadifamgate2686
    @mahadifamgate2686 Před 2 lety

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