C++ :: Vector Of Void Pointers Which Point To Array Of Characters
Jan 21, 2014
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*'".
View 19 Replies
ADVERTISEMENT
Feb 28, 2014
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..
View 4 Replies
View Related
May 21, 2014
I am trying to initialize an array of pointers to an array of characters, I can do it in 3 lines but I really want to do it in one line at the same time keeping the #define.
3 lines initialization (can compile)
======================
#define A 1
#define B 2
char row1[] = {A|B, B, A};
char row2[] = {B, A};
char *test[]= {row1, row2};
1 line initialization (failed)
===============================
char *test[] = { {A|B, B, A}, {B, A} }; // <- how do i do this??
I do not want this because it waste ROM space
=============================================
char test[][3] = { {A|B, B, A}, {B, A} };
View 18 Replies
View Related
Jan 16, 2013
Please consider the following code :
#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
View 2 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
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
May 14, 2014
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.
View 2 Replies
View Related
Jan 11, 2014
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
View 8 Replies
View Related
Aug 31, 2014
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. */
View 9 Replies
View Related
Jul 13, 2014
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 *.
View 7 Replies
View Related
Sep 27, 2014
I'm wondering about the point of pointers to functions. When is it used?I saw the below example. It doesn't make sense to me. I mean we can easily write code that does the same without having to use pointers.
Code:
#include <stdio.h>
int addInt(int a, int b); // Adds 2 integers
int add5to4(int (*function_pointer)(int, int));
int main(void)
{
int sum;
int (*function_pointer)(int, int);
}
[code]....
View 2 Replies
View Related
Dec 23, 2012
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?
View 7 Replies
View Related
Sep 30, 2013
Pointers point to an address in memory. What if I used 3 pointers: 2 to mark the first/last nodes, and the third to mark the current node being referenced? I would wrap it in a class (to make the memory management automatic, of course), but is this practical?? maybe some pseudo code will get the juices flowing:
template<class type>
class supercondensed_list{
public:
supercondensed_list();
~supercondensed_list();
[code].....
Any things I should take into consideration? I'm not exactly the most experienced with pointers, and manually managing memory, but I think it's worth trying. If this works, then my programs should, in theory, be 100% memory efficient.
View 7 Replies
View Related
Apr 14, 2013
So I'm writing a function isPalindrome() that can accept a string as an argument, and copy from it only the alphabetic characters in the argument (original) string to another string named alpha_array which contains only the alphabetic characters. Then the function should be able to call the isPurePalindrome function to determine if alpha_array is an ordinary palindrome.
The problem is that when I call isPalindrome in main, the program crashes.
Here's the code I have for isPurePalindrome and isPalindrome:
Code:
/* 1 */
int isPurePalindrome( const char * sentence ) // Can accept strings, array and pointer arguments
{
// Declarations
[Code].....
View 4 Replies
View Related
May 12, 2014
I am trying to write a basic editor program and one of the parts asks me to "process dot commands that move point in whole line increments"
Earlier it says that a point is interpreted as specifying the location between characters rather than the characters themselves. and i need to use this to implements the following:
< moves point to the beginning of the document. > moves point to the end of the document. p moves point to the beginning of the previous line. n moves point to the beginning of the next line. k deletes the current line and leaves point at the beginning of the following line.
(there was a previous part before this so i already have some code that works) and in this part I'm trying to do a switch case
ie
for
< , >, p , n and k
I guess my question is how do i make this "point" need. Ive been using vectors, so the point needs to be somewhere inside the vector. I don't think ill be able to do what i need to do with out the point.
View 14 Replies
View Related
May 9, 2013
I've got a struct called Node that contains, among other things, a pointer to a vector of pgm objects. (pgm is a class i've created)
struct Node {
int label;
vector <pgm> *ptr;
Node* lessNode;
Node* moreNode;
};
in another class, i create a vector and a Node and am having trouble assigning the pointer in the Node to point to my new vector.
vector <pgm> lessData;
Node* left;
left->ptr=&lessData;
This all compiles ok, but the last line in the code above causes a segmentation fault. I should mention Node is declared on its own in Node.h and what pgm is. including pgm.h in node.
View 2 Replies
View Related
May 7, 2013
I have a pretty big std::vector<matrix>, where matrix is a custom class defined by me. I would like to know how much memory has been allocated to that vector at a certain point in time. Is there any way of doing this in c++?
Or is my only shot, taking a look at the task monitor of windows/unix/whatever at execution time to estimate this?
View 2 Replies
View Related
Mar 20, 2014
so my question is i want to print characters,no string just an array of characters,i do this but it s not working,maybe i have to put the '' at the end?
Code:
int main() {
int i;
char ch[5];
for(i = 0; i < 5; i++) {
scanf("%c",&ch[i]);
[Code]...
View 6 Replies
View Related
Jun 16, 2013
After I cleared the first vector and copy the second in it there still some characters left behind after copying. How is that possible?
Code:
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main()
[Code]...
View 3 Replies
View Related
Aug 21, 2014
I am new to smart pointers and have a question.If you push a smart pointer onto a vector and then some where later pop it back off it will delete the memory right?
View 1 Replies
View Related
Jul 25, 2013
I have something like this:
class A {
};
class B : public A {
};
class C : public A {
};
B*b1;
B*b2;
C*c1;
C*c2;
vector<A*>vec;
int main() {
vec.push_back(b1);
vec.push_back(b2);
[Code] ....
And it don't works at all. all i get (when playing with variations of this stuff until it compiles correctlly) is a memory leak.
For example, let say i have b1 address = 1234
I will effectively free the memory at 1234, but for a strange reason, the memory leak is elsewhere, for example, at 2345, and the memory value at this address is equal to... 1234, the address of the pointer i wanted to delete.
View 7 Replies
View Related
Nov 23, 2013
Currently I am implementing the A* algorithm in C++. I have chosen to use a hybrid of a '2D vector grid' and two 1D pointer vectors to specific places in the '2D vector grid'. I have chosen to do it this way, so I can access the nodes using coordinates and also for iterating over the appropriate nodes(rather than the whole '2D vector grid').
In the below examples I have not included the full code because I deemed it irrelevant to the question.
vector <int> CInGame::AStarAlgorithm(vector<vector<bool>>& resistanceMap, int startX, int startY, int targetX, int targetY, int cutOff) {
vector <int> returnVec;
vector <vector<CNode>> twoDimNodes;
vector <CNode*> openSet;
vector <CNode*> closedSet;
[code].....
The error is:
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
do I need to free the pointers or is it done automatically? If not then how would I do this?
View 3 Replies
View Related
Oct 14, 2014
I made a vector of pointers and the problem is that I have trouble deleting the pointers in the vector. I used to simply do vector.clear() or vector.erase() however it does not clear the actual memory. And I tried something like this:
std::vector<Foo*> Vector;
std::vector<Foo*>::iterator i;
for (i = Vector.begin(); i < Vector.end(); i++)
delete *i;
View 5 Replies
View Related
Sep 4, 2014
If I have a Vector of pointers, how can I iterate through that vector, to access the name function of each object in the vector? There seems to be a problem with my implementation, the output returns only random symbols.
Implementation of the name() function in Drug.cpp:
//name() is a function returning the name in the parent class
string Drug::name() {
string out = "Drug: ";
out += mName + " [ ";
//The problem is with this loop
for (int k = 0; k < mComponents.size(); k++)
[Code] .....
View 10 Replies
View Related
Mar 26, 2014
I have an array of characters. I am trying to find "houston". The only way I can think of to do this is to use a for loop and check each individual character. Is there an easier way to do this?
char string[] = "Fishing tourism miami atlanta dallas houston";
View 9 Replies
View Related
Mar 27, 2013
I have a vector of pointers inside a seperate Exam class.
vector <Question* > question_list
The Question class is my base class in which I have derived sub classes for the different types of questions (MultipleChoice, LongAnswer, etc.). I am using my vector to hold the different types of questions.
in each of those classes I have virtual "write" functions in both the base and the derived classes, that write to a file differing for each type of question.
My problem now is calling the write function from a Exam function. I've tried several methods, such as:
for (size_t i = 0; i < question_list.size(); i++) {
question_list[i].write(testfile.c_str());
}
but it comes with two errors: "error C2228:left of '.write' must have class/struct/union" along with "IntelliSense: expression must have class type"
I have made a write function for the exam class as well but am not sure what it should include since the Exam class is not a derived class of the Question class.
View 15 Replies
View Related