Last active
June 7, 2019 10:09
-
-
Save AssafTzurEl/334e419776839a861c196e8bba7df40b to your computer and use it in GitHub Desktop.
C workshop
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
#include <stdio.h> | |
void f(int arr[]) // == void f(int *arr) | |
{ | |
size_t size = sizeof(arr); | |
printf("Size in f(): %d\n", size); | |
} | |
void main() | |
{ | |
int arr[] = { 10, 20, 30 }; | |
size_t size = sizeof(arr); | |
printf("Size in main(): %d\n", size); | |
f(arr); | |
} |
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
#include <stdio.h> | |
#define M 2 | |
#define N 3 | |
//void Print2DArray(int **arr, size_t rowSize, size_t columnSize) | |
//void Print2DArray(int arr[][], size_t rowSize, size_t columnSize) | |
void Print2DArray(int arr[][N], size_t rowSize, size_t columnSize) // works only on specific solumn size | |
{ | |
for (size_t i = 0; i < rowSize; i++) | |
{ | |
for (size_t j = 0; j < columnSize; j++) | |
{ | |
printf("%d ", arr[i][j]); | |
} | |
printf("\n"); | |
} | |
} | |
void Print2DArray_v2(int **arr, size_t rows, size_t columns) | |
{ | |
int *p = (int *) arr; // Use as one-dimensional array | |
for (size_t i = 0; i < rows; i++) | |
{ | |
for (size_t j = 0; j < columns; j++) | |
{ | |
printf("%d ", p[i * columns + j]); | |
} | |
printf("\n"); | |
} | |
} | |
void main() | |
{ | |
int arr2d[M][N] = | |
{ | |
{10, 20, 30}, | |
{40, 50, 60} | |
}; | |
Print2DArray(arr2d, M, N); | |
printf("\n"); | |
Print2DArray_v2(arr2d, M, N); | |
} |
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
#include <stdio.h> | |
#include <malloc.h> | |
int Sum(int value) | |
{ | |
static int sum = 0; | |
sum += value; | |
return sum; | |
} | |
int global; | |
void f() | |
{ | |
int j; | |
global++; | |
int *r = malloc(4); | |
} | |
void main() | |
{ | |
int *p = malloc(4); | |
int *q = malloc(4); | |
global = 0; | |
int i = Sum(10); | |
i = Sum(20); | |
f(); | |
} | |
/* | |
Variable addresses - notice stack, heap, globals "neighborhoods": | |
------------------- | |
+ &p 0x00fcfe64 {0x01024f30 {-842150451}} int * * | |
+ &q 0x00fcfe58 {0x01024f60 {-842150451}} int * * | |
+ &i 0x00fcfe4c {30} int * | |
+ &global 0x0116a574 {CApp.exe!int global} {1} int * | |
+ &sum 0x0116a138 {CApp.exe!int sum} {10} int * | |
+ &j 0x00fcfd6c {-858993460} int * | |
+ &r 0x00fcfd60 {0x01025ab8 {-842150451}} int * * | |
+ r 0x01025ab8 {-842150451} int * | |
+ p 0x01024f30 {-842150451} int * | |
+ q 0x01024f60 {-842150451} int * | |
*/ |
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
#include <stdio.h> | |
#define M 5 | |
#define N 4 | |
void main() | |
{ | |
int arr[] = { 10, 20, 30 }; | |
int *p = arr; | |
int i; | |
arr == *arr; | |
*p == arr[0] == *arr == p[0]; | |
p + 2 == &(arr[2]) == arr + 2 == &(p[2]); | |
p = &i; | |
// arr = &i; arr is const pointer | |
// arr[N] compiles to (pseudo-code): *(arr + N * sizeof(TYPE)) | |
int arr2d[M][N]; | |
int **p2d = arr2d; | |
int *p2d_2 = arr2d[2]; | |
// arr2d[2] compiles to (pseudo-code): *(arr2d + 2 * N * sizeof(TYPE)) | |
// arr2d[2][3] compiles to (pseudo-code): *(arr2d + 2 * N * sizeof(TYPE) + 3 * sizeof(TYPE)) | |
int sum = 0; | |
// Inefficient: | |
for (size_t i = 0; i < M; i++) | |
{ | |
for (size_t j = 0; j < N; j++) | |
{ | |
sum += arr2d[i][j]; | |
} | |
} | |
// More efficient: | |
sum = 0; | |
int *iterator = *arr2d; // == &(arr2d[0][0]) | |
for (size_t i = 0; i < M * N; i++) | |
{ | |
sum += iterator[i]; | |
} | |
// Most efficient: | |
sum = 0; | |
const int *end = *arr2d + (M * N); | |
for (int *iterator = *arr2d /* == &(arr2d[0][0]) */; iterator < end; ++iterator) | |
{ | |
sum += *iterator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment