2016-02-27 45 views
-1

所以我有一个数组,无须提出任何具体类型:分配变量无效*到无效的C数组

void* buff = malloc(size*eltSize); 

而且我有一个功能,即有一个void *参数,我想将它分配给数组,像这样:

void function(void* p1){ 
    buff[i] = p1; 
} 

我知道,这是不行的,但说我想让它尽可能地通用,什么是最好的办法吗?请记住,我不知道使用的类型(它应该接受任何可能的类型;即使结构)。 谢谢

+4

声明'buf'为'无效**'。 – a3f

+0

您能否更详细地描述您的计划阵列结构?从阅读你的文章我拿你想要一个连续数组的大小为'eltSize'的数组,但其他读者似乎认为你需要一个指针数组 –

回答

1

你要通过的元件尺寸(和数组索引,就此而言)手动每次,类似于如何qsort作品。你必须给你的函数更改为类似:

void function(void * buff, void * p1, size_t elt_size, size_t index){ 
    memcpy(((char *) buff) + index * elt_size, p1, elt_size); 
} 

,并调用它,如:

int array[] = {3, 1, 4, 1, 5, 9}; 
int n = 8; 
function(array, &n, sizeof(n), 5); // Equivalent to array[5] = n; 

一个完整的工作示例:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

void function(void * buf, void * data, size_t elt_size, size_t index) 
{ 
    memcpy(((char *) buf) + index * elt_size, data, elt_size); 
} 

int main(void) 
{ 
    int narray[] = {3, 1, 4, 1, 5, 9}; 
    int n = 8; 
    function(narray, &n, sizeof(n), 5); // Equivalent to array[5] = n 
    for (size_t i = 0; i < sizeof(narray)/sizeof(narray[0]); ++i) { 
     printf("Value of element [%zu] is: %d\n", i, narray[i]); 
    } 

    char * sarray[] = {"The", "mome", "raths", "outgrabe"}; 
    char * p = "barked"; 
    function(sarray, &p, sizeof(p), 3); // Equivalent to sarray[3] = p 
    for (size_t i = 0; i < sizeof(sarray)/sizeof(sarray[0]); ++i) { 
     printf("Value of element [%zu] is: %s\n", i, sarray[i]); 
    } 

    return 0; 
} 

与输出:

[email protected]:~/Documents/src/sandbox$ ./generic2 
Value of element [0] is: 3 
Value of element [1] is: 1 
Value of element [2] is: 4 
Value of element [3] is: 1 
Value of element [4] is: 5 
Value of element [5] is: 8 
Value of element [0] is: The 
Value of element [1] is: mome 
Value of element [2] is: raths 
Value of element [3] is: barked 
[email protected]:~/Documents/src/sandbox$ 

很明显,它会工作得很好w ith动态分配malloc()的数组,因为它将与本示例使用的常规数组一起使用。

,如果你创建一个struct到的数据与元件尺寸抱在一起,比如,你可以无需在每次将元素的大小:

struct generic_array { 
    void * data; 
    size_t elt_size; 
} 

当你传递一个指向这个struct到你的功能,它将能够访问元素的大小本身,既不需要你提供它,并消除由于你无意中通过错误的大小产生的整个类别的错误。如果添加第三个成员来存储最初malloc() ed的元素数量,那么也可以执行边界检查。这种做法的

全部工作示例:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

struct generic_array { 
    void * data; 
    size_t elt_size; 
    size_t size; 
}; 

struct generic_array * generic_array_create(const size_t elt_size, 
              const size_t size) 
{ 
    struct generic_array * new_array = malloc(sizeof *new_array); 
    if (!new_array) { 
     perror("couldn't allocate memory for array"); 
     exit(EXIT_FAILURE); 
    } 

    void * data = malloc(size * elt_size); 
    if (!data) { 
     perror("couldn't allocate memory for array data"); 
     exit(EXIT_FAILURE); 
    } 

    new_array->data = data; 
    new_array->elt_size = elt_size; 
    new_array->size = size; 

    return new_array; 
} 

