Difference between arrays and pointers in C

  Рет қаралды 32,341

CodeVault

CodeVault

Күн бұрын

int[] and int*... what is the difference actually? That's what we're trying to clarify in this video.

Пікірлер: 47
@Miguel-mv5yc
@Miguel-mv5yc 3 жыл бұрын
How can you have so little views, this content is the best I have found so far!
@Alpharabius99
@Alpharabius99 Жыл бұрын
Best content on youtube about c as far as i know
@imperatusmauser7096
@imperatusmauser7096 3 жыл бұрын
Hands down the best programming KZbin channel. I learnt a lot more than the Indian dudes that can't explain what an unsigned integer is.
@guitargeorge7255
@guitargeorge7255 7 ай бұрын
Racist
@arielvaldman
@arielvaldman 4 ай бұрын
@@guitargeorge7255 🤣🤣🤣
@dragancens
@dragancens 12 күн бұрын
This guy talks on English lanquage. Normal one. That is the difference.
@noweare1
@noweare1 2 жыл бұрын
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 жыл бұрын
thank you for this series of c programming, very helpful and very easy explained with correct type of examples.. realy thx man
@talalquleh9160
@talalquleh9160 3 жыл бұрын
great explanation👏🔥, thanks for the video
@diskoboi3342
@diskoboi3342 3 жыл бұрын
Thanks, this cleared up a lot of confusion i had :)
@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..
@ar9iem
@ar9iem 4 жыл бұрын
Great explanation.
@jld_86
@jld_86 3 жыл бұрын
Fantastic video. Great content!
@pitankar
@pitankar 4 жыл бұрын
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 4 жыл бұрын
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.
@matteopisati9966
@matteopisati9966 2 жыл бұрын
Wonderful job ! Thank you
@BooHeeee
@BooHeeee 3 жыл бұрын
Good video, thank you
@monadeem8792
@monadeem8792 Жыл бұрын
Great 👍
@Immerz
@Immerz 3 жыл бұрын
Thank you. I wondered why *array++ is invalid code. It kinda makes sense now why.
@ed.peguillan.3
@ed.peguillan.3 4 жыл бұрын
EDIT: I see that you covered this here so nevermind!: kzbin.info/www/bejne/d2msnHp_pr-igrM 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): kzbin.info/www/bejne/nYW7g5l7ibBmsJY
@CosmJJ
@CosmJJ 2 жыл бұрын
Merci beaucoup!
@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 жыл бұрын
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)
@StealOfApproval
@StealOfApproval Жыл бұрын
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 Жыл бұрын
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
@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 жыл бұрын
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
@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
@camygiuliani8758
@camygiuliani8758 3 жыл бұрын
Thank :)
@wrosen03
@wrosen03 3 жыл бұрын
why does char *name = "Bob" work but char **names = {"Bob", "Bill", "Steve"} doesn't?
@CodeVault
@CodeVault 3 жыл бұрын
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 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 жыл бұрын
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
@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.
@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 Жыл бұрын
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 )
Difference between memmove and memcpy
9:15
CodeVault
Рет қаралды 25 М.
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 45 МЛН
Фейковый воришка 😂
00:51
КАРЕНА МАКАРЕНА
Рет қаралды 6 МЛН
Incredible Dog Rescues Kittens from Bus - Inspiring Story #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 37 МЛН
C Arrays and Pointers to Pointers
35:20
Kris Jordan
Рет қаралды 17 М.
How to use dynamically allocated arrays
11:29
CodeVault
Рет қаралды 72 М.
Pointers and arrays
8:43
mycodeschool
Рет қаралды 644 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 359 М.
What are double pointers in C?
14:43
CodeVault
Рет қаралды 43 М.
Arrays as function parameters in C
13:28
CodeVault
Рет қаралды 12 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,2 МЛН
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 304 М.
How to properly deal with dynamically allocated memory
13:44
CodeVault
Рет қаралды 9 М.
But, what is Virtual Memory?
20:11
Tech With Nikola
Рет қаралды 268 М.
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 45 МЛН