C :: Dereferencing Of Void Pointer
Apr 14, 2014
I could understand void pointers I created the following program:
Code:
#include <stdio.h>
#include <string.h>
int main(void) {
char word[] = "Zero";
int number = 0;
void *ptr = NULL;
[Code] .....
The program works fine, however i really want to fully understand what is going on with the dereferencing of the void pointer, for example: With the following code:
Code:
ptr = &number;
*((int *)ptr) = 1;
Why can't you just do:
Code:
ptr = &number;
*(int *)ptr = 1;
And again with this code, (i'm guessing it's becuase its a pointer to a pointer?):
Code:
ptr = &word;
strcpy(ptr,"One");
View 2 Replies
ADVERTISEMENT
Jan 11, 2014
I want to know how to dereference a void pointer through the way of typing it.
Lets just say that I malloc'd a huge bunch of memory and i can do whatever i want
void* randomData = malloc ( 1000000 );
And i decide to make my own virtual 'int'
I am not sure how to do this.
*( int* ) ( randomData + 10 ) = ( int ) 323453 //323453 can be an int variable aswell
Im not sure if this is the right way to do perform a dereference.
This is an overview of what has to be done:
-The pointer has to be dereferenced
-Cast the pointer as an int pointer so we can change it like a normal 4-byte int
-Perform pointer arithmetic, so that the int can be placed anywhere we want
View 8 Replies
View Related
Jan 15, 2013
I have a set of functions at work which are incredibly useful, however it only supports labels that come from a specific database because that database contains information on the type. I'd like to re-create it to make it more applicable to any member/static/global variables but don't know how to store the type.
I've already re-written it to an extent, but it only accepts int types. I'd like to template it to accept any type. The trick is storing that type so that the pointer can be dereferenced at a later time which I don't know how to do.
Interface:
typedef int T; // The goal is to remove this line!
namespace TimerDelay {
void SetAfterDelay ( T* lpLabelAddress, float delay, T target = T(1)); // Queues the set
void ManageDelays ( float dt ); // sets the labels when appropriate
}
Source:
#include <vector>
namespace TimerDelay{
struct DelayObject {
void* address; // I will probably need to add a container
void* target; // to hold the type, but how can this be done?
[code]....
Edit:Is it possible to store a std::iterator_traits<> struct as a member of my structure? The g_list isn't templated as it needs to accept all types at the same time. That means that DelayObject cannot be templated. I think that means that I cannot use a templated member class as the size may be inconsistant.
View 2 Replies
View Related
Feb 27, 2014
keep getting "deferencing pointer to incomplete type" on the bold lines:
main:
int main(int argc, char *argv[]) {
printf("Please think of an animal. I will try to find out what it is by asking you some yes/no questions.");
struct treenode *root = mkTreeNode("Is it a reptile?
", NULL, NULL);
struct treenode *selectedNode = root;
root->left = mkTreeNode("Does it have legs?
[code]....
View 14 Replies
View Related
Feb 21, 2015
I've been writing the math functions for a 3d game and tried compiling it at about 30 functions in. I get this error related to my pointers to my structures. it affects almost everything in all my functions (as youll see by looking at how i do the math in the function below). The compiler gives me the error
"error: dereferencing pointer to incomplete type"
on all my struct Type4D pointers but referencing the values in my struct TypeMatrix4X4 using pointers seems to work fine i think (it doesn't seem to complian explicitly about it. so here is the important code...
one example function
Code:
struct Type4D *MatVecMult4X4RtoL(struct TypeMatrix4X4 *mat, struct Type4D *vec) {
struct Type4D *dest = (struct Type4D *) malloc(sizeof(struct Type4D));
[Code]....
View 2 Replies
View Related
Oct 2, 2013
Let's assume "person" is a class that has a member "age", and personptr is a pointer to a person object.
doStuff(personptr->age);
doMoreStuff(personptr->age);
andSomethingElse(personptr->age);
andSomethingElse(personptr->age);
Is this bad for performance? Is the following better or doesn't it matter?
int person_age = personptr->age;
doStuff(person_age);
doMoreStuff(person_age);
andSomethingElse(person_age);
andSomethingElse(person_age);
View 2 Replies
View Related
Jul 10, 2013
I am having trouble with this program I get the error dereferencing pointer to incomplete type in the populate function I am using BloodShed's Dev C++ compiler v4.9.9.2 I copied this program out of a book because I was having a problem with a linked list in a similar program. I think there is a problem with the compiler not supporting these types of pointer's in a function.
#include <stdio.h>
struct tel_typ {
char name[25];
char phone_no[15];
struct tel_typ *nextaddr;
[code].....
View 3 Replies
View Related
Sep 13, 2014
I am getting this error:
drivers/media/video/mxc/capture/gt2005.c:2256:62: error: dereferencing pointer to incomplete type
I am at a loss trying to figure this out. Here it is:
case Sensor_Flash:
{
struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
struct sensor *sensor = to_sensor(client);
if (sensor->sensor_io_request && sensor->sensor_io_request->sensor_ioctrl) {
sensor->sensor_io_request->sensor_ioctrl(icd->pdev,Cam_Flash, on);
if(on){
Lines 6 and 7 are giving me the same error. What am I doing wrong?
View 14 Replies
View Related
Mar 7, 2013
I have a function:
const void insertStuff(const void *key, const int value){
// I want to convert the void pointer into one
// of three types of pointers(int, string, or double)
switch(value){
case 0:
int *intPtr = key;
[Code] .....
But this causes an error of: "crosses initialization of int*intPtr"
What's the correct way of implementing this?
View 1 Replies
View Related
May 26, 2014
Code:
int main() {
List* newList= lst_new();
names* nama;
char* data;
int x=1;
[Code] ....
I cant seem to be able to print a string.. the functions lst_next() lst_first() return void*.
View 9 Replies
View Related
Mar 14, 2013
I want to have a function that has a pointer to an int/double/string so I thought I'd use a void pointer like so:
int someFnc(int a, int b, const void *key){
// take care of converting key into appropriate type in here
}
And when I want to use this function I'd like to be able to do something like this:
main{
...
int myKey;
someFnc(1,2,myKey);
]
But I get a compiler error telling me:
invalid conversion from 'int' to 'const void' -[fpermissive]
Do I need to convert myKey into a void pointer before passing it as an argument?
Why does passing myKey like this work?
someFnc(1,2,&myKey);
View 1 Replies
View Related
Sep 27, 2014
int (*cInts)(void*,void*);
cInts= &compareInts;
int x=(cInts)(2,5); //This wont work. I tried a bunch of other things
printf(x);
View 5 Replies
View Related
Jun 11, 2014
Why does the following code compile and execute without any error? I mean, the function compareid should get 2 arguments so why does the compiler not complaining, is it because of the type of arguments?
Code:
#include <stdio.h>
int compareid(void* info, int value); // ansi declaration
int compareid(void* info, int value)
[Code] .....
View 5 Replies
View Related
Mar 15, 2012
#include "vehicle.h"
...
void exchange(vehicle *&v1, vehicle *&v2) {
vehicle *tmp = v2;
v2=v1;
v1=tmp;
}
Is it right?
How about: void exchange(vehicle *v1, vehicle *v2){...}
What is the difference between *&v1 and *v1 ?
View 4 Replies
View Related
Feb 16, 2014
I have a the following in a header file.
Code:
struct SortedList{
void * data;
struct SortedList * next;
struct SortedList * previous;
int (*compareFunc)(void *, void *);
void (*destructor)(void *);
[Code] ....
In a .c file, I implemented the SLCreate function.
Code:
SortedListPtr SLCreate(CompareFuncT cf, DestructFuncT df){
struct SortedList item;
item.data = NULL;
item.next = (struct SortedList *) malloc(sizeof(struct SortedList));
[Code] ....
In main.c, I try to do the following:
Code:
SortedListPtr list = SLCreate(&compareInts, &destroy);
A bunch other code that does not alter list or it's contents at all.
struct SortedList item = (*list);
void * data = item.data;
if (data != NULL) {
printf(Why did data become not null???
"); }
How come my variable data became not null anymore when I haven't altered it at all....
View 2 Replies
View Related
Mar 17, 2013
i need to return a struct pointer dynamically allocated inside a function call void function() which is done using 'out parameters' in following code
struct my_struct {
int x;
} void my_function( my_struct** result ) {
my_struct* x = new my_struct{ 10 };
//...
*result = x;
}
Now i have a doubt, so if i want to print the return value from struct pointer, should i need to print it in the void function() or in the caller the function...
View 3 Replies
View Related
Apr 25, 2013
I having a problem which I'm not able to resovle. I try to dereference a void pointer but I always get a C2440 error. It says: 'static_cast':void* cannot be converted in wqueue<T>. I tried different cast ways but I always get the same error. As far as I found out I should get the error if I try to dereference without cast but in my case I cast before and still get that error.
void *srumbler (void *arg) {
wqueue<workclas*> m_queue= static_cast<wqueue<workclass*>>(arg);
return NULL;
}
The according type wqueue in the header file:
template <typename T> class wqueue {
list<T> m_queue;
pthread_mutex_t m_mutex;
pthread_cond_t m_condv;
[Code] .....
View 3 Replies
View Related
Dec 1, 2013
How can I add the variable adress to a void pointer inside of a class?
class variant2 {
private:
void *Vvariant=NULL;
public:
template<typename b>
variant & operator = (b *adress)
[Code] ....
if possible i want avoid the '&' when i assign the variable address.(variant2 f=varname;//like you see i don't use the '&')
for the moment i just need put the address to Variant pointer. but i receive several errors .
View 4 Replies
View Related
May 6, 2014
I declared all functions in header file, such as:
bool readCase();
bool meshing();
bool readMesh();
bool calculateFlowfield();
bool readFlowfield();
bool calculateEvaporation();
And then I define them in separated .cpp files, each .cpp file include the header, but I got multiple definition error, why?
Even the int main() function, which only decalred and defined once got this error, why?
View 14 Replies
View Related
Nov 5, 2012
why doesnt the following program work as expected:
Code:
char x = 0xff;
char* y = &x;
if(*y == 0xff)
{
return 1;
}
return 0;
imo, it should return "1", but it doesnt. It seems like instead of comparing 0xff == 0xff, the compiler compares 0xffffffff == 0xff. Why?
If i use "byte" for this example, everything works as expected, even though it`s just defined as an "unsigned char".
Code:
typedef unsigned charbyte;
View 4 Replies
View Related
May 7, 2015
This is the main header with three classes, in summary ADNodeInstance is a data holder, ADNode is encapsulating a pointer to ADNodeInstance, and ADGraphBuilder is a main class which holds all the ADNodeInstances and manages them.:
Code:
#ifndef ADVIBE_STACK_H
#define ADVIBE_STACK_H
#include "memory"
#include "iostream"
#include "fstream"
#include "Eigen/Dense"
typedef Eigen::MatrixXd Matrix;
[Code] ....
From this all I could infer is that in the funcreateGradientMessage on the switch for TANH the segfault occurs for the expression: directGradient * child * (1 - child). From the output I can see that this is what happens in order:
Unary negation on node 5 resulting in 15Addition of node 16 and 15 (e.g. the brackets) resulting in 17trying to multiply 14 and 5 - SEGFAULT something wrong with 5
So my question is what exactly is happening? I tried to understand but can't.
View 2 Replies
View Related
Feb 8, 2013
The error is coming up in line 13(srand() issue I think) of the following code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main(int argc, char *argv[]){
[Code] ....
I'm sure it's something really simple that I'm overlooking.
View 2 Replies
View Related
May 4, 2014
Using a template in the assignment, I don't know what I did wrong?
Code:
#include <iostream>
#include <string>
using namespace std;
template<class T>
void mpgcalc(T& Miles, T& Gallons)
[Code] .....
View 2 Replies
View Related
Jul 16, 2014
I have a void that needs to end a program but a break and return 0 both won't work. Instead I have it cout (1/0). It works but is there an alternative?
#include <iostream>
#include <thread>
#include <Windows.h>
#include <limits>
using namespace std;
double clicks=0;
double result;
bool gameon=false;
[Code] .....
View 3 Replies
View Related
Nov 25, 2013
how can i use the void pointers? i understand that can recive an adress variable(&). but can recive a value?
Code:
int a=5;
void *d;
b=&a;
b=100;//???
why i can't do these?
View 14 Replies
View Related
Apr 22, 2012
Why is it not okay to return void? Most compilers will probably let you (gcc does) but it gives you a warning that you aren't supposed to. Most languages allow you to return void.
Something like
Code:
void log(const std::string & txt){ std::cout << txt << std::endl; }
//C++ way to do it
void bar(int i){
[Code].....
View 10 Replies
View Related