Questions tagged [void-pointers]

A void pointer (void *) in C and C++ is a pointer that points to a memory location with no specified type.

A void pointer (void*) in and is a pointer that points to a memory location with no specified type. A void * is typically used to exchange pointers, where some called function may not care about the type of the argument that it receives — for example, functions which operate on raw memory (such as realloc and bzero) and functions which pass along the pointer without needing to access it (for example, GTK+ callbacks).

In C, the use of void * is considered idiomatic as long as no undefined operations (such as dereferencing it or performing pointer arithmetic on it) are taken on the pointer, as C lacks type-safe generics. Unless working with C libraries, C++ users should avoid void * and use templates instead, as compilers can detect usages of templates that are not type-safe.

Note that in standard C, you cannot perform arithmetic on a void *; the GNU C Compiler allows such arithmetic as a (non-portable) extension.

1365 questions
-1
votes
2 answers

Cast from void * produces Segmentation Violation error

I'm using the Gnu Scientific Library to implement a module in my program that computes integrals numerically. The functions are based on the example that can be found on the GSL website in Numerical integration examples: and here's my code (most of…
Natalia Zoń
  • 980
  • 2
  • 12
  • 22
-1
votes
3 answers

void* of a bool[] conversion to std::string

I need to save some data and the only viable option is a std::string; so I get a bool array passed as a void*. Now I need to save it in a way that I can convert it into a std::string and be able read a void* to a bool[] from that exact string. Sadly…
dos
  • 135
  • 1
  • 6
-2
votes
1 answer

How to connect my button's void statement with all of the button's actions.

Sorry if this is a noob question, but All my code is error-free and I cannot figure out how to get the button I press to do all these things. It's connected correctly in interface builder. Anyway, if you can help me out, here's what I have: IN THE…
-2
votes
3 answers

Passing a void* pointer and then cast in C

I'm passing a pointer to my function, but the type of this pointer may change. I don't want to write several versions of this function such as func_float() and func_int(). So I'm passing a pointer of void* to my function and then type casting to…
ZR Han
  • 238
  • 1
  • 3
  • 10
-2
votes
2 answers

Unexpected behaviour of a function which returns a void pointer

I wrote a void pointer function to find the square of an integer. void * square(const void * num); int main(){ int x = 6; void * ptr = &x; printf("%d", square(ptr)); } void * square(const void * num){ return (*(int * )num) *…
StaticESC
  • 79
  • 5
-2
votes
1 answer

error problem(the current name doesn't exist in the )

private void btnSave_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=LoginDB;Integrated Security=True"); con.Open(); SqlCommand commamd = new SqlCommand("insert into…
-2
votes
1 answer

pointer equality test with same address yields false

Why is this pointer comparison not working? // in handleSlot()... void* ptr1 = m_expected; // defined as SimpleBase* void* ptr2 = sender(); // defined as QObject* if (ptr1 != ptr2) return; // this should not be reached, since the debugger shows…
Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
-2
votes
2 answers

How to understand pointer behavior for this code snippet

I know this code doesn't make much sense but I just wanted to know how the pointers in this code are working. int main() { int a=2; int *b = &a; void* c = (void*)b; printf("\n%d %d %d %d %d %d",a,&a,*b,b,c,*(int*)(c+1)); …
-2
votes
7 answers

How to generically assign a pointer passed into a function in C

I am new to C and wondering how to do some pointer stuff. Specifically here I am wondering how you can pass a pointer into a function and "get a value out of the function". Sort of like this (semi-pseudocode): assign_value_to_pointer(void* pointer)…
Lance
  • 75,200
  • 93
  • 289
  • 503
-2
votes
3 answers

C - How create an array that points to different datatypes?

I want to create an array that has a double value at it's zeroth index, a pointer that points to a double array, and a pointer that points to an integer array. Is this possible? Or should I use a structure? Something like this: pointer description
sigsegv
  • 159
  • 7
-2
votes
1 answer

C, assignment of struct pointers changes the content of the right side of assignment

I have the following structs: typedef struct{ char *name; int size; void *data; } Struct1; typedef struct st2{ char *name; struct st2 **Struct2array; Struct1 **Struct1array; int Struct1_n; int Struct2_n; }…
S. Bing
  • 11
  • 3
-2
votes
2 answers

Nested references to member functions by member functions of other classes

I am trying to build a generic stochastic differential equation solver coded by a class de_solver which takes some set of differential equations given by a model class. This model is fed to the solver through a class sde which works interface…
maurizio
  • 745
  • 1
  • 7
  • 25
-2
votes
1 answer

How do I find the even numbers on the line into C program

I got an exercise to do I need to find in matrix what line do i have even number All numbers in a row should be double for exemple: in matrix matrix[R][C]={{8,1,2},{3,7,5},{6,2,14},{13,8,15},{8,0,2,},{4,50,26},{2,84,11},{12,36,9}}; Example output:…
B RAMI
  • 19
  • 1
  • 5
-2
votes
4 answers

Obtaining an int from a void pointer which points to a short

I have a return value from a library which is a void pointer. I know that it points to a short int; I try to obtain the int value in the following way (replacing the function call with a simple assignment to a void *): short n = 1; void* s = &n; int…
muhzi
  • 119
  • 2
  • 14
-2
votes
1 answer

can't cast to void *

class MyClass { public: register_callback(int, void*); } typedef boost::shared_ptr myClass_p; class MyOtherClass { public: registerItem(std::pairinsertItem) { auto foo = insertItem.second; …