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
ADVERTISEMENT
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
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 4, 2015
I need to make a copy of a string that is defined by char *full and copy it into a different pointer defined by char *duplicate. I have written code to do this however it will not work and i cannot figure it out my code is as follows:
char *duplicate = (char *)malloc(strlen(full) + 1);
strcpy(duplicate, full); /*Make second version of full*/
char *Ptr = strtok(duplicate, " "); /*Split duplicate up*/
I have a full program written but i know this is where the problem is because i have used printf statements to see where the program fails. I get no errors and it compiles successfully but it hits this point of the program and it just stops and windows automatically shuts down the program.
char *full is pointing to:
"To be, or not to be? That is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,"
I need to duplicate the string because i need to use strtok but i will need the original string later on so i need an unaltered version.
View 9 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
Oct 24, 2013
I'm writing a program involving a theoretical hotel. Most of the code is already written, but the part I'm having trouble with involves the very beginning of the code.
The program is designed to read in from an input file the hotel's ID number, the types of rooms it has, the Room Numbers of each room, the base rate for each room, and the rate of additional charge per person.
The code demonstrates inheritance for my Object-Oriented Programming class, as each type of room will inherit from generic class Room, but I can't seem to figure out how to make the double-pointer for dynamic allocation work.
The code is below.
Hotel.h
Code:
class Hotel{
int hotelID;
static const int MAX_SIZE = 101;
Room **rPoint;
[Code] ....
View 11 Replies
View Related
May 2, 2013
When I try to assign strings to a double pointer, it only prints out the last string when I try to print everything out, and then it segfaults.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
main(){
char **test = calloc(3,sizeof(char));
[Code] .....
Am I assigning something the wrong way? Also, I am trying to avoid using array notation in order to practice, at least for the assigning of the strings.
View 11 Replies
View Related
Apr 22, 2013
I have a double pointer, and I need to read words into it. Here's what I have so far:
Code:
while(fscanf(keywordFile,"%s",keyinputter)==1){
keys_cnt++;
}
keywords = (char**)calloc(keys_cnt,sizeof(char));
[Code]....
keyinputter is an array of size 30. The first while loop finds the number of words in a file so that I can calloc the correct amount of space for the double pointer, keywords. The second while loop is supposed to read the words into the double pointer, but when I run a print statement to print the string at keywords[0], it only prints null and not the word. Am I assigning the strings to the double pointer in the wrong way?
View 4 Replies
View Related
Aug 1, 2013
How to initialize double pointer array..?
int (**aLines)[2];
View 3 Replies
View Related
Feb 24, 2015
I have recently come across a function call that I do not understand. It uses the arrow operator for a function call, twice, and I don't understand.
Code:
static inline void b43_write32(struct b43_wldev *dev, u16 offset, u32 value)
{
dev->dev->write32(dev->dev, offset, value);
}
I see that the function itself does not return anything but calls another function. The main difficulty I have is with the "dev->dev->" operator, where dev, I expect is a pointer to a structure.
View 8 Replies
View Related
Dec 22, 2014
I have int pointer called p and i want to calculate average.Since average involves using double or float so i am trying to convert this in the function averagetemp. It still gives me an error saying "cannot be converted"...
#include <iostream>
using namespace std;
int* createArray(int n);
int lowesttemp(int *p,int f);
int highesttemp(int *p,int f);
double averagetemp(int *k,double f);
void print(int *p,int lowest_temp,int highesttemp,int average_temp);
[Code] ....
View 8 Replies
View Related
Dec 16, 2012
string codeentrd = Console.WriteLine("Enter the code for verification.", codeentrd );
if (codeentrd.CompareTo("#2") == 0);
{
block = 2;
continue;
}
i don't understand why its an error ....
View 3 Replies
View Related
Mar 6, 2015
I want to convert string to double. I refered this strtod() - C Library Function Example
Code:
#include <stdio.h>
#include <string.h>
int main() {
const char string[] = "$$GPRMC,013732.000,A,3150.7238,N,11711.7278,E,0.00,0.00,220413,,,A*68";
char term;
const char delims[] = ",";
}
[code]....
View 6 Replies
View Related
Dec 28, 2013
In the below function, it muliplies "10.0 * val". Why does it use 10.0? If you remove the 10.0, the return value will still be a double.
#include <ctype.h>
/* atof: convert string s to double */
double atof(char s[])
{
[Code]....
View 1 Replies
View Related
Mar 1, 2014
Why do most C examples pass a double pointer when manipulating linkedlists? Why can not we just pass a single pointer to the struct?I think using an external reference accessor for a linked list would be a more appropriate solution, yes or no?
View 1 Replies
View Related
Nov 30, 2014
The following code compiles and runs fine till it reaches line 16 and gets a sigsev violation which I'm not sure about as to why. I have no problem passing the object of type node** into the constructor of base and storing it into the double pointer node** copy;; but when I call the function void pass(node** temp) it crashes.
#include <iostream>
class base;
class node {
private:
node** data;
public:
[Code] .....
View 3 Replies
View Related
Jun 3, 2013
I have to convert string to double. i'm using "atof" function to achieve same.
I have string as "0.0409434228722337" and i'm converting with "atof" But i'm getting double value as "0.040943422872233702". Why it adds 02 additionally at the end?
More example :
"0.0409434228722337" converts to "0.040943422872233702"
"0.067187778121134" converts to "0.067187778121133995"
Is there any other possibility to convert string to double without changing data ?
View 5 Replies
View Related
Jul 23, 2013
I have pointer object and i have to assign to another variable ( new object ). But if i change any value in new object should not reflect in old object. I did in the below way, it changes the value.
class Test {
public:
int num;
};
Test* obj;
obj.num=1;
Test obj_n=*obj;
obj_n.num=2;
Now both object's num have value as 2;
i.e
obj.num // has 2
obj_n.num // has 2
Actually my expected result is:
obj.num - should have value 1
obj_n.num - should have value 2
My actual scenario is different where Test obj is pointer object, so obj should be pointer object. i have given sample scenario.
View 5 Replies
View Related