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
ADVERTISEMENT
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
Apr 12, 2014
This code i made is a cent converter from 5 to 95 cents. The problem i'm receiving is when the 'cents' function is sent back to the 'main' function it only prints one line. It seems to just print the first if construct that complies with the statement. Is there anyway i can have this function print multiple cent values? For example if 60 cents was entered it would only print '50c', and i want it to print '50c' and '10c' instead.
Code:
#include <stdio.h>
int x;
void check(int x)
{
if( x < 5)
printf("Less then 5 cannot be calculated
");
else if(x > 95)
[code]....
View 3 Replies
View Related
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
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
Oct 7, 2014
I'm trying to print the full value (in hexadecimal) of a pointer.This is my best attempt so far:
Code:
char *text = "x10x11x12x13x14x15x16x17";printf("%x", &text);
And it obviously doesn't work, best I've gotten is a single byte, but I want the whole lot of bytes.
View 4 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
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
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
Jan 11, 2014
If i declare a function as a void function. But for testing purpose if i use a return statement in the function definition. i have tested and found that the function does not return and executes the entire function. How does the function not return even if a return statement is available? Does the compiler removes this return statement or how it is?
View 2 Replies
View Related
Nov 27, 2014
Write a C++ program consisting of main plus two other functions which will do the following:
Take an integer input from the keyboard.
Send the integer to a function which will output the integer to the screen.
Send the integer to a second function which will tell the user that the integer is an odd value.
Do not tell the user anything if the integer is an even value.
Repeat this process until the user enters something which is not an integer; use input validation to check for validity.
Any not valid input should terminate the program.
View 3 Replies
View Related
Mar 24, 2014
I was reading about void as function argument, but I did not fully understand it's meaning in C.
In C++
void foo(void) {}
and
void foo() {}
are the same. It means no arguments for foo function. But in C it's different. First function means the same as in C++, but second means
In C, an empty parameter list means that the number and type of the function arguments are unknown. But if it is unknown you can't use this arguments if user specifies same. Because here are no variables to store them. So doesn't result are the some? You do not get any arguments. O do I can get this arguments from some hidden variable?
For example.
void foo() {
printf("%d", var);
}
foo(5);
It is very unclear for me. Do this apply to main function too?
int main(void)
int main()
or can I use arguments given to int main() like given to int main(int argc, char* argv[])
View 4 Replies
View Related
Mar 11, 2014
My assignment is to write a program using VOID FUNCTIONS WITH AN ARGUMENT.
*I need one non-void function with an argument to generate the first 15 numbers greater than 500, another non-void function with an argument to generate the first 15 perfect squares that are greater than 500. Last, they need to be in columns next to each other.* also i cant use x,y, coordinates to align them. i must create a for loop with the
These are some notes from examples in the class. i just don't know how to do it with non void functions with an argument.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
void ClearTheScreen();
void NormalTermination();
[Code] ....
View 5 Replies
View Related
Jul 25, 2013
How to declare variable for all void() as I have another void s in my C++ program. I want to have a variable that can use for all the void and not only in a simple void.Is it possible?
View 17 Replies
View Related