C++ :: Void Pointer As A Parameter

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


ADVERTISEMENT

C++ :: How To Pass A Void Function As Parameter

Oct 20, 2013

How can I pass a function as a parameter? I have a class that I'm trying to reuse and one of the methods in this class need to take three parameters, two ints and a function. In other words I want to be able to call a custom function every time this method is invoked when used in other classes. The function I want to call will not return any values, its a void function.

In my Class:

void Utility::someFunction(int var1, int var2, void customFunction) {
int num1 = var1;
int num2 = var2;

[Code] .....

View 9 Replies View Related

C++ :: How To Convert Void Pointer To Int / Double / String Pointer

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

C++ :: Linked List Node Passed As Parameter / Argument Pointer To Pointer Notation

Jan 17, 2014

I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this

bool deleteNode(struct node *head, struct node *delptr)

I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.

Would also like to know why the argument call needed &head instead of just head.

remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);

#include "stdafx.h"
#include<iostream>
struct node{

[Code].....

View 1 Replies View Related

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 :: Printing From Void Pointer

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

C/C++ :: How To Use Void Pointer Functions

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

C++ :: Void Pointer Argument In A Function?

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

C++ :: Reference Pointer - Void Exchange

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

C :: Sorted List - Why Value Of Void Pointer Change

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

C++ :: Return Struct Pointer From A Void Function And Print

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

C++ :: Cannot Cast From Void Pointer - Returns Always Error C2440

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

C++ :: Add Variable Address To Void Pointer Inside Of Class?

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

C++ :: Pointer As Parameter Is Null?

Apr 9, 2014

In my raytracer-project I have the following function:

bool hitObjects(Ray &ray, std::vector<Object *> &objects, Vector3& normal, Material *mat) {
double tmin = K_HUGE_VALUE;
double t;

[Code]....

When I try to run this, I only get the message "Material pointer is null". So, somehow the pointer is null after the hitObjects-function is called, even though it isn't inside that function.

View 6 Replies View Related

C++ ::  Why Every First Function Of Each File Get Error - Multiple Definition Of Void Pointer

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

C++ :: Function Pointer With Object As Parameter?

May 30, 2013

I'm making a code that uses a Function pointer. The problem is, when I try to compile appears an error like:

error: no matching function for call to 'rnVector::rnVector()'

Here's part of the code:

phiFunction::phiFunction(double (*f)(rnVector), rnVector (*df)(rnVector)) {
//... Here comes the code stuff...
}

View 12 Replies View Related

C++ :: How To Use Function With Pointer Parameter To Print Array

Jul 20, 2013

The printArray function should take in the dynamically created array and the size of the array as parameters. It should print out the contents of the array.

#include <iostream>
#include <string>
using namespace std;

[Code].....

My problem is that how to write the code to print the array using pointers. I've been stuck for awhile trying to figure it out.

View 2 Replies View Related

C++ :: How To Pass Function Pointer As A Template Parameter

Jun 13, 2013

void extf(int a) { }
template<typename P>
struct A {
// 1
template< void (*F)(P) >
static void call(P arg) {

[Code]...

Why it is not working? What would be a proper way to pass function pointer as a template parameter?

View 6 Replies View Related

C++ :: Class Constructor With String Const Pointer As Parameter

Feb 19, 2013

I know my product.cpp constructor is wrong but how can I set my private variables.

//PRODUCT.h
#ifndef PROJECT1_PRODUCT_H
#define PROJECT1_PRODUCT_H

[Code].....

View 2 Replies View Related

C++ :: Template Function Parameter Passing By Reference Instead Of Copy / Pointer

Sep 19, 2014

Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.

You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.

#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
Obj(string name): name(name) {cout << "create " << this << endl;}

[code]....

In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.

View 3 Replies View Related

C++ :: Function Parameter Scope - NumArray Not Recognized As Valid Parameter

Sep 28, 2014

My errors are at the end of the program in two function calls within the definition of the InsertByValue function. g++ does not seem to recognize NumArray as a valid parameter.

#include <iostream>
#include <assert.h>
using namespace std;
const int CAPACITY = 20;

/* Displays the content of an int array, both the array and the size of array will be passed as parameters to the function
@param array: gives the array to be displayed
@param array_size: gives the number of elements in the array */
void DisplayArray (int array[], int array_size);

[Code] ....

View 1 Replies View Related

C++ :: Cannot Convert From Int And Missing Default Parameter For Parameter 1

Dec 4, 2013

I am developing new project in Qt with existing MFC project . SO in MFC I have a function which uses SYSTEMTime and return CString.

example

CString getTimestampString( void )
{
SYSTEMTIME systemTime;
CString datestr;

[Code]....

PS -> I cant able to make any changes in lib_know as this library is being used by many other projects..

View 1 Replies View Related

C++ :: Setting Default Parameter Based On Another Parameter?

Jul 2, 2013

Here's my function definition

bool validateNumber(string& text, int min = 0, int max = -1, bool useMin = true,
bool getValid = true)

The code takes the string text, and checks the make sure that the input is valid and safe to convert and use as a number. However, sometimes there is not min, and sometimes there is no max. The lack of min is done by using the parameter useMin, while the lack of max is done by max < min.

My predicament is the following call:
validateNumber(text, -2);

Now, max will be used, even though I don't want it. Ideally, I would want to do something like... int max = (min - 1), ... but that doesn't work. I also can't check to see if the parameter hasn't been changed (that I know of), because the following call would make it look like it hasn't
validateNumber(text, -2, -1);

So the question is, is there a way to do what I want, without having to add in a bool useMax parameter? Or is this my only option? I don't want to do that for simplicity, but if I have to, I have to.

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







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