-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.c
More file actions
45 lines (36 loc) · 990 Bytes
/
pointers.c
File metadata and controls
45 lines (36 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
int main(){
/*
int var = 10;
int *ptr = &var;
int **pptr = &ptr;
printf("%d \n", *ptr);
printf("%d \n", *pptr); //shows memory address of ptr
printf("%d \n", **pptr); //shows the value, dereferences twice
int *arr[5]; //stored stack, better for constant stuff
int **arr2 = (int **)malloc(5*sizeof(int*)); //same thing, stored heap
for(int i=0; i<5; i++){
arr[i] = (int *)malloc(sizeof(int));
*arr[i] = i;
}
for(int i = 0; i < 5; i++){
printf("%4d", *arr[i]);
}*/
int rows=2, columns=4, value=1;
int ** matrix = (int **)malloc(rows * sizeof(int *));
for(int i = 0; i < rows; i++){
matrix[i] = (int *)malloc((columns+i)*sizeof(int)); //jagged
}
for(int i=0; i<2; i++){
for(int j = 0; j < columns+i; j++){
matrix[i][j] = value++;
}
}
for(int i=0; i<2; i++){
for(int j = 0; j < columns+i; j++){
printf("%4d", matrix[i][j]);
}
}
return 0;
}