使用以下代码片段:
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的初学者,并试图实现我自己的链表实现,基本上看起来像这样:
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中实现.现在出现问题,当我想使用该实现时,例如使用结构作为值: …
我有一个程序,我无法理解它是如何工作的.这是它的一部分.我不明白线路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) 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中获得这个缓冲区的大小.第二行将完成这项工作.
我试图通过引用将变量传递给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中的另一个函数.
标头文件:
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描述的问题,这似乎修复了截断问题.谢谢你的帮助!
我想做一个交换函数,可以通用地用于任何数据类型。我知道以下函数适用于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代码:
#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) 是否有可能void*保留类型信息?
我试图使它忘记了真正的类型(B*)通过投B* b来void* 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*(一个错误的地址)的地址,但似乎知道这v是B*正确的.
它为什么有效?难道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
void-pointers ×10
c ×7
pointers ×4
c++ ×3
void ×2
c++14 ×1
casting ×1
function ×1
linked-list ×1
return ×1
sizeof ×1
static-cast ×1
struct ×1
swap ×1
typedef ×1
visual-c++ ×1