Browse Source

merge remote HEAD with local HEAD

master
T. Meissner 11 years ago
parent
commit
9b0948507c
1 changed files with 50 additions and 23 deletions
  1. +50
    -23
      c_pointers/chapter_4.c

+ 50
- 23
c_pointers/chapter_4.c View File

@ -4,6 +4,8 @@
void displayArray(int arr[], size_t size); void displayArray(int arr[], size_t size);
void displayArrayPtr(int *arr, size_t size); void displayArrayPtr(int *arr, size_t size);
int safeCompare(const unsigned char *a, const unsigned char *b, size_t asize, size_t bsize);
int valid(void *p);
void displayArray(int arr[], size_t size) { void displayArray(int arr[], size_t size) {
@ -24,12 +26,21 @@ void displayArrayPtr(int *arr, size_t size) {
} }
int safeCompare(const unsigned char *a, const unsigned char *b, size_t asize, size_t bsize) {
if (asize != bsize) {
return -1;
}
int value = 0;
for (size_t i = 0; i < asize; i++) {
value |= a[i] ^ b[i];
}
return value;
}
int main(void) { int main(void) {
int vector[5] = {1,2,3,4,5}; int vector[5] = {1,2,3,4,5};
int *pv;
pv = vector;
printf("length of vector: %zu\n", sizeof(vector)/sizeof(int)); printf("length of vector: %zu\n", sizeof(vector)/sizeof(int));
@ -41,28 +52,33 @@ int main(void) {
} }
// pointer notation and arrays // pointer notation and arrays
int value = 3;
for (size_t index = 0; index < sizeof(vector)/sizeof(int); index++) {
// all following operations are equivalent
*pv++ *= value;
//pv[index] *= value;
//*(pv + index) *= value;
//vector[index] *= value;
}
for (size_t index = 0; index < sizeof(vector)/sizeof(int); index++) {
printf("vector[%zu]: %d\n", index, vector[index]);
{
int *pv = vector;
int value = 3;
for (size_t index = 0; index < sizeof(vector)/sizeof(int); index++) {
// all following operations are equivalent
*pv++ *= value;
//pv[index] *= value;
//*(pv + index) *= value;
//vector[index] *= value;
}
for (size_t index = 0; index < sizeof(vector)/sizeof(int); index++) {
printf("vector[%zu]: %d\n", index, vector[index]);
}
} }
// using malloc to create a one-dimensional array // using malloc to create a one-dimensional array
pv = (int*) malloc(5 * sizeof(int));
for (size_t i = 0; i < 5; i++) {
pv[i] = i + 1;
}
for (size_t i = 0; i < 5; ++i) {
*(pv + i) = i + 1;
}
for(size_t index = 0; index < 5; index++) {
printf("pv[%zu]: %d\n", index, pv[index]);
{
int *pv = malloc(5 * sizeof(*pv));
for (size_t i = 0; i < 5; i++) {
pv[i] = i + 1;
}
for (size_t i = 0; i < 5; ++i) {
*(pv + i) = i + 1;
}
for(size_t index = 0; index < 5; index++) {
printf("pv[%zu]: %d\n", index, pv[index]);
}
} }
// passing array to function using array notation // passing array to function using array notation
@ -77,10 +93,12 @@ int main(void) {
int *arr[5]; int *arr[5];
for (size_t i = 0; i < 5; i++) { for (size_t i = 0; i < 5; i++) {
// both variants are equivalent // both variants are equivalent
arr[i] = malloc(sizeof(int));
arr[i] = malloc(sizeof(arr[i]));
*arr[i] = i; *arr[i] = i;
//arr[i][0] = i;
//*(arr + i) = (int*) malloc(sizeof(int)); //*(arr + i) = (int*) malloc(sizeof(int));
//**(arr + i) = i; //**(arr + i) = i;
printf("%d\n", **(arr + i));
} }
} }
@ -92,6 +110,15 @@ int main(void) {
} }
} }
unsigned char a[] = "wurst";
unsigned char b[] = "wurst";
if(safeCompare(a, b, sizeof(a), sizeof(b))) {
printf("%s ungleich %s\n", a, b);
} else {
printf("%s gleich %s\n", a, b);
}
return 0; return 0;
} }

Loading…
Cancel
Save