标签: void-pointers

如何将 int** 转换为 void**?

使用以下代码片段:

int n = 11;
int* c = &n;
void** v = &c;
Run Code Online (Sandbox Code Playgroud)

我在 Visual Studio 中收到以下错误:

type 的值int**不能用于初始化 type 的实体void **

这工作正常:

int n = 11;
int* c = &n;
void* v = c;
Run Code Online (Sandbox Code Playgroud)

但这个代码片段是针对某人的库中的一个更大的问题。

我将变量转换为 时做错了什么 void**

完整示例

使用caen 数字化仪库尝试从外围设备收集数据的方式具有以下原型:

/******************************************************************************
* X742_DecodeEvent(char *evtPtr, void **Evt)
* Decodes a specified event stored in the acquisition buffer writing data in Evt memory
* Once used the Evt memory MUST be deallocated by …
Run Code Online (Sandbox Code Playgroud)

c++ casting void void-pointers

1
推荐指数
1
解决办法
1239
查看次数

C - 链接列表和指针问题

嘿,
我是C的初学者,并试图实现我自己的链表实现,基本上看起来像这样:

struct Element
{
    void *value;
    struct Element *next;
};

typedef struct
{
    struct Element *first;
    struct Element *last;
    unsigned int size;
} LinkedList;

void LinkedList_init(LinkedList *this)
{
    this->size = 0;
    this->first = NULL;
    this->last = NULL;
}

void LinkedList_add(LinkedList *this, void *value)
{
    struct Element *node = malloc(sizeof(struct Element));
    node->value = value;
    node->next = NULL;

    if (this->size == 0)
        this->first = this->last = node;
    else
    {
        this->last->next = node;
        this->last = node;
    }

    this->size++;
}
Run Code Online (Sandbox Code Playgroud)

简而言之,我想要一个可以保存任意类型的链表 - 我听说,这可以通过使用void指针在C中实现.现在出现问题,当我想使用该实现时,例如使用结构作为值: …

c linked-list void-pointers

0
推荐指数
1
解决办法
1870
查看次数

为什么在这个接口中使用`typedef void*COMPLEX`?

我有一个程序,我无法理解它是如何工作的.这是它的一部分.我不明白线路typedef void *COMPLEX,命令this和使用原因struct COMPLEX_IMPL.

#ifndef _COMPLEX_H 
#define _COMPLEX_H 

typedef void *COMPLEX; 

COMPLEX NewCOMPLEX (double a, double b ); 

void DeleteCOMPLEX(COMPLEX this ); 

double GetA (COMPLEX this ); 

double GetB (COMPLEX this ); 

COMPLEX AddComplex (COMPLEX c1, COMPLEX c2, COMPLEX res); 
COMPLEX MultComplex (COMPLEX c1, COMPLEX c2, COMPLEX res); 

#endif /* _COMPLEX_H */

#ifndef _COMPLEX_H 
#define _COMPLEX_H 

typedef void *COMPLEX; 

COMPLEX NewCOMPLEX (double a, double b ); 

void DeleteCOMPLEX(COMPLEX this ); 

double GetA (COMPLEX this …
Run Code Online (Sandbox Code Playgroud)

c struct typedef void-pointers

0
推荐指数
1
解决办法
7494
查看次数

在C++中指向缓冲区(字符串"")的void指针上使用sizeof()以获得BYTES的大小?

void * ptr0 = (void*)"Buffer_1";
int size0 = sizeof(ptr0);
// WILL THIS RETURN THE NUMBER OF BYTES OCCUPIED BY THE MEMORY POINTED BY ptr0?
Run Code Online (Sandbox Code Playgroud)

我的要求是
1)在缓冲区中存储一些数据并创建一个void*指针,我在第一行做.
2)后来我想在BYTES中获得这个缓冲区的大小.第二行将完成这项工作.

c++ pointers sizeof void-pointers visual-c++

0
推荐指数
1
解决办法
2649
查看次数

使用C中的void指针修改原始文件

我试图通过引用将变量传递给void指针,以更新原始值.当我尝试它时,旧值永远不会更新.任何帮助将不胜感激. gst_filter_chain就像主要功能(Gstreamer)

void update_value(void *oldValue, void *newValue)
{
    oldValue = newValue;
}

void update_struct(myStruct *oldStruct, myStruct newStruct)
{
    update_value((void *)&oldStruct->a, (void *)&newStruct.a)
}

