What's the difference between this two term "Array of pointers" and "Pointers to an array"? Please help, Thank you. | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 1

What's the difference between this two term "Array of pointers" and "Pointers to an array"? Please help, Thank you.

20th Jun 2021, 8:22 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
5 Antworten
+ 4
" int* a[5] " is an array of 5 integer pointers " int (*ptr) [5] " is a pointer that will point to an array of 5 Integers. you might wanna look at following discussion to gain more info about the topic 👇 https://www.sololearn.com/Discuss/2752595/?ref=app
20th Jun 2021, 8:39 AM
Arsenic
Arsenic - avatar
+ 1
Arsenic Thank you.. Sir. It's really helpful but I'm still wondering why do we use pointer to an array it doing same thing what array of pointers do also the same way we are accessing elements. Please help me with example. (I went through that discussion and attached code there) but still still too confusing
20th Jun 2021, 6:21 PM
I Am a Baked Potato
I Am a Baked Potato - avatar
+ 1
Tushar Kumar when using pointer to array, you need only one pointer to point to a 2-D array. Comparing this to array of pointers, we have *n* pointers ( each pointing to a different row of the 2-D array ) which will take more space and will also be harder to manage compared to one single pointer ( for example, when passing/returning arrays to and from different function )
21st Jun 2021, 2:28 AM
Arsenic
Arsenic - avatar
+ 1
Arsenic Sir how do I'll know this particular value is for row and other is for colum? mean how I will actually assign value in row and column way inside multidimensional array implimented by pointer to an integer? Also please clear my doubt, this works perfectly.. char* prompt1="Hello";     char* prompt2[]={"Please","help","me!"}; but this one not why? It throw warning. int* prompt3[]={10,20,30}; for(int i=0; i<3; i++) https://code.sololearn.com/cPV0j1F4bQZW/?ref=app
21st Jun 2021, 10:49 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
0
Tushar Kumar ( sorry for late reply ) C/C++ natively have row major storage order of multidimensional arrays, which means all the consecutive elements of a row reside next to each other. To understand this clearly, you can consider a 2-D array as a 1-D array where each element is a 1-D array. So if the pointer points to the element (0,0) of the matrix, the element next to it would be the one at (0,1) and the element next to the (0,n) { where 'n' is length of the array - 1 } would be (1,0) and so on. As for the warning is considered, it is because you are trying to manually assign a perticular value to the pointer data-type ( which is meant to store addresses in it ) and as you are not explicitly casting the value to a pointer type, the compiler thunks it is something programmer doesn't intended( which is true in this case) and thus gives warning regarding the same.
22nd Jun 2021, 4:17 PM
Arsenic
Arsenic - avatar