> For the complete documentation index, see [llms.txt](https://ykkim.gitbook.io/EC/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ykkim.gitbook.io/EC/c-programming/c-programming-review/pointer.md).

# Pointer

## Introduction

**코딩도장 요약** : [포인터 사용하기 핵심요약](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\&token=211aad27-25d9-469a-a0ba-0bee85926ef2)

#### 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\&token=ba496b0c-4e83-4708-a7e1-5692de80e388)

![](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\&token=85088d3c-ccd9-4312-9500-863a90119277)

![](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\&token=73a0d9fa-ebff-40ff-92a1-4496a81f76cc)

### 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\&token=8963ba76-eaba-48b4-ba69-42c85c696289)

## For EC

{% file src="/files/wDRE6rCg891LVapP2Kap" %}

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2F9NEoC5tIhDYmjkTAt3Xo%2Fimage.png?alt=media&amp;token=94755553-86a6-4516-93e0-6a45a7f1cab9" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2FJc9GpmWnODPcR3mQbn7W%2Fimage.png?alt=media&amp;token=25cae3f1-a3cf-4d7e-b816-8772ed2ff220" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3453146497-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MgmrEstOHxu62gXxq1t%2Fuploads%2FREQmlUwyR4I4BScYSTek%2Fimage.png?alt=media&amp;token=9410a750-ff11-439b-a775-777d2b1c1226" alt=""><figcaption></figcaption></figure>

###

## Exercise

* [Online C Compiler](https://my.newtonschool.co/playgrounds/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 *)

```