static GstFlowReturn
gst_filter_chain (GstPad * pad, GstBuffer * buf)
{
    GstFilter *filter= GST_FILTER (gst_pad_get_parent (pad));
    myStruct temp_data;

    int buf_size = GST_BUFFER_SIZE(buf);     
    if(buf_size > 1) //if buffer is not empty
    {
        if(!filter->is_init)
        {
            memcpy(&filter->data, GST_BUFFER_DATA(buf), sizeof(myStruct));
            filter->is_init = TRUE;
        }
        else
        {
            memcpy(&temp_data, GST_BUFFER_DATA(buf), sizeof(myStruct));
            update_struct(&filter->data, temp_data);
        }
    }   


    gst_buffer_unref(buf);
    return GST_FLOW_OK;
}
Run Code Online (Sandbox Code Playgroud)

c pointers pass-by-reference void void-pointers

0
推荐指数
1
解决办法
190
查看次数

返回Void指针截断值

我无法将空指针返回到C中的另一个函数.

标头文件:

void *function2( void );
Run Code Online (Sandbox Code Playgroud)

MAIN.C

#include "myHeader.h"

void function1 (){
    void *temp = NULL;
    temp = function2();
}
Run Code Online (Sandbox Code Playgroud)

FUNCTION2.C

int a = 3;

void *function2(void){
    printf("Memory Address %p\n",&a );
    return (void *) &a; // value of &a is 0x100023444
}
Run Code Online (Sandbox Code Playgroud)

但是,function1()中temp的值是0x3444,而不是0x100023444.

有没有人知道这个解决方案,或者我做错了什么?

编辑:似乎,标题被添加到错误的位置,导致下面的AndreyT和Jonathan描述的问题,这似乎修复了截断问题.谢谢你的帮助!

c pointers void-pointers

0
推荐指数
2
解决办法
1308
查看次数

使用空指针交换函数

我想做一个交换函数,可以通用地用于任何数据类型。我知道以下函数适用于int:

void swap(void *a, void *b)
{
    int temp;
    temp = *(int*)a;
    *(int*)a = *(int*)b;
    *(int*)b = temp;
}
Run Code Online (Sandbox Code Playgroud)

这适用于字符串:

void swap(void *a, void *b)
{
    void *temp;
    temp = *(void**)a;
    *(void**)a = *(void**)b;
    *(void**)b = temp;
}
Run Code Online (Sandbox Code Playgroud)

c swap void-pointers

0
推荐指数
1
解决办法
7096
查看次数

如何从单个函数返回不同的类型

我有以下c代码:

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

void *func(int a) { 
    if (a==3) {
        int a_int = 5;
        int *ptr_int = &a_int;
        return (void *)ptr_int;
    } 
    else if (a==4) {
        char a_char = 'b';
        char *ptr_char = &a_char;
        return (void *)ptr_char;
    }
    else {
        fprintf(stderr, "return value is NULL");
        return NULL;
    }
}

int main (int argc, char *argv[]) {
    int *ptr_int = (int *)func(3);
    char *ptr_char = (char *)func(4);
    fprintf(stdout, "int value = %d\n", *ptr_int);
    fprintf(stdout, "char value = %c\n", *ptr_char); …
Run Code Online (Sandbox Code Playgroud)

c return function void-pointers

0
推荐指数
2
解决办法
3786
查看次数

void*保留继承信息吗?

是否有可能void*保留类型信息?
我试图使它忘记了真正的类型(B*)通过投B* bvoid* v,但它似乎知道它的起源.
这对我来说是好运,但我不知道为什么.

这是一个简化的代码(完整演示): -

#include <iostream>
#include <memory>
using namespace std;

class B{public: int ib=1;};
class C:public B{public: int ic=2;};

int main() {
    B* b=new C();
    void* v=b;                     //<- now you forget it!
    C* c=static_cast<C*>(v);    
    std::cout<<c->ib<<" "<<c->ic<<std::endl; //print 1 and 2 correct (it should not)
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望c指向B*(一个错误的地址)的地址,但似乎知道这vB*正确的.

它为什么有效?难道void*真的记得类型?
它能"记住"多远?

背景

我试图在这里创建一个void* …

c++ void-pointers static-cast c++14

0
推荐指数
1
解决办法
107
查看次数

C:如何访问存储在void指针中的函数指针(void*)?

混淆了如何访问存储在void指针(void*)中的函数指针.


假设你有这个:

void *functions[] =
{
    &sqrt,         // int ft_sqrt(int nb);
    &power,
    &logN,
    &factorial;
};

// An array of void pointers, each storing a function pointer.
Run Code Online (Sandbox Code Playgroud)

如果我想访问该sqrt功能,我的猜测如下:

(int (*)(int)) functions[0](x)

但我的猜测是错误的:

error: called object type 'void *' is not a function or function pointer


那么如何访问其中一个函数呢?

c pointers function-pointers void-pointers typecasting-operator

0
推荐指数
2
解决办法
243
查看次数