Pointers and Arrays

2022. 7. 2. 00:57Coding - C

Constant Pointer: Pointer that can't change the address value

Pointer to Constant: Pointer that refers to a constant

http://www.tcpschool.com/c/c_pointerArray_relation

In this example, arr[0], arr[1], arr[2] will be same as ptr_arr[0], ptr_arr[1], ptr_arr[2], which is 10, 20, 30. The ptr_arr's value is same as arr, which we can see that the array acts as a pointer, as the address value is needed to get value by using the asterisk. So the pointer and array name can replace each other. But for the size, while the arr gives 12, the ptr_arr gives 8 since sizeof(ptr_arr) just gives the size of the pointer itself.  

 

 

Array's Pointer Operations

Backwards with the previous example, this example uses array name as a pointer.

http://www.tcpschool.com/c/c_pointerArray_relation

The results will be the same, as 10, 20, 30. The name of the array, arr, shows the address of the starting point of the array. By using *(arr+1), it will print 10, as it will use the asterisk to get the value that is stored in that address of the second component of the array. The formula will be arr[n] = *(arr + (n-1)). 

http://www.tcpschool.com/c/c_pointerArray_relation

 

Pointer Arrays: Arrays that have pointers(addresses) for its components. For example, if we have int *arr[3] = {&num01, &num02, &num03}, then it is pointer arrays.

http://www.tcpschool.com/c/c_pointerArray_relation

Here is the diagram for pointer arrays. 

 

Array Pointers: Pointer that have the address of the array. This only works for two or more dimensional arrays. 

http://www.tcpschool.com/c/c_pointerArray_relation

This would print 10 and 40, as the array name function as the pointer, and arr[1] 40 since 1 is 2nd column.

http://www.tcpschool.com/c/c_pointerArray_relation

In this case, (*pArr) would be the Array pointer since it is a pointer that has an array as a component. Both values will be 50, as the array name acts as a pointer, and it is the 2nd component of 2nd column.

 

*int (*pArr) = Array Pointer, int* pArr or int *pArr = Pointer Arrays. 

'Coding - C' 카테고리의 다른 글

Character and Strings  (0) 2022.07.09
Memory Management  (0) 2022.07.08
Pointers  (0) 2022.07.02
Practice Problem Part 2 & Practice Problem #2  (0) 2022.06.25
Multi-Dimensional Array and Practice Problem: Part 1  (0) 2022.06.18