C++ :: Dereferencing Void Pointers Through The Way Of Typing It

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


ADVERTISEMENT

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 View Related

C++ :: Storing Data As Void And Dereferencing Them Later?

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

C++ :: Dereferencing Char Pointers?

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

C++ :: Use The Void Pointers?

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

C :: Memcpy Between Void Pointers

Feb 13, 2014

I am trying to add data to a queue with the following simplified code:

Code:
typedef struct Queue {
void * data;
int head;
int tail;
int elementSize;

My question is, how do I move the queue->data pointer to the correct memory location in order to copy given data to head? The code above inside memcpy gives med the error: "expression must be a pointer to a complete object type".

Do I need an extra pointer to be able to navigate between the queue's head and tail, and keep queue->data as a reference to the first byte of the allocated memory, or is it possible with only queue->data?

Edit. Just noticed I have mixed up head and tail. The enqueued data should probably go to the Queue's tail and not the head. However, the problem is still the same.

View 2 Replies View Related

C :: Program To Use Void Pointers In A Function

May 14, 2014

As part of my ongoing c programming education, I have written a program to use void pointers in a function,

Code:

#include <stdio.h>
#define MAX_NUMBERS 10
size_t size(void *object);
int main(void) {
}

[code]....

Now I think that the result 4 is the size of the pointer, so I'm confussed as why it doesn't give me the same result as 40.

View 2 Replies View Related

C/C++ :: Find Max And Min Value In Void Function Using Pointers

Aug 31, 2014

I'm having issues with pointers and relationship operators in C.

I need to find a max and min value in a void function using pointers. max and min would work if they had values. mul works, because you can just do math operations with pointers.

There are 0 errors and warnings; but max and min are never going to work as is.

Clearly I'm missing something.

#include <stdio.h>
#include <stdlib.h>
void max(int *a, int *b, int *c, int *d, int *result);
void min(int *a, int *b, int *c, int *d, int *result);
void mul(int *a, int *b, int *c, int *d, int *result);
int main()

[Code]...

Your job will be to create a program that uses pointers. Your output must be done in the main function and the calculations MUST be done in the three functions. Therefore you MUST use pointers correctly.

You must declare and implement the following 3 functions. Below are the three prototypes that you must use in this program.

void max(int *a, int *b, int *c, int *d, int *result);
void min(int *a, int *b, int *c, int *d, int *result);
void mul(int *a, int *b, int *c, int *d, int *result);

The functions have the following meaning:

max
finds the max value of a,b,c,d and stores the largest value in result.
min
finds the min value of a,b,c,d and stores the largest value in result.
mul
multiplies a * b * c and divides by d. Stores that value in result.

Below is an example input/output. This input will be read in via the keyboard (use scanf).

input
output (note that user input is shown in bold)
1 2 3 4
Enter the 4 numbers: 1 2 3 4
The max is 4. The min is 1. (a * b * c) / d = 1
100 3 201 103
Enter the 4 numbers: 100 3 201 103
The max is 201. The min is 3. (a * b * c) / d = 585

Your output MUST match exactly the output below for the input from above. Your program must compile, failure to do so will result in 0 points. */

View 9 Replies View Related

C/C++ :: Unable To Find The Distance Between Two Void Pointers

Jul 13, 2014

I am trying to find the distance between two void pointers, so I can follow this distance to a certain pointer in a vector when given only the previous element in that vector.

int distance = (char*) prev - (char*) first;
next = (char*) cv->elems + cv->elemsz + distance;

Basically, prev and first are void pointers. I am trying to cast them into a char, subtract the first element in the vector from the previous one, and then use this distance to determine what the next element in the vector is. However, it is not working. I am not sure how to do this. To complicate matters, prev is a const void *.

View 7 Replies View Related

C++ :: Vector Of Void Pointers Which Point To Array Of Characters

Jan 21, 2014

This code work perfectly, as follows.

Code #A:

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
typedef std::vector <void *> gr_vector_void_star;
gr_vector_void_star output_items;

[Code] .....

Output of above code #A:

char * sentence = "Angel";
for (int i=0; i < 5; i++)
{ out[i] = sentence[i]; } // error: invalid conversion from 'char' to 'char*' [-fpermissive]

It fails to compile with error message "invalid conversion from 'char' to 'char*'".

View 19 Replies View Related

C/C++ :: Why Do Void Function Pointers EXECUTE Arrays Of Hexcode

Dec 23, 2012

I know how to use functions pointers in C and C++ and I know if you have something like

char buf[] = {
0x48, 0xb8, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x48, 0xbf, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0x05
};
((void (*) (void))buf)();

That this will execute those binary instructions in hexadecimal notation BUT WHY? I don't get why that works since that's an array of data not a function?

View 7 Replies View Related

C++ :: Constructing A List To Typing Numbers And Count Them

Dec 17, 2013

i have read a lot of about lists but i dont understand this. I know its something like dogs on leash where we have

dog1->dog2->dog3->....
and
Code:
struct DOG
{char* (name of a dog of first leash)
DOG* (next dog ) } I have written something like this but this doesnt work as i wanted Code: #include <iostream>
using namespace std;
struct line {

[Code]....

I wanted to make program where i can type XX numbers , then cout those numbers without changing the order, and my next exercise is to change order in this programme from end to start.

View 7 Replies View Related

C++ :: Structures / Loops - How Not To Repeat Typing Same Code

Jul 27, 2013

I have a structure for 4 divisions, and can't figure out how to loop through it.

So I ended up writing everything for times.

If I had 500 divisions I wouldn't be able to get away with it!

How can I have the structure in array form so I can fill it via looping?

/*Headers*/
#include <iostream>//needed 4 input/output
#include <iomanip>//needed to round
#include <string>//needed 4 strings
using namespace std;//global namespace to avoid name clash
struct division {

[code].....

View 7 Replies View Related

C :: Number 1 Appears In Next Line Every Time On Typing A Character

May 17, 2014

Everytime I type a character, the number 1 appears in the next line. And i just keep getting the message "Wrong! I have more than that." even when I type a number bigger than 1023

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
srand(time(NULL));

[Code] ....

View 10 Replies View Related

C/C++ :: Dereferencing Pointer To Incomplete Type

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

C :: Error - Dereferencing Pointer To Incomplete Type

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

C++ :: Performance Penalty For Repeatedly Dereferencing A Pointer?

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

C++ :: Error Dereferencing Pointer To Incomplete Type

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

C/C++ :: Error / Dereferencing Pointer To Incomplete Type

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

C++ :: Operator Overloading And Mysterious Object Dereferencing?

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

C :: Create Array Of Pointers To Pointers Which Will Point To Array Of Pointers

Feb 28, 2014

I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried

Code:

int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",

[code]...

the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..

View 4 Replies View Related

C++ :: Comparing Char Pointers To Integer Pointers

May 21, 2013

I am a little confused while comparing char pointers to integer pointers. Here is the problem:

Consider the following statement;
char *ptr = "Hello";
char cArr[] = "Hello";

When I do cout << ptr; it prints Hello, same is the case with the statement
cout << cArr;

As ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e.
int iArr[] = {1, 2, 3};

If I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??

View 2 Replies View Related

C++ :: Void Value Not Ignored As It Ought To Be

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

C++ :: Error - Void Value Not Ignored As It Ought To Be

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

C++ :: Have To End A Program In A Void

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

C++ :: Why Can't Void Be Returned

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







Copyrights 2005-15 www.BigResource.com, All rights reserved