void generic_array_destroy(struct generic_array * array) 
{ 
    free(array->data); 
    free(array); 
} 

void generic_array_set(struct generic_array * array, void * elem, 
         const size_t index) 
{ 
    if (index >= array->size) { 
     fprintf(stderr, "Index %zu out of bounds of size %zu.\n", 
       index, array->size); 
     exit(EXIT_FAILURE); 
    } 

    memcpy(((char *)array->data) + index * array->elt_size, 
      elem, array->elt_size); 
} 


void generic_array_get(struct generic_array * array, void * elem, 
         const size_t index) 
{ 
    if (index >= array->size) { 
     fprintf(stderr, "Index %zu out of bounds of size %zu.\n", 
       index, array->size); 
     exit(EXIT_FAILURE); 
    } 

    memcpy(elem, ((char *)array->data) + index * array->elt_size, 
      array->elt_size); 
} 

int main(void) 
{ 
    int narray[] = {3, 1, 4, 1, 5, 9}; 
    const size_t nsize = sizeof(narray)/sizeof(narray[0]); 

    struct generic_array * garray = generic_array_create(sizeof(int), nsize); 

    for (size_t i = 0; i < nsize; ++i) { 
     generic_array_set(garray, &narray[i], i); 
    } 

    for (size_t i = 0; i < nsize; ++i) { 
     int n; 
     generic_array_get(garray, &n, i); 
     printf("Value of element %zu: %d\n", i, n); 
    } 

    generic_array_destroy(garray); 

    return 0; 
} 
+0

它工作。谢谢。 – user52713

0

既然你尝试插入指向void类型元素buff,然后buff必须void**类型。

int i = 0; 
void* *buff = malloc(size * sizeof(void*)); 
if (buff == NULL) 
    // handle error 

void function(void* p1) { 
    buff[i] = p1; // now OK 
} 
+0

@ M.M当然,更正。谢谢(我认为OP希望根据他给我们的示例代码存储通用指针)。 – 4pie0

+0

也许,但这样就不清楚'eltSize'应该代表什么 –

+0

我同意。可能元素的大小。 – 4pie0

0

如果您想记住通用数组中存储的每个数据的相应类型,这是一个潜在的解决方案。我使用了一个固定大小的数组,并在枚举中添加基本类型,以及如何在各自类型中取回数据的两个例子。

如果你有更多的类型并且不想使用大量'if'语句,你可以使用函数指针。

#include <stdio.h> 

enum type { 
    INT, 
    FLOAT, 
    CHAR, 
    STRING 
}; 

struct gen_array { 
    enum type elm_type; 
    void *data; 
}; 

int  to_int(void *data) { 
    return ((int) data); 
} 

char *to_string(void *data) { 
    return ((char *) data); 
} 

void printer(struct gen_array *arr, size_t size) { 
    for (size_t i = 0; i < size; i++) { 

    if (arr[i].elm_type == STRING) 
     printf("%s\n", to_string(arr[i].data)); 

    if (arr[i].elm_type == INT) 
     printf("%d\n", to_int(arr[i].data)); 

    } 
} 

int  main(void) { 
    struct gen_array buff[2]; 

    struct gen_array elm_0; 
    elm_0.elm_type = INT; 
    elm_0.data = (void*)10; 

    buff[0] = elm_0; 

    struct gen_array elm_1; 
    elm_1.elm_type = STRING; 
    elm_1.data = (void*)"helloWorld!"; 

    buff[1] = elm_1; 

    printer(buff, 2); 

    return (0); 
} 
1

如果要复制的对象,你不知道它的类型,只是它的大小,您使用memcpy:

void* buff = malloc(size*eltSize); 


void function(void* p1) { 
    memcpy((char *)buff + i * eltSize, p1, eltSize); 
} 

由于您不知道类型,因此不能直接使用索引,而必须使用指针算术手动计算地址。

+0

我不知道我是如何忘记memcpy(..)的。谢谢您的回答。 – user52713