Skip to content

Commit 485da5c

Browse files
committed
poiners
1 parent 06a4768 commit 485da5c

File tree

4 files changed

+315
-0
lines changed

4 files changed

+315
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
a.out
22
.build
33
.DS_Store
4+
*.exe

C/dynamic-memorry.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# dynamic memory management
2+
```c
3+
#include<stdio.h>
4+
#include<stdlib.h>
5+
6+
/*
7+
malloc: allocate single block of memory with garbage value
8+
calloc: allocate multiple blocks of memory without garbage value
9+
realloc: resize memory of malloc and calloc memory
10+
free: free memory
11+
12+
*/
13+
14+
/*syntax
15+
malloc: ptr = (type-cast*) malloc(byte-size);
16+
calloc: ptr = (type-cast*) calloc(count, byte-size);
17+
realloc: pre = (type-cast*) realloc(ptr, new-byte-size);
18+
free: free(ptr);
19+
*/
20+
21+
int main(){
22+
int n, i, *ptr, sum = 0;
23+
printf("Enter number of elements: ");
24+
scanf("%d", &n);
25+
//ptr = (int *)malloc(n * sizeof(int));
26+
ptr = (int *)calloc(n * sizeof(int));
27+
ptr = realloc(ptr, n * 2 * sizeof(int));
28+
if(ptr == NULL){
29+
printf("Memory allocation failure!");
30+
exit(1);
31+
}
32+
printf("Elements of array: ");
33+
for(int i = 0; i < n; i++){
34+
scanf("%d", ptr + i);
35+
printf("%d\t", *(ptr + i));
36+
sum += *(ptr + i);
37+
}
38+
printf("\nsum = %d\n", sum);
39+
free(ptr);
40+
return 0;
41+
42+
}
43+
44+
```

C/dynamic-memory.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
4+
/*
5+
malloc: allocate single block of memory with garbage value
6+
calloc: allocate multiple blocks of memory without garbage value
7+
realloc: resize memory of malloc and calloc memory
8+
free: free memory
9+
10+
*/
11+
12+
/*syntax
13+
malloc: ptr = (type-cast*) malloc(byte-size);
14+
calloc: ptr = (type-cast*) calloc(count, byte-size);
15+
realloc: pre = (type-cast*) realloc(ptr, new-byte-size);
16+
free: free(ptr);
17+
*/
18+
19+
int main(){
20+
int n, i, *ptr, sum = 0;
21+
printf("Enter number of elements: ");
22+
scanf("%d", &n);
23+
//ptr = (int *)malloc(n * sizeof(int));
24+
ptr = (int *)calloc(n , sizeof(int));
25+
ptr = realloc(ptr, n * 2 * sizeof(int));
26+
if(ptr == NULL){
27+
printf("Memory allocation failure!");
28+
exit(1);
29+
}
30+
printf("Elements of array: ");
31+
for(int i = 0; i < n; i++){
32+
scanf("%d", ptr + i);
33+
printf("%d\t", *(ptr + i));
34+
sum += *(ptr + i);
35+
}
36+
printf("\nsum = %d\n", sum);
37+
free(ptr);
38+
return 0;
39+
40+
}

