Browse Source

added examples for (passing) multidimensional arrays; dynamic allocated multidim. arrays & jagged arrays

master
T. Meissner 11 years ago
parent
commit
31e6e28eb8
1 changed files with 200 additions and 1 deletions
  1. +200
    -1
      c_pointers/chapter_4.c

+ 200
- 1
c_pointers/chapter_4.c View File

@ -1,11 +1,16 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void displayArray(int arr[], size_t size);
void displayArrayPtr(int *arr, size_t size);
int safeCompare(const void *a, const void *b, size_t asize, size_t bsize);
int valid(void *p);
void display2DArray(int arr[][5], size_t rows);
void display2DArrayPtr(int (*arr)[5], size_t rows);
void display2DArrayUnknownSize(int *arr, size_t rows, size_t cols);
void display3DArrayPtr(int (*arr)[2][4], size_t rows);
void displayArray(int arr[], size_t size) {
@ -33,12 +38,61 @@ int safeCompare(const void *a, const void *b, size_t asize, size_t bsize) {
int value = 0;
for (size_t i = 0; i < asize; i++) {
//value |= a[i] ^ b[i];
value |= ((char*) a)[i] ^ ((char*) b)[i];
value |= ((unsigned char*) a)[i] ^ ((unsigned char*) b)[i];
}
return value;
}
void display2DArray(int arr[][5], size_t rows) {
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < 5; j++) {
printf("%d", arr[i][j]);
}
printf("\n");
}
}
void display2DArrayPtr(int (*arr)[5], size_t rows) {
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < 5; j++) {
printf("%d", *(arr[i] + j));
}
printf("\n");
}
}
void display2DArrayUnknownSize(int *arr, size_t rows, size_t cols) {
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
// both are equivalent
//printf("%d", *(arr + (i * cols) + j));
printf("%d", (arr + (i * cols))[j]);
}
printf("\n");
}
}
void display3DArrayPtr(int (*arr)[2][4], size_t rows) {
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < 2; j++) {
printf("{");
for (size_t k = 0; k < 4; k++) {
printf("%d", arr[i][j][k]);
if (k != 3) {
printf(" ");
}
}
printf("}");
}
printf("\n");
}
}
int main(void) {
int vector[5] = {1,2,3,4,5};
@ -80,6 +134,7 @@ int main(void) {
for(size_t index = 0; index < 5; index++) {
printf("pv[%zu]: %d\n", index, pv[index]);
}
free(pv);
}
// passing array to function using array notation
@ -101,8 +156,12 @@ int main(void) {
//**(arr + i) = i;
printf("%d\n", **(arr + i));
}
for (size_t i = 0; i < 5; i++) {
free(arr[i]);
}
}
// pointers and multidimensional arrays
{
// declare a 2-dimensional array
@ -125,6 +184,125 @@ int main(void) {
printf("%p %d\n", matrix[0] + 1, *(matrix[0] + 1));
}
// passing a multidimensional array
{
// declare a 2-dimensional array
int matrix[2][5] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
display2DArray(matrix, 2);
display2DArrayPtr(matrix, 2);
display2DArrayUnknownSize(&matrix[0][0],
sizeof(matrix) / sizeof(matrix[0]),
sizeof(matrix[0]) / sizeof(*matrix[0]));
int arr3D[][2][4] = {
{{1, 2, 3, 4}, {5, 6, 7, 8}},
{{9, 10, 11, 12}, {13, 14, 15, 16}},
{{17, 18, 19, 20}, {21, 22, 23, 24}}
};
display3DArrayPtr(arr3D, 3);
}
// dynamically allocating a two-dimensional array
{
// allocating contigously memory
int matrix[2][5] = {{1, 2, 3, 4, 6}, {6, 7, 8, 9, 10}};
for (size_t i = 0; i < 2; i++) {
for (size_t j = 0; j < 5; j++) {
printf("matrix[%zu][%zu] Address: %p Value: %d\n", i, j, &matrix[i][j], matrix[i][j]);
}
printf("\n");
}
// allocating potential noncontiguous memory
{
size_t rows = 2;
size_t cols = 5;
int **matrix = malloc(rows * sizeof(int *));
for (size_t i = 0; i < rows; i++) {
matrix[i] = malloc(cols * sizeof(int));
}
free(matrix);
}
// allocating contiguous memory
{
size_t rows = 2;
size_t cols = 5;
int **matrix = malloc(rows * sizeof(int*));
matrix[0] = malloc(rows * cols * sizeof(int));
for (size_t i = 1; i < rows; i++) {
matrix[i] = matrix[0] + i * cols;
}
free(matrix);
}
// allocating contiguous memory
{
size_t rows = 2;
size_t cols = 5;
int *matrix = malloc(rows * cols * sizeof(int));
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
*(matrix + (i * cols) + j) = i * j;
}
}
free(matrix);
}
// jagged arrays and pointers
{
// 2 dimensional array with compound literals
int *arr1[] = {
(int[]) {0, 1, 2},
(int[]) {3, 4, 5},
(int[]) {6, 7, 8}
};
for (size_t i = 0; i < 3; i++) {
for (size_t j = 0; j < 3; j++) {
printf("arr1[%zu][%zu] Address: %p Value: %d\n", i, j, &arr1[i][j], arr1[i][j]);
}
printf("\n");
}
// 2 dimensinal jagged array with compound literals
int *arr2[] = {
(int[]) {0, 1, 2, 3},
(int[]) {4, 5},
(int[]) {6, 7, 8}
};
for (size_t i = 0; i < 4; i++) {
printf("arr2[0][%zu] Address: %p Value: %d\n", i, &arr2[0][i], *(arr2[0] + i));
}
printf("\n");
for (size_t i = 0; i < 2; i++) {
printf("arr2[1][%zu] Address: %p Value: %d\n", i, &arr2[1][i], *(arr2[1] + i));
}
printf("\n");
for (size_t i = 0; i < 3; i++) {
printf("arr2[2][%zu] Address: %p Value: %d\n", i, &arr2[2][i], arr2[2][i]);
}
printf("\n");
}
}
// test safeCompare function
{
// test with char parameters
@ -146,6 +324,27 @@ int main(void) {
} else {
printf("%s gleich %s\n", "bla", "bli");
}
// test with unsigned char parameters
unsigned char blar[] = {0x00, 0x01, 0x02, 0x03, 0x04};
unsigned char blir[] = {0x00, 0x01, 0x02, 0x03, 0x04};
if(safeCompare(blar, blir, sizeof(blar), sizeof(blir))) {
printf("%s ungleich %s\n", "blar", "blir");
} else {
printf("%s gleich %s\n", "blar", "blir");
}
// test with unsigned char
unsigned char blab = 0x00;
unsigned char blib = 0x01;
if(safeCompare(&blab, &blib, sizeof(blab), sizeof(blib))) {
printf("%s ungleich %s\n", "blar", "blir");
} else {
printf("%s gleich %s\n", "blar", "blir");
}
}
return 0;


Loading…
Cancel
Save