Difference between arrays and pointers in C

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

CodeVault

CodeVault

5 жыл бұрын

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

Пікірлер: 46
@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 KZfaq channel. I learnt a lot more than the Indian dudes that can't explain what an unsigned integer is.
@guitargeorge7255
@guitargeorge7255 6 ай бұрын
Racist
@arielvaldman
@arielvaldman 3 ай бұрын
@@guitargeorge7255 🤣🤣🤣
@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
@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 : )
@talalquleh9160
@talalquleh9160 3 жыл бұрын
great explanation👏🔥, thanks for the video
@jorgealejandroleon9373
@jorgealejandroleon9373 3 жыл бұрын
Fantastic video. Great content!
@diskoboi3342
@diskoboi3342 3 жыл бұрын
Thanks, this cleared up a lot of confusion i had :)
@missrudy8553
@missrudy8553 2 жыл бұрын
you explain things to clearly. thank you !
@iamqaasim
@iamqaasim 2 жыл бұрын
Thanks for this!! it was really insightful.. keep it up!
@ar9iem
@ar9iem 4 жыл бұрын
Great explanation.
@navidnouri151
@navidnouri151 2 жыл бұрын
Thank you so much. That was one of my open question..
@matteopisati9966
@matteopisati9966 2 жыл бұрын
Wonderful job ! Thank you
@BooHeeee
@BooHeeee 3 жыл бұрын
Good video, thank you
@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.
@monadeem8792
@monadeem8792 Жыл бұрын
Great 👍
@Immerz
@Immerz 3 жыл бұрын
Thank you. I wondered why *array++ is invalid code. It kinda makes sense now why.
@CosmJJ
@CosmJJ Жыл бұрын
Merci beaucoup!
@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
@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 Жыл бұрын
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
@camygiuliani8758
@camygiuliani8758 2 жыл бұрын
Thank :)
@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
@Yankee14ed
@Yankee14ed 4 жыл бұрын
EDIT: I see that you covered this here so nevermind!: kzfaq.info/get/bejne/d555nqp61sDTfIU.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): kzfaq.info/get/bejne/nbqIhcl2ubGXqmg.html
@wrosen03
@wrosen03 2 жыл бұрын
why does char *name = "Bob" work but char **names = {"Bob", "Bill", "Steve"} doesn't?
@CodeVault
@CodeVault 2 жыл бұрын
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.
@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
@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 )
How to use dynamically allocated arrays
11:29
CodeVault
Рет қаралды 72 М.
array vs &array Pointers Difference Explained | C Programming Tutorial
17:38
Mama vs Son vs Daddy 😭🤣
00:13
DADDYSON SHOW
Рет қаралды 47 МЛН
БАБУШКИН КОМПОТ В СОЛО
00:23
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 18 МЛН
39kgのガリガリが踊る絵文字ダンス/39kg boney emoji dance#dance #ダンス #にんげんっていいな
00:16
💀Skeleton Ninja🥷【にんげんっていいなチャンネル】
Рет қаралды 8 МЛН
Arrays as function parameters in C
13:28
CodeVault
Рет қаралды 12 М.
How to properly deal with dynamically allocated memory
13:44
CodeVault
Рет қаралды 9 М.
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 293 М.
Why Function Pointers are Awesome
11:11
Jacob Beningo
Рет қаралды 6 М.
C Arrays and Pointers to Pointers
35:20
Kris Jordan
Рет қаралды 17 М.
Communicating between processes (using pipes) in C
14:00
CodeVault
Рет қаралды 271 М.
why do void* pointers even exist?
8:17
Low Level Learning
Рет қаралды 344 М.
What are double pointers in C?
14:43
CodeVault
Рет қаралды 42 М.
Запрещенный Гаджет для Авто с aliexpress 2
0:50
Тимур Сидельников
Рет қаралды 963 М.
Look, this is the 97th generation of the phone?
0:13
Edcers
Рет қаралды 7 МЛН
Nokia 3310 top
0:20
YT 𝒯𝒾𝓂𝓉𝒾𝓀
Рет қаралды 3,5 МЛН
Как бесплатно замутить iphone 15 pro max
0:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН