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.
1,284
questions
0
votes
3
answers
66
views
How can I pass void pointer to function in C?
I have got sort function. I am passing arguments to this function like this for example:
double tab[] = {-46, -81, 89, -13, -24, -65, 78, -32, -92, -43, 58, -48, 87, 31, 81};
int res = sort(tab, 15, ...
-4
votes
0
answers
7
views
AVL tree using generic linked list Using Void pointer Inputs are taken from user And it can be string and integer both [closed]
AVL tree using generic linked list
Using Void pointer
Inputs are taken from user
And it can be string and integer both
0
votes
0
answers
19
views
std::shared_ptr<void> deleting when storing std::shared_ptr<unsigned int> [duplicate]
I have some code that I have written where it will likely create a dynamic amount of pointers of various datatypes to be stored in an std::unordered_map. I would like to use shared_ptr<void> so ...
0
votes
0
answers
11
views
Lossy integer to pointer (bad cast)
1041 #pragma GCC diagnostic push
1042 #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
1043 ev.m_arg=(void*)lwm2m_gettime();
I get an error on line 1043 of Lossy integer to ...
-1
votes
0
answers
40
views
Adding nodes to list
typedef struct NODE
{
struct NODE *right ,*left;
void *info;
}NODE;
I need to make list where are strings sorted in alphabetical order, with generic pointer to string that confuses me. i m ...
0
votes
1
answer
44
views
find elements of an array in another array, why do I have this error?
I was trying to visualize the algorithm of this exercise, but I'm having a lot of problems.
the exercise asks to implement this function:
extern const void *memmem(const void *haystack, size_t hsize, ...
0
votes
1
answer
43
views
Stack abstract data type C
I'm trying to build and abstract lib for a stack in c, but i'm having some trouble defining the push function because of the variable void *data. How can i solve the push problem?
Is there a better ...
0
votes
0
answers
44
views
Converting void* to std::vector<std::uint8_t>
I have the API with the prototype:
mani(void* data, std::size_t s, std::size_t& ds);
This function reads the server response into data, s is the size of the buffer fixed to 100, and ds is the ...
0
votes
1
answer
90
views
In plain C, could we implement generic functions using pointers to char instead of pointers to void?
In plain C, pointers to void are useful as arguments to generic functions, such as a generic quicksort, or a generic swap, etc., as seen here:
Implementation of generic quicksort, etc.
However, as the ...
0
votes
0
answers
30
views
Printing garbage when I convert char* to void* and back in a struct
I'm new to C and am attempting to accept a string from stdin and pass it as a void pointer to a struct (this is a requirement for the program) and then covert it back to a string again in order to ...
2
votes
3
answers
78
views
Why does memchr() take a void pointer as input?
I'm currently reprogramming some C libraries. So my question is: Why does memchr take an const void *s pointer and not simply an character pointer?
I see that you could probably search for other stuff,...
0
votes
1
answer
80
views
Convert void pointer to a dynamic type
I am trying to convert a void pointer to a dynamic type.
For example, I passed double variable to test1 function which I expected the sum function will work. But I got C2664 cannot convert argument ...
0
votes
2
answers
69
views
Is an external void pointer to a local variable safe?
I've one struct declared as above
struct foo {
void * elem;
};
I want to manage the free() of elem in this struct, but I don't know which is the type of the elem, so I'm making a void * to manage ...
0
votes
1
answer
57
views
casting a two parameter function to a pointer to a single parameter function
I have a function that takes two parameters: "var" is the variable that one wishes to modify and "value" is the value for the desired variable.
void fun(int var, int value){
...
58
votes
10
answers
5k
views
Do all pointers have the same size in C++?
Recently, I came across the following statement:
It's quite common for all pointers to have the same size, but it's technically possible for pointer types to have different sizes.
But then I came ...
4
votes
5
answers
207
views
Why can't we use a void* to operate on the object it addresses
I am learning C++ using C++ Primer 5th edition. In particular, i read about void*. There it is written that:
We cannot use a void* to operate on the object it addresses—we don’t know that object’s ...
0
votes
2
answers
100
views
Is it possible to set bits of a void* value without type casting it to an int or char?
I am working on a project that requires me to allocate some space using the mmap function and I need to create a bitmap in the beginning of the allocated space. The bitmap shows which sections are ...
2
votes
3
answers
131
views
How to properly solve memset() function MISRA errors in C?
I have written a simple function to initialise the structure values using memset(). These are the code I have written in C language.
myfile.h
typedef struct{
bool flag;
bool check;
int val_1;
int ...
0
votes
2
answers
105
views
invalid conversion from 'void (*)()' to 'void*' (C++)
I was learning about generic pointer void*, that allows you to store addresses of multiple data types. However, when trying to store the address of a function, I need to cast it, eg:
ptr = (void*)...
0
votes
1
answer
65
views
Why do void pointers behave differently in this scenario when passed to functions other than main()?
This works :
int main(){
char *pc[2]={"Hello","Welcome"};
void *p=pc;
char **pt=p;
printf("%s\n",*pt);//Output : Hello
return 0;
}
However this doesn't work :
...
0
votes
1
answer
62
views
Is this a correct way to store different types in the same allocation?
I need to allocate a chunk of memory using malloc and then store multiple values of different plain old data types in there. Is the following a correct way to do it?
#include <cstddef>
#include &...
1
vote
1
answer
88
views
Cast a variable to void pointer in Julia
In C/C++ we are able to do this:
double my_var = 4.32;
void* my_var_ptr = &my_var;
which results in the my_var_ptr being a void pointer pointing to the memory which stores the value 4.32.
I am ...
0
votes
2
answers
65
views
C Void Array Traversal
I have a question about coding styles when using void pointers.
This code is modified from code geeks
#include<stdio.h>
int main()
{
int a[2] = {1, 2};
void *ptr = &a;
for(int i=...
0
votes
3
answers
68
views
In plain C, how to assign a void pointer value to a double
void foo(void* p) {
double d = (double) p; // Doesn’t compile.
printf(“%f\n”, d);
}
What I need is to be able to call
foo((void*) 13.0);
and have 13.0 printed.
Note
I need this in order to ...
0
votes
2
answers
49
views
*(int *)foo VS (int *)*foo. Whats the difference between these two?
I am working on RTOS project and I'm trying to pass struct type typecasted to void pointer to thread function and derefernce that void pointer using typecast to same struct type. I was getting error ...
1
vote
1
answer
88
views
How can the alignment requirement be satisfied?
I think that I am misreading the standard quotation, hence I do not fully understand what's the exact intent of the wording.
Firstly, I am already aware of what alignment requirement is, but I can't ...
0
votes
1
answer
76
views
Why is there no function pointer conversion specifier to the printf() family functions?
Printing a function pointer can be done in a portable way in a Kernel but I am wondering why the userspace printf() family functions never got the functionality?Is the reason because it can't be done ...
1
vote
1
answer
35
views
Linked list traversal after reversing the linked list
I've been trying to run this linkedListTraversal() function after reversing the linked list using the function reverseLinkedList(). I know that I'm applying the correct logic in the reverseLinkedList()...
0
votes
2
answers
111
views
Void function required to return value?
I wrote a function as follows which is used to create pthread:
void *downloadfile(void *arg)
{
char *req_path = NULL;
char local_path[PATH_BUFFER_SIZE];
int returncode = 0;
gfcrequest_t *gfr = ...
0
votes
1
answer
95
views
How to print char buffer on FreeRtos returned by function xQueueReceive
I am using Free RTOS for a project.
I have a higher priority task appending char buffers to a Free RTOS queue and a smaller priority task popping the char buffers from the queue and printing them.
...
1
vote
2
answers
97
views
Ternary Operator (?:) based assignment avoids type check in C
I am not able to understand why the compiler doesn't warn when assigning incompatible pointers through (?:) but does warn on direct assignment.
Compiler warns in this case:
test.c: In function ‘main’:...
0
votes
1
answer
75
views
When I write my own strchr function, there is a part in the return step that I don't understand
I'm trying to write my own strchr function for school, but there is something I don't understand.
char *ft_strchr(const char *s, int c)
{
size_t i;
size_t len;
i = -1;
len = ...
-4
votes
2
answers
89
views
Why can void pointers be subtracted but not added?
Why does
printf("%ld\n", (void *)0 - (void *)0);
compile, but
printf("%ld\n", (void *)0 + (void *)0);
does not?
2
votes
1
answer
87
views
Why can `qsort` be called with a compare function with the wrong signature and compile has no warnings
I was working on consolidating a code base (moving a qsort compar function to a new header /library so that it could be shared without being copy/pasta) and noticed something strange in the process.
...
0
votes
2
answers
72
views
Flexibility of functions in C
Suppose that I had a function in C that accepted as input two void pointers, *a and *b say, as well as a function (as pointer) that itself accepted two void pointers. For example, the function might ...
1
vote
1
answer
36
views
getting values of void pointer while only knowing the size of each element
ill start by saying ive seen a bunch of posts with similar titles but non focus on my question
ive been tasked to make a function that receives a void* arr, unsigned int sizeofArray and unsigned int ...
0
votes
1
answer
52
views
C++11 : Cast std::tuple type to void* and back
In summary, I would like to know how to properly store a std::tuple type as void* and later convert it back into the matching std::tuple type.
Problem, my program is currently crashing on the ...
0
votes
2
answers
53
views
C Generic Linked List - Compare void pointer
I'm writing a LinkedList cause an exercise and I'm going much further cause I'm lovin'it. However, I'm stuck in this:
I'ld remove an element from list by value, like C#.
I've this code:
int ...
0
votes
0
answers
60
views
Type Erasure with void pointer stored in vector of unique_ptr issues when copying
I've been banging my head on a crash that has to do with copy assignment of an object that holds a vector of unique_ptr that point to a class that hides std::vector data behind a void *. I have taken ...
0
votes
0
answers
34
views
genaral array printer in c for pointers array
I've been asked to write a general printer for arrays, and it works just fine with void* arrays.
But if I'm trying to print a pointers array it crashes (I understand the logic behind the problem but ...
1
vote
0
answers
135
views
Do any of these pointer operation trigger undefined behavior?
The question:
I have some code executing which is very unsafe. My question is, does this code trigger undefined behavior or is it 'correct' (= behavior fully defined).
Some context:
I have a program ...
1
vote
1
answer
46
views
Void Pointer Usage
#include <iostream>
int main()
{
char *p = new char[1024];
std::cin >> p;
char q = *p;
std::cout << "&q = " << (void*)&q << '\...
0
votes
3
answers
96
views
C getting type of a pointer
I want to implement a function that takes either int or a char pointer as the input and stores then as a void pointer but I also want to store the type of the pointer that has been passed so is there ...
2
votes
1
answer
84
views
Calculate range of variable types in C
a hobbyist here so sorry for the nooby question. K&R ex. 2-1. Use header constants and/or direct computation to calculate range of various variable types (char, short, int, long) + floating types. ...
0
votes
1
answer
140
views
Encode/decode strings inside byte type in nanopb with C
I have a .proto file that looks like this:
message ObjValue
{
// ...
optional bytes byteval = 6 [max_size = 256]; // arbitrary structure or message
// ...
I use this proto encoding to send ...
0
votes
3
answers
59
views
Casting generic pointers in c
I found this code in a book, trying to explain how generic pointers works using this to copy one matrix to another one.
#include <stdio.h>
#define FILAS 2
#define COLS 3
void copiarMatrices(...
2
votes
2
answers
57
views
Why do I need to avoid "casting a function pointer to a generic pointer" in this case?
assume that I have a struct that has a function pointer as a member:
struct Class
{
void (*function)();
}
I then want to assign a function to this member. I wanted to do this:
typedef void (*...
0
votes
1
answer
64
views
Handling void pointers in C
I'm making a C code to handle sparse matrixes, and i want to modularize my code at its maximum.
I noticed that there are a couple times when i need to walk-until-i-find-the-previous-node (oh, i am ...
2
votes
5
answers
250
views
C | find the size of the value a void Pointer is pointing at using sizeof()
Im trying to use sizeof() to find the size of the value a void pointer is pointing at, I want to use it to print the value.
this code
int num = 5;
void *voidPtr;
voidPtr = #
...
-1
votes
1
answer
130
views
Correct way to dynamically allocate a const char array in C
I have to read from a file line by line and add those lines into a char array.
I need to keep the array const and also pass it as a const pointer.
#include<stdlib.h>
#include<stdio.h>
#...