# Pointer

## Lecture PPT

### For Numerical Programming

{% file src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2FGkNtfz21qCOPNCtTPWNK%2F(C-program)%20Pointer_Array_2022.pdf?alt=media&token=6997338f-ee7a-4ea6-8951-d6343d939201" %}

### For Embedded Controller

{% file src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2FSL2J83YrocOawwURziyk%2F(C-program)%20Embedded_Pointer_Array_2023.pdf?alt=media&token=63b9afa2-c5c1-47e5-a530-db5e6d176f47" %}

## Summary

**코딩도장 요약** : [포인터 사용하기 핵심요약](https://dojang.io/mod/page/view.php?id=605)



What are Pointers?

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.

(a) Define a pointer variable: int \*ptr;

(b) Assign the address of a variable to a pointer: ptr = \&var;

(c) Access the value at the address available in the pointer variable: int value = \*ptr

![](https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2Fgit-blob-4c5a62d223c2207cd1471012d6990af862a5597c%2Fimage.png?alt=media)

#### For 32-Bit Word system (e.g. MCU)

32-CPU: 1 word = 4 bytes (32bit)

* Memory: byte units
* Int : 4 byte
* Pointer variable: 4 byte (default)

![TCP school.com](https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2Fgit-blob-9dcead925fbdb1c21ed719b42b458b555a1feb53%2Fimage.png?alt=media)

![](https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2Fgit-blob-b355f936944ff0a62a337d3129d63e5829d83c25%2Fimage.png?alt=media)

![](https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2Fgit-blob-7f000ba8efb6f4bd933d77a8dec1531b1e4b98fb%2Fimage.png?alt=media)

### Example Code

```cpp
#include <stdio.h>
#include <stdlib.h>

int main() {
int  var = 20;   
double a[4] = { 2, 2, 3, 4 };

// Pointer Declaration
int  *ptr;
double *ptr2 = NULL;// good practice for not Addr. assgined pointer
double *ptr3 = NULL;

// Pointer Assignment
ptr = &var;// store address of var in pointer variable
ptr2 = a;
ptr3 = &a[0];

printf("Address of var: %x\n", &var);
printf("Address of a  : %x\n", &a);
printf("Address of a[0]: %x\n", &a[0]);

// Using Pointer - Access the values pointed by Ptr
printf("\nAddress stored in \n  ptr: %x \n ptr2: %x  \n ptr3: %x\n", ptr, ptr2, ptr3);
printf("Value of \n  *ptr: %d \n *ptr2: %.1f  \n *ptr3: %.1f\n", *ptr, *ptr2, *ptr3);

system("pause");
return 0;
}
```

![](https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2Fgit-blob-58e5810c1eff4a027114f08a2a1504da977c8a4a%2Fimage.png?alt=media)

## Exercise

* [Online C Compiler](https://www.onlinegdb.com/online_c_compiler)
* [Exercise Code](https://github.com/ykkimhgu/Tutorial-C-Program/tree/main/pointer-array)



### Exercise 1

다음 소스 코드를 완성하여 10과 20이 각 줄에 출력되게 만드세요.

```c
#include <stdio.h>

int main()
{
    int *numPtr;
    int num1 = 10;
    int num2 = 20;

    ① ________________
    printf("%d\n", *numPtr);

    ②_________________
    printf("%d\n", *numPtr);

    return 0;
}
```

실행 결과

```
10
20
```

<details>

<summary><strong>Solution</strong></summary>

```c
① numPtr = &num1;
② numPtr = &num2;
```

</details>





### Exercise 2

```c
int x =10;            
double y=2.5;
int *ptrX = &x;      
int *ptrY = &y;

/*
-Print the address of variable ‘x’
-Print the address of variable ‘y’
-Print the value of pointer ‘ptrX ‘
-Print the address of pointer ‘ptrX ‘
-Print the size of pointer ‘ptrX ‘
*/

/*
-Print the value of pointer ‘ptrY ‘
-Print the address of pointer ‘ptrY ‘
-Print the size of pointer ‘ptrY 
*/


```



### Exercise 3 - for EC only

```c
int x =10;            
double y=2.5;
int *ptrX = &x;      
int *ptrY = &y;


// Typecast pointer 'ptrY' to as (double *)

```





