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 : )
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
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
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] ?
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)
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
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?
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
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.
Here's a video specifically for that question (although quite old, please disregard the poor quality of the audio): ruclips.net/video/gTXRfETK3z4/видео.html
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.
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
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)
How can you have so little views, this content is the best I have found so far!
Best content on youtube about c as far as i know
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.
Racist
@@guitargeorge7255 🤣🤣🤣
This guy talks on English lanquage. Normal one. That is the difference.
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 : )
thank you for this series of c programming, very helpful and very easy explained with correct type of examples.. realy thx man
Your content is gold. And your voice is pleasant to listen to while learning.
great explanation👏🔥, thanks for the video
Thanks, this cleared up a lot of confusion i had :)
Thanks for this!! it was really insightful.. keep it up!
you explain things to clearly. thank you !
Great explanation.
Thank you so much. That was one of my open question..
This is wonderful. Thanks for making these. I was also looking for tutorials on linker script, you know of any good resources on that?
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.
Fantastic video. Great content!
Wonderful job ! Thank you
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
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
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] ?
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)
Why while intialization of a char array pointer memory allocated automaticly, but with int arrays we must allocate it manually
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
Good video, thank you
Great 👍
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?
arrays as function parameters are treated as pointers
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
Thank you. I wondered why *array++ is invalid code. It kinda makes sense now why.
Merci beaucoup!
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.
Here's a video specifically for that question (although quite old, please disregard the poor quality of the audio): ruclips.net/video/gTXRfETK3z4/видео.html
why does char *name = "Bob" work but
char **names = {"Bob", "Bill", "Steve"} doesn't?
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.
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
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)
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 ?
Ahh, my bad, that's another difference I missed. They behave like const pointers too
Thank :)
Why is p[0]=1 ? it shouldn't be *p[0]=1 since
you are assigning a value and not an address?!
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
@@CodeVault Ty so much I need to learn better pointers and arrays.
.....................free(p); ⛔
...p = NULL; free(p); 👍why?
free(p);
p = NULL;
Would be the correct order. There is this video explaining the practice: code-vault.net/lesson/rh1tys8m13:1603733526917
@@CodeVault thx u )
i can't understand why u have so less subscriber, your channel just a hidden PEARL , u need some publicity work.