C++ :: Get Pointer That Iterator Points To And Store In Pointer Variable?
Apr 19, 2014
I'm making a system like twitter for class called ShoutOut.com I want to be able to get the PublicShoutOut pointer pointed to by the start iterator and assign it to firstShoutOutToDisplay and secondShoutOutToDisplay because I need that in order to pass the pointers to one of my functions. When I step through the debugger the values in start are all default values like "" and so are the values in this->firstShoutOutToDisplay but the message that start points to is being output just fine.
EDIT: got rid of irrelevant code. Am I using the correct syntax to do this?
if (start != finish) {
//getting these because a shoutout needs to be passed to the function that displays
//options for a shoutout
this->firstShoutoutToDisplay = (*start);
I just figured out that some std functions (for example: copy) need the resource and target objects have iterators to work. Otherwise, the compiler rejects. In case of two arrays, declared as:
myA[0] is like a pointer, myB.begin() an iterator, if I do not make any mistake. So, what is exactly the difference between the pointer and the iterator here? They all give the access to elements.
If I need the target of copy to be an array like myA which cannot give an iterator, is there a way to make the command "copy" work for it?
what i do with this is to stack fragments of data of type char* coming from a socket in buffer to a vector that acts as buffer, I do this since I transfer big chunks of data and the data gets fragmented by the nature of the sockets, I stack the data once its complete I retrieve the final result from the vector.
this code worked flawlessly for long time but now Im trying to port and compiler throws this error, whats the new way to assign a char array pointer to a iterator so i can stack it in the vector.
I have a program that is trying to find all factors of an integer given. It needs to be done in a recursion function. Right now i have code similar to just getting the prime factors of a integer.
unsigned int * find_factors_using_recursion(unsigned int x ) { unsigned int * factor = new unsigned int[];//do i put x in here ? for(unsigned int i = 2; i < x; ++i) { if(x % i == 0) { find_factors_using_recursion(x / i); *factor = (factor[i] = i); } } return factor; delete [] factor; }
When i cout the *factor = (factor[i] = i) it gives me the prime numbers of the integer passed into the function but when I return the pointer it only returns one of the prime numbers. I'm new to c++, how to return pointers from functions that would be great with an example to go with it.
Sem is a pointer to semantic which is a struct type variable. I pass the sem into function yylex so i can fill the semantic.i and semantic.s(s points to an array). The problem is that when sem->i = a; is used inside yylex function, sem->s stops showing to the array.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <iostream> using namespace std; union SEMANTIC_INFO
I'm trying to keep track of the size of blocks of memory that a pointer points to. No matter what I do, this code below always outputs the integer 8.
If I change 1000 to 5, I still get 8. If I change it to 0, I get 8... If I change it to -1, I get 8. If I change int *a to double *a, I get 8. If I take away the & symbol, I get 8. If I use *& instead, I get 8.
Why? I want it to output 1000. If I change that to 500, I want it to output 500.
int *a; a = malloc(1000 * sizeof(int));
int j = sizeof(&a); printf("%d", j);
I want to build my skills where I can allocate, inspect and change memory sizes.
That code should make the size of the pointer (how many chars it can store) bigger but when i run it it show always 3 char positions while it should show N*M.
Code: #include <stdio.h>#include <stdlib.h> int main(void) { int M, N, P, i; scanf ("%d %d", &M, &N); P = M * N; char *c = malloc(P * sizeof(char));
what happens to pointer pt when string s is reallocated to accommodate bigger size? does it updates itself or does it points to previous s which is not used anymore?
#include <iostream> #include <string> using namespace std; int main() { string s= "aa";
I am stuck at a problem where I have two pointers pointing to the same object, and I need to change an int on one of the pointers but point to the same object.
To be more specific, there is an array of Item objects. A long list of items a player can buy. Then, there is the player's inventory, a vector pointer. Whenever a player buys an item, it sets the pointer to the bought object.
The problem arises when he buys two of the same object. I tried to identify the objects with an ID, but it does nothing, because they are just pointing to the same object, and so I have no way of telling them apart.
This is further complicated by the fact that it is a polymorphic object. So, I can't simply make a new every time I buy an object, without making a hassle. Well, at least I am not familiar with that kind of code just yet.
Suppose you a class declared with a pointer initialization variable. When writing the copy constructor, how would one make a deep copy of the pointer variable? Can it be done in the same manner as automatic variables i.e. in the initialization list or in some other manner?
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
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);
I need to initialize a pointer variable with a knowing address. See code below, ptr is the final destination and value of ptr_address contains the address value, so I need to do something like ptr = *ptr_address.
Code:
int *ptr; int *ptr_address; int address; address = 0x10000005; ptr_address = &(address); ptr = *ptr_address;
The problem is that compiler gives the following warning message:
warning: assignment makes pointer from integer without a cast [enabled by default]
Is my code wrong or there is any other way to do it without receiving this compiler warning?
this question would their be a different process if they asked "declare and initialize a pointer to the variable int cost[N][N]" Here's what I have so far
[#include<stdio.h> int main() { int a; // Step 1 int *ptr; // Step 2 a = cost; // Step 3 ptr = &a; // Step 4 return(0);
I'm writing a delete function for a linked list, and I'm having issues with this bit of code:
void deleteNode(int data){ node* del = NULL; t = h; n = h; while(n != NULL && n->_data != data){ t = n; n = n->next; } }
Or more precisely, this portion:
&& n->_data != data
n is my new node variable, _data is the storage variable in the private section of my class, and data is the information being searched for that needs to be deleted. Everything works without this section of the code. My assumption is that n->_data is somehow wrong, but I don't see how. I've tried everything I can think of- using parenthesis, using the variable rather than the pointer, I've tried expressing the pointer in a different way, I've tried using my t variable rather than n, and I've found examples online that use this exact same expression without any issues.
I am making a function that will return a pointer to a long long variable. For example, I have the next variable prototype: Code: long long funcName(long long x, int s); I want to change the return value, and the first parameter to pointers to long long.
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 am getting and infinite loop for loop pointer variable current which points to head and is incremented by current->next in while loop. I use the pointer variable the same way in my display routine and it works. Here is listing of code.
#include "stdafx.h" #include<iostream> struct node{ int data; node *next;
I'm having to do a little c++ (coming from java) and don't understand the syntax of the following declaration
Code: SensorBase* const sensor(mSensors[i]);
It looks like it's declaring a const pointer to a SensorBase object but I don't understand how that applies to sensor(mSensors[i]) which looks like a function??
I'm fairly new to C++ and have begun working with pointers. I wish to create am array called sigmaf_point that reads data from a text file. I have managed to get that working, but when it comes to using this pointer I come across some problems. The array is created as such:
I then create a coordinate system inside the main file, as the program I am writing is about modelling the movement of atoms, which requires you to know the coordinates:
Code: int main(); double **coords_fluid = new double*[5000]; for (int i = 0; i < n_atoms_methane; i++) { coords_fluid[i] = new double[4]; }
Now, the problem arises when I want to calculate a new variable as so:
Code: for (int i = 0; i <= n_atoms-1; i++) { sf1=sigmaf_point(coords_fluid[i][3]); }
I get the error C2064: term does not evaluate to a function taking 1 arguments, and a red line under sigmaf_point that says it must be pointer to function type. I am a bit confused about this.
I have to write a program to print pascals triangle and stores it in a pointer to a pointer , which I am not entirely sure how to do. I also have to write the file and read it, then create a binary file. Assignment is attached. I am not the best with programming and especially with pointers. I will post my code below.
Code: #include <stdio.h> #include <stdlib.h> void writePascalTriangle(char *fileName, int heightOfTriangle, int **triangle) { FILE *fp; fp=fopen("writePascalTriangle.txt", "w");