C++ :: Pointer Point To Pointer?
Jan 30, 2014Is this the correct format to point a pointer (pointer1) to another pointer (pointer2)?
&pointer1 = &pointer2
I am kinda rusty when it comes to pointers ...
Is this the correct format to point a pointer (pointer1) to another pointer (pointer2)?
&pointer1 = &pointer2
I am kinda rusty when it comes to pointers ...
Do I really need to create a separate pointer to point to dynamic arrays?
T* temp = new T[capacity_ * 2];
T* tIter = &temp; //Do these need to be here?
T* dIter = &data; //Or can I use *(temp + i) and *(data + i)?
(for unsigned int i = 0; i < size_; i++) {
*(tIter + i) = *(dIter + i);
}
I have a program that has a base class 'control' and there are 2 dervied classes 'button' and 'textbox'. How do i make a constructor in the 'button' or 'textbox' that initializes a pointer of the data type 'control' to point to the object that invokes the constructor. the code should look like this
class control {
//data
} class button:public control {
buton() {
//code for the constructor
}
}
actually i have an array of pointers of the type 'control' and as soon as any instance of a control like button or textbox is created the constructor should make an element of the array to point to the instance
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.
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].....
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
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");
[Code] ....
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);
[Code] ....
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?
i have been fiddling with pointers but I don't understand how the proper syntax is written when I want to acces an element of an array through a pointer to a pointer...The code is all mostly just random bs for learning purposes. I marked the problem "// THIS LINE"
Code:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DATA 100
int find_average(char *iden, ...) {
[Code]...
This is a sample program that declares a Matrix as a structure with an array as a pointer to a pointer. The following program is supposed to store a matrix in the structure "_Matrix" and later print the matrix just entered but it fails giving me a "segmentation fault". The sample code is given below
Code:
#include <stdio.h>
#include <stdlib.h>
struct _Matrix {
int row_size;
int col_size;
int **mat;
[Code] ......
i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)
[Code]...
I need to make a copy of a string that is defined by char *full and copy it into a different pointer defined by char *duplicate. I have written code to do this however it will not work and i cannot figure it out my code is as follows:
char *duplicate = (char *)malloc(strlen(full) + 1);
strcpy(duplicate, full); /*Make second version of full*/
char *Ptr = strtok(duplicate, " "); /*Split duplicate up*/
I have a full program written but i know this is where the problem is because i have used printf statements to see where the program fails. I get no errors and it compiles successfully but it hits this point of the program and it just stops and windows automatically shuts down the program.
char *full is pointing to:
"To be, or not to be? That is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,"
I need to duplicate the string because i need to use strtok but i will need the original string later on so i need an unaltered version.
I have the following code :
Code:
#ifndef TDYNAMICARRAY_H
#define TDYNAMICARRAY_H
namespace Massive {
template<class T>
T **AllocateDynamic2DArray(int nRows,int nCols)
[Code] .....
I wish to know how to traverse or loop through a dynamic 2D array using pointer to pointer as returned by the code above. Like I would in a static T[20][20] 2D array.
I'm trying to call a function via a function pointer, and this function pointer is inside a structure. The structure is being referenced via a structure pointer.
Code:
position = hash->(*funcHash)(idNmbr);
The function will return an int, which is what position is a type of. When I compile this code,
I get the error: error: expected identifier before ( token.
Is my syntax wrong? I'm not sure what would be throwing this error.
I create an instance of a base class (not derived class) and assign it to base class pointer. Then, I convert it to a pointer to a derived class and call methods on it.
why does it work, if there is a virtual table?
when will it fail?
// TestCastWin.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
#include <iostream>
class B
{
public:
B(double x, double y) : x_(x), y_(y) {}
double x() const { return x_; }
[Code] ....
"
#include <stdio.h>
struct datastructure {
char character;
};
void function(struct datastructure** ptr);
[Code] ....
These codes give these errors:
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
These errors are related to
"
*ptr->character='a';
printf("Ptr: %c",*ptr->character);
"
I want to access "character" data inside the structure "trial" by a pointer to pointer "ptr" inside function "function",but I couldn't find a way to do this.
I'm trying to store an array of vectors using a double pointer as a 2D array, but after the first "vector" (column) has been filled, I get a segfault. I correctly included all the libraries needed, and declared all the variables; I am using this function to allocate the space for the array:
double **vec_array(int rows,int clms)
/*allocates a double matrix with -rows- rows and -clms- columns */
{
int i;
double **m;
[Code].....
I get no error while compiling, but when I run the program I get a segfault right after having read the first vector: switching on the check I get in my output just the components of the first vector and then the segfault:
Terminal:
$ ./myprog input_data.dat
0.000000 0.512468 -0.152468
Segmentation fault (core dumped)
Code:
char *ptr;
printf("Output: %d %d %d
",sizeof(ptr), ptr, ptr+1);
Output: 4 214734480 214734481
int *ptr;
printf("Output: %d %d %d
",sizeof(ptr), ptr, ptr+1);
Output: 4 214734480 214734484 when size of the pointer is the same "4 bytes" whether its a char pointer or a int pointer, but why a char pointer increments 1 where as int pointer increments 4.
how can I call and print the Pointer:
ptr->address
Code: #include<stdio.h>
struct account {
int address;
int value;
[code]....
So I was playing around with trying to do an integral using pointers. I've written what I'm trying to do already using arrays
Code:
#include <stdio.h>
#include <math.h>
#define RANGE 500 // Final temp = T0 + RANGE
int main() {
int i, j, h = 1, T0 = 300;
float k300 = 148, alpha = -1.25;
[Code] ...
Which works just fine. Instead I want to try to go through it using pointers. Is there a good way to go through the first loop above, something like?
Code:
#include <stdio.h>
#include <math.h>
#define N 501 // Final temp = T0 + N
int main() {
int T0 = 300;
float k300 = 148, alpha = -1.25;
[Code] ....
Here is a function,which deletes the spaces of a string...
char *removespaces(char *s1) {
Code: char *s2=s1;
int i,j=0;
for (i = 0; i<strlen(s1); i++){
if (s1[i]!=' ') {
s2[j]=s1[i];
[Code] .....
why I have to initialize the pointer *s2 with the first element of the array s1...???If I don't initialize the pointer,or initialize it with something else,I get a segmentation fault...
Can I do the following? Is it a good practice?
Code:
bool *bAct;
bAct = (bool*) calloc (2, sizeof(bool));
for (i=0;i<2;++i)
{
bAct[i] = true; //initialize
}
I need an array of bools for my application.
I tried to use pointer with a function but I guess I am missing out on something
Code:
#include<stdio.h>
int add(int *a,int *b){
int *c,*d;
*c = *c+1;
*d = *d-1;
[Code] ....
And this one too
Code:
#include<stdio.h>
void add(int *a){
int *c;
c = c+1;
[Code] ....
Following is the code snippet
Code:
[COLOR=white !important]?
1
2
3
4
5 char str1[]="Bombay";
char str2[]="Pune";
char *s1=str,*s2=str2;
while(*s1++=*s2=str2);
printf("%s",str1);
Output of this code comes out to be Pune
But according to me output should be puneay.
Pune should be copied in place of Bomb.and rest should be as it is.
[/COLOR]
I was wondering what magic does a * pointer before function actually do? Today our programming teacher asked us to look into it and explain it in the next class!
#include<iostream>
using namespace std;
int *binary(int []);
[Code].....