-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d446ca1
commit cd6140c
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
date: 2024-10-05 | ||
categories: | ||
- C | ||
- pointers | ||
--- | ||
|
||
# Pointers | ||
|
||
- Pointer: group of cells that hold | ||
|
||
```c | ||
p = &c // & gives address of an object (=c) | ||
``` | ||
|
||
- `*object`: indirection (dereferencing) operator | ||
- access the object which pointer points to | ||
|
||
```c | ||
int x = 1, y = 10; | ||
int *p; // p is a pointer to int | ||
|
||
p = &x // p points to x (=1) | ||
y = *p // x = y = 1 | ||
``` | ||
|
||
- **Every pointer points to a particular object** | ||
|
||
```c | ||
int x = 1 | ||
int *p; // declaration | ||
|
||
p = &x; | ||
*p = *p + 10; // x = x + 10 | ||
``` | ||
|
||
## Pointers-functions | ||
|
||
- Using pointers as arguments, you can modify parameters even if c follows call by value rule | ||
|
||
```c | ||
f(&a[2]) == f(a + 2) | ||
|
||
f(int array[]) == f(int *arr) | ||
``` | ||
## Pointers-arrays | ||
- Pointer is almost equal to array except one difference. | ||
- The difference is that pointer is variable so it can be passed as arguments. | ||
- Example | ||
```c | ||
int *pa; | ||
pa = &a[0] // == (pa = a) | ||
&a[i] == a + i | ||
a[i] == *(a + i) | ||
*(pa + i) == a + i | ||
*(pa + i) == pa[i] | ||
``` | ||
|
||
- String: arrays which contain character | ||
|
||
```c | ||
char s[] == char *s | ||
``` | ||
|
||
reference: C programming language 2d edition |