By Kyle Loudon
Price: $39.95 USD
£28.50 GBP
Cover | Table of Contents | Colophon
int f(int **iptr) {
int a = 10;
*iptr = &a;
return 0;
}typedef struct ListElmt_ {
void *data;
struct ListElmt_ *next;
} ListElmt;Incorrect Swap | Correct Swap |
|---|---|
void swap1(int x, int y) {
int tmp;
tmp = x; x = y; y = tmp;
return;
} | void swap2(int *x, int *y) {
int tmp;
tmp = *x; *x = *y; *y = tmp;
return;
} |
#include <stdlib.h>
#include <string.h>
int swap2(void *x, void *y, int size) {
void *tmp;
if ((tmp = malloc(size)) == NULL)
return -1;
memcpy(tmp, x, size); memcpy(x, y, size); memcpy(y, tmp, size);
free(tmp);
return 0;
}int (*match)(void *key1, void *key2);
match = match_int;
retval = match(&x, &y);
a) char *sptr = "abc",*tptr; *tptr = sptr; | b) char *sptr = "abc",*tptr; tptr = sptr; |
c) char *sptr = "abc",*tptr; *tptr = *sptr; | d) int *iptr = (int *)10; *iptr = 11; |
e) int *iptr = 10; *iptr = 11; | f ) int *iptr = (int *)10; iptr = NULL; |
*(p + 5)






/*****************************************************************************
* *
* ------------------------------- issort.c ------------------------------- *
* *
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "sort.h"
/*****************************************************************************
* *
* -------------------------------- issort -------------------------------- *
* *
*****************************************************************************/
int issort(void *data, int size, int esize, int (*compare)(const void *key1,
const void *key2)) {
char *a = data;
void *key;
int i,
j;
/*****************************************************************************
* *
* Allocate storage for the key element. *
* *
*****************************************************************************/
if ((key = (char *)malloc(esize)) == NULL)
return -1;
/*****************************************************************************
* *
* Repeatedly insert a key element among the sorted elements. *
* *
*****************************************************************************/
for (j = 1; j < size; j++) {
memcpy(key, &a[j * esize], esize);
i = j - 1;
/**************************************************************************
* *
* Determine the position at which to insert the key element. *
* *
**************************************************************************/
while (i >= 0 && compare(&a[i * esize], key) > 0) {
memcpy(&a[(i + 1) * esize], &a[i * esize], esize);
i--;
}
memcpy(&a[(i + 1) * esize], key, esize);
}
/*****************************************************************************
* *
* Free the storage allocated for sorting. *
* *
*****************************************************************************/
free(key);
return 0;
}