[英]Transform function to generic void pointer as function parameter
我必須將接收 int 數組、將值與常量進行比較並返回值與 const 不同的數組的函數轉換為可以接收任何類型數組的通用函數。 為此,我使用了 void 指針,但是在獲取正確輸出時遇到了一些問題,並且數組的重新分配也存在一些問題。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ALG 0
int f2(int * e) {
return *e == ALG;
}
int * f1(int a[], size_t *size, int (*fin)(int * e)) {
size_t i = *size;
while(i--) {
if(fin(&a[i])) {
memmove(&a[i], &a[i+1], (--*size -i)*sizeof(int));
}
}
return realloc(a, *size * sizeof (int));
}
//generic type function
int * f1a(void *a, size_t *size, int (*fin)(int * e)) {
size_t i = *size;
while(i--) {
int * x = (int*) a + i;
if(fin(x)) {
memmove(a + i, a + i + 1, (--*size -i)*sizeof(*a));
}
}
return realloc(a, *size * sizeof (*a));
}
int main(void) {
int *a = malloc(8*sizeof(int));
a[0] = 2; a[1] = -3; a[2] = 1; a[3] = 0; a[4] = 4; a[5] = 7; a[6] = 0; a[7] = 6;
size_t i, size=8;
printf("%zu\n", size);
//int *res = f1(a, &size, f2); output : 2 -3 1 4 7 6
int *res = f1a(a, &size, f2);
for(i=0; i<size; i++) {
printf("%d ", res[i]);
}
return 0;
}
有人可以幫我嗎?
提前致謝!
編寫可讀的代碼。 一次只做一件事。 命名變量有意義。 將上下文傳遞給用戶回調函數。 從qsort
和qsort_r
獲取靈感。 free
內存。 處理錯誤。
翻譯 1:1 您的函數,並帶有一個額外的size
參數,這意味着一個數組元素的大小。
void *filter_generic(
void *base, size_t nmemb, size_t size,
size_t *out_nmemb,
int (*fin)(void* e, size_t size, void *cookie), void *cookie) {
for (size_t i = nmemb; i--; ) {
void *elempnt = (char*)base + (size * i);
if (fin(elempnt, size, cookie)) {
// the following is invalid and
// results in out-of-bounds access
memmove(
(char*)base + (size * i),
(char*)base + (size * (i + 1)),
size * (nmemb - i)
);
nmemb -= 1;
}
}
*out_nmemb = nmemb;
return realloc(base, size * );
}
在這種情況下,使用兩個指針或兩個索引就簡單多了。
#include <stdlib.h>
#include <string.h>
int intpnt_compare_to_zero(void *elem, size_t size, void *cookie) {
return *(int *)elem == 0;
}
/**
* Filters array elements depending on function.
* @param base like for qsort
* @param nmemb like for qsort
* @param size like for qsort
* @param out_nmemb return new size of the array
* @param compare comparison function
* @param cookie cookie for compare
* @return new allocate memory for array
*/
void *filter_generic(void *base, size_t nmemb, size_t size, size_t *out_nmemb,
int (*compare)(void *e, size_t size, void *cookie), void *cookie) {
void *out = base;
void *in = base;
void *const end = (char *)base + (size * nmemb);
for (; in < end; in = (char *)in + size) {
if (compare(in, size, cookie)) {
memmove(out, in, size);
out = (char *)out + size;
}
}
const size_t new_nmemb = ((char *)out - (char *)base) / size;
*out_nmemb = new_nmemb;
return realloc(base, size * new_nmemb);
}
int main(void) {
size_t size = 8;
int *a = malloc(sizeof(*a) * size);
a[0] = 2;
a[1] = -3;
a[2] = 1;
a[3] = 0;
a[4] = 4;
a[5] = 7;
a[6] = 0;
a[7] = 6;
int *res = filter_generic(a, size, sizeof(*a), &size, intpnt_compare_to_zero, NULL);
for (size_t i = 0; i < size; ++i) {
printf("%d%s", res[i], i + 1 == size ? "\n" : " ");
}
free(res);
}
代碼打印0 0
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.