C++ :: Use The Void Pointers?
Nov 25, 2013how 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?
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?
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.
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.
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
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. */
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 *.
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*'".
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?
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..
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??
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.
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] .....
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] .....
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].....
I'm trying to write a function that takes a 32bit address and a data to store at this address.
I'm wanting to take the 32 bit memory address eg 0x12345678 and split it
into 4 x 2 bytes
12, 34, 56, 78
then each of the 4 entries is at most a 256 entry array.eg
FF, FF, FF, FF
So in this example, 0x12 points to 0x34 in the second array, which points to 0x56 in the third array, which finally points to 0x78 in the last array. This last array holds the actual data.
After successfully doing 0x12345678, say I might get a read for 0x1234AABB. So, the first and second pointers already exist, but I then have to create and write to dynamically created arrays.
The arrays need to have all entries set to NULL so that i know whether to follow the pointers to overwrite a previously entered value or create new arrays and pointers.
It all looks good and simple in the pseudo code I've written up but I'm having trouble coding it. I'm currently trying to deal with the first entry case, ie all array elements are NULL, but I'm getting confused with the pointers and creation of new arrays.
void cpu::store(unsigned int mem_add,unsigned int mem_val) {
int first = (mem_address&4278190080)>>24;
int second = (mem_address&16711680)>>16;
int third = (mem_address&65280)>>8;
int fourth= (mem_address&255);
[Code] .....
A1 has been declared as
int* A1[256] ;
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");
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 RelatedCode:
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*.
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.
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[])
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 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);
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 Relatedint (*cInts)(void*,void*);
cInts= &compareInts;
int x=(cInts)(2,5); //This wont work. I tried a bunch of other things
printf(x);
I need this break in my main function, but I'm not allowed to put it in void since void is not located in a loop. How can I solve this?
Code:
#include <iostream>#include <string>
using namespace std;
void login(string x, string y);
string username;
string password;
string x;
string y;
[Code] ....
I wanted to make a multiplication table.
Here's the code:
#include<iostream>
#include<conio.h>
using namespace std;
void initArray(int arg1[50][50], int r, int c) {
int val=1;
for(int row=0; row<r; row++) {
[code]....
I want the output to be like this:
1 2 3
2 4 6
3 6 9
If the user inputs 3 rows and 3 columns.