If I have a void pointer, how do I put an int into it?

前端 未结 5 1816
情歌与酒
情歌与酒 2021-02-04 09:47

I have an array of arbitrary values, so I have defined it as an array of void pointers, so I can point to any kind of information (like int, character arrays, etc).

5条回答
  •  遥遥无期
    2021-02-04 10:22

    Although you can use a cast to make the assignment, it is probably much cleaner to write the code like:

    void *data[ 10 ];
    int x = 100;
    int *p;
    
    p = malloc( sizeof *p );
    data[ 0 ] = p;
    *p = x;
    

提交回复
热议问题