C/C++ :: How To Use Void Pointer Functions
Sep 27, 2014int (*cInts)(void*,void*);
cInts= &compareInts;
int x=(cInts)(2,5); //This wont work. I tried a bunch of other things
printf(x);
int (*cInts)(void*,void*);
cInts= &compareInts;
int x=(cInts)(2,5); //This wont work. I tried a bunch of other things
printf(x);
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?
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.
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] ....
I want to write a code that gets three values from the user and puts them into three arrays. When the user enters -999, I want to print out a chart showing all the values they put in. This is what I have so far but it wont build. It tells me std::string is requested, but I'm not sure where to put it, and printArrays is declared void. How can I fix this?
#include <iostream>
#include <string>
using namespace std;
const int ARSIZE = 400;
void printArrays (string reportTitle, int levelsArray[], int scoresArray[], int starsArray[], int i);
[Code] ....
1) This first function initializes an array of 30 components so that the first 15 components are equal to the square of the index value and the last 15 components are equal to the index value multiplied by 3.
2) The second function processes the array by finding the sum of the first 15 components and the sum of the last 15 components to determine which sum is bigger. The output to the screen should do the following:
a)State “The sum of the first 15 components is:” and then show the sum.
b)State “The sum of the last 15 components is:” and then show the sum.
c)State which of the two resulted in the greater sum or if the two sums were equal.
Function: The program uses two subroutines. One to initialize an array and the other to process the array and print to screen results:
#include <iostream>
#include <iomanip>
// Include any other header files you may need.
const int ARRAY_SIZE = 30;
void initialize ( double list[], int index );
void square ( double list[], int index );
void threeTimes ( double list[], int index );
void output ( const double list[], int index );
[Code] .....
At first i had my int variables in global scope however i cant do the so im trying to pass my variables from my main to the void functions but cant.....
View 1 Replies View RelatedI missed last class on doing void functions because I got sick and im completely lost! ive being using the texts book example for a reference but its not running !
The output should look similar to this:
how much was your shirt?
20
shirt 20.00
tax =1.20
the total 21.20
Code:
include <iostream>
#include <iomanip>
using namespace std;
void getShirtCost(double shirtCost);
void calculate(double shirtCost,double taxAmount, double total, double taxamount) ;
void printReceipt(double shirtCost, double taxAmount, double total, double taxamount);
[Code] ....
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]....
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");
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*.
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);
This is an assignment which the purpose is to calculate an angle value in form of trigonometric functions. These are the codes that I've wrote so far.
#include <iostream>
#include <cstdlib>
using namespace std;
void menu(double &value) {
system("cls");
cout<<"*****Trigonometry Program*****"<<endl;
[Code] ....
I have completed the codes for the interface part. Before I proceed with the formula for the trigonometric functions, I would like to make sure the program is Error-free, which if there is accidental invalid input from the user, the program would the user to enter another input until it is a valid response.
The only problem I have encountered for this matter was in menu(value)
If I enter an integer, the program will proceed without error. However, If I enter a character, the program will slip into an endless loop which constantly shows this
*****Trigonometry Program*****
Please enter an angle value => Is the angle in Degree or Radian?
Type D if it is in Degree
Type R if it is in Radian
Your response=> 0 //my initial input for value
Do you want to continue?
Type Y to continue
Type any other key to stop
Your response =>
Where is the source of the problem? I'm pretty sure it's the loop, but I don't know what to do.
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] .....
#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 ?
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....
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...
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] .....
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 .
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?
I made this example code to illustrate my question:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Right(char* In, char* Out, int Position){
Out=&In[strlen(In)-Position];
[Code] ....
Well, I guess the code explains it all. I want b to be the last three characters of a using a function called "Right".
Doing exactly the same thing without the function involved works fine. I just let b point to the third last character of a.
Why does the function not work?
I get a run time error saying something about memory locations when I run this program.How can I make this program work using pointers?
//this program converst miles to km
#include<iostream>
#include "std_lib_facilities.h"
[Code]....
whats the difference between functions as pointer and normal function, eg:
void function1(void)
void *function1(void)
What is the difference between the two?I'm doing parallel programming and we use pointer functions (void *function1(void)) when calling threads. I want to why it is done.
Here is the assignment: (3pts) Given the following class header file, write the class’ source code for each of the accessor and mutator functions listed. (How the functions have listed their parameters, varying between passing by reference and by value.) Don’t forget to comment your code – it counts!
class Album {
private:
char * artist; // band or singer’s name
char * title; // title of the album
[code]....
The input will be an array. My questions: First, am I on the right track?
When using (char * a) for a function, for example, this is passing the address of a, correct? so then *artist=a; changes what the address of a points to?
also, the functions are bool when I would expect void. Why? for all of the set_" " functions, the parameter is *... but for set_record_label it is *&. That appears to be a mistake to me. Is that right?
what is the difference between *& and * as parameters?
There are, or course, better ways to do this, but I need to stick to some rules:
(1) Use only pointer variables and not arrays or structs.
(2) Use the three functions shown--regardless of easier methods.
The program should ask for some input, operate on those numbers, and then display the results. I know I am confused over these things:
(1) All that syntax using '*' and '&' or neither.
(2) How to use the char type correctly.
(3) How to use a char type input as an operator (a + b).
(4) How to use the pointer of the operator variable (+,-,*,/) in an actual equation.
Code:
#include <stdio.h>
#include <stdlib.h>
// *** Prototype Functions ***
void Post_Results (float*);
void Calculate (float*, float*, char*, float*);
void Get_Numbers (float*, char*, float*);
[Code]......
I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.
We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.
As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.
NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.
Stack.h
#include "stackADT.h"
#include "unorderedLinkedList.h"
template<class Type>
class Stack: public stackADT<Type>{
template <class T>
struct nodeType
{
T info;
nodeType<T> *link;
[Code]...