documents/pointers.md

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
# pointers
2+
A pointer is a variable that contains the address of a variable
3+
## Pointers and addresses
4+
`A typical machine has an array of consecutively numbered or addressed memory cells.`<br>
5+
`&: gives address of an object, this operator can be applied only to objects in memory : variable and array elements.`<br>
6+
`*: when applied to a pointer, it accesses the object that pointer points to.`<br>
7+
`Every pointer points to a specific data type`
8+
```cpp
9+
int x = 1, y = 2, z[10];
10+
int *ip; // pointer to int
11+
int *iq; // pointer to int
12+
ip = &x; // ip points to x, since ip stores the address of x
13+
iq = ip; // iq points to whatever ip pointed to
14+
y = *ip; // y is now 1
15+
*ip = 0; // x is now 0
16+
ip = z + 2; // ip points to z[2];
17+
*ip = 5; // z[2] is now 5
18+
```
19+
`if a pointer ptr points to a variable x, then *ptr can occur in any context where x could.`
20+
21+
## Pointers and Function arguments
22+
```cpp
23+
void swap(int *x, int *y){
24+
int * temp;
25+
temp = x;
26+
x = y;
27+
y = temp;
28+
}
29+
int main(){
30+
int a = 10, b = 20;
31+
swap(&a, &b); // pass address
32+
return 0;
33+
}
34+
```
35+
36+
## Pointers and arrays
37+
Any operation that can be achieved by array indexing can also be done with pointers.
38+
```cpp
39+
int arr[10]; // a[10]: {1,2,3,4,5,6,7,8,9,0}
40+
int *pa; // pa pointer to int
41+
pa = &arr[0]; // pa pointers to a
42+
pa = arr; // &arr[0] and array-name have identical value
43+
int x = *pa; // x is no arr[0]
44+
```
45+
`Note: A pointer is a variable so pa = arr and pa++ is legal, but an array name is not a variable so arr = pa or arr++ is illegal.`<br>
46+
`pass array name as function argument`
47+
48+
```cpp
49+
void printArray( const int *arr, int n){
50+
for(int i=0; i<n; i++){ // sizeof(arr) will return size of arr pointer not size of array.
51+
// *(arr + i) = 4; // error: read-only variable is not assignable
52+
printf("%d", *(arr + i));
53+
}
54+
}
55+
int main(){
56+
int arr[] = {0,1,2,3,4,5,6};
57+
printArray(arr, 7);
58+
}
59+
```
60+
61+
```cpp
62+
// strlen: return length of string s
63+
int strlen(const char *s){
64+
int n = 0;
65+
while(s!='\0'){
66+
n++;
67+
s++;
68+
}
69+
return n;
70+
}
71+
```
72+
## Pointes arithmetic
73+
`pointers, arrays, and address arithmetic is one of strengths of the language.`<br>
74+
`if p is a pointer to some element of an array, then p++ increments p to the next element of the array, and p+=i increments to point ith elements beyond where it currently pointed.`<br>
75+
76+
`The valid pointer operations` :
77+
1. assignment of pointers of the same type,
78+
2. adding and subtracting a pointer and integer,
79+
3. assigning and comparing to zero(NULL).<br>
80+
4. subtracting and comparing two pointers to members of the same array,
81+
82+
`Illegal pointer operations` :
83+
1. All other pointer operation are illegal,
84+
2. not legal to add/subtract/multiply/divide/shift/mask two pointers.
85+
3. add float or double to them.
86+
4. Assign a pointer of one type to a pointer of another type without a cast illegal. except `void *`
87+
88+
## Character pointers and functions
89+
A string constant, written as
90+
```cpp
91+
"I am a string"
92+
```
93+
is an array of characters. In the internal representation, the array is terminated with null character `\0` so that program can the find end.
94+
95+
```cpp
96+
char amessage[] = "now is time"; // amessage: [now is time\0]
97+
char *pmessage = "now is time"; // pmessage --> [now is time\0]
98+
```
99+
100+
## pointer array
101+
Array of pointers.
102+
```cpp
103+
/*
104+
int *p1, *p2;
105+
var: [p1, p2];
106+
*/
107+
int *var[2]; // var is array of size 2 pointer to integer
108+
/*
109+
var -> [1,2]
110+
*/
111+
int (*var)[2] // var is pointer to array of size 2 integer.
112+
```
113+
114+
## 2-D arrays
115+
If 2D array is to be passed to a function, parameter declaration in function must include the number of columnss the number of rows is irrelevant, since a pointer to an array of row is passed to a the function.
116+
```cpp
117+
// Online C++ compiler to run C++ program online
118+
#include <iostream>
119+
/*void show2D(int dayTab[4][5]){..}
120+
void show2D(int dayTab[][5]){...}*/
121+
void show2D(int (*dayTab)[5]){
122+
for(int i =0; i < 4; i++){
123+
for(int j = 0; j < 5; j++){
124+
std::cout << dayTab[i][j] << "\t";
125+
}
126+
std::cout <<"\n";
127+
}
128+
}
129+
int main() {
130+
int dayTab[4][5] = {{0,1,2,3,4},
131+
{0,1,2,3,4},
132+
{0,1,2,3,4},
133+
{0,1,2,3,4}
134+
};
135+
show2D(dayTab);
136+
return 0;
137+
}
138+
```
139+
140+
## Pointers to functions
141+
A function itself is not a variable, but it is possible to define pointers to functions which can be assigned, placed in arrays, passed to functions, returned by functions and so on.
142+
143+
```cpp
144+
// function pointer
145+
#include<iostream>
146+
using namespace std;
147+
typedef bool (*funPtr)(int, int);
148+
typedef int *p;
149+
bool fun(int x, int y){
150+
return x>y?true:false;
151+
}
152+
void fun1(int x, int y, bool (*fun2)(int, int)){
153+
154+
cout << boolalpha << fun2(x,y) << endl;
155+
}
156+
funPtr fun3(){
157+
return fun;
158+
}
159+
bool (*fun4())(int, int){
160+
return fun;
161+
}
162+
int main(){
163+
cout << boolalpha << fun(4,3) << endl;
164+
bool (*comp)(int, int);
165+
comp = fun;
166+
cout << boolalpha << comp(4,3) << endl;
167+
fun1(4,6,fun);
168+
funPtr f = fun3();
169+
cout<< boolalpha << f(4,5) << endl;
170+
funPtr arr[2] = {comp, f};
171+
cout << boolalpha << arr[0](4,5) << endl;
172+
cout << boolalpha << arr[0](7,5) << endl;
173+
bool(*fun5)(int, int) = fun4();
174+
cout << boolalpha << fun5(6,7) << endl;
175+
p q;
176+
q = new int(2);
177+
cout << *q;
178+
}
179+
/*
180+
true
181+
true
182+
false
183+
false
184+
false
185+
true
186+
false
187+
2
188+
*/
189+
```
190+
191+
## Complicated Pointer Declarations
192+
`right -> left rules`:<br>
193+
First, symbols. Read<br>
194+
- `*` as "pointer to"
195+
- `[]` as "array of"
196+
- `()` as "function returning"
197+
198+
1. STEP1:
199+
Find the identifier. This is your starting point.
200+
then say `Identifier is ...`
201+
2. STEP2: Look at the symbols on the right of the identifier. continue right until you run out of symbols *OR* hit a *right* parenthesis `)`.
202+
3. STEP3: Look at the symbols to the left of the identifier. If it is not one of our symbols just say it. otherwise translate into English using above table. keep going left until you run out of symbols *OR* hit a *left* parenthesis `(`.
203+
204+
Example:
205+
int *p[];
206+
1. find identifier "p is"
207+
2. move right until run out of symbols or right parenthesis hit.
208+
"p is array of"
209+
3. can't move right anymore so move left and find.
210+
"p is array of pointer to"
211+
4. keep going left until you run out of symbols or left parenthesis hit
212+
"P is array of pointer to int."
213+
<br>
214+
`int*(*func())()`
215+
func is function returning pointer to function returning pointer to int.
216+
<br>
217+
`int (*(*fun_one)(char *,double))[9][20];`
218+
fun_one is pointer to function(char *, double) returning pointer to array(size 9) of array(size 20) of int.
219+
220+
## typedef specifier used in declaration
221+
`Typedef can be used to aliasing predefined types.`
222+
223+
224+
225+
226+
227+
228+
229+
230+

0 commit comments

Comments
 (0)