I am new in C++ area and faced with double pointers. The question is how to set x=5 in function test?
#include <iostream>
using namespace std;
typedef struct _DoublePointer
{
int x;
int y;
} DoublePointer;
void test(_DoublePointer** pointer)
{
}
void main()
{
_DoublePointer* pointer = new _DoublePointer;
_DoublePointer** doublePointer = &pointer;
test(doublePointer);
}
the way to do that is
(*pointer)->x = 5;
Related
I am starter in this language and I have question: Does anybody can explain why this code works? Even if it works, Is this code "dangerous"? Is it a good practice to use global references in C++?
#include <stdio.h>
#include <iostream>
using namespace std;
class v3 {
protected:
double t[3];
public:
v3(double x, double y, double z);
void printout(void);
};
v3::v3(double x, double y , double z) {
t[0]=x;
t[1]=y;
t[2]=z;
}
void v3::printout(void) {
cout<<"(";
for (int i = 0; i < 2; i++) cout<<t[i]<<";";
cout<<t[2]<<")"<<endl;
}
v3 global(3, 3, 3);
v3& globalref = global;
void copylocal2globalref(void) {
v3 local(4, 4, 4);
globalref = local;
}
int main()
{
globalref.printout();
copylocal2globalref();
globalref.printout();
return 0;
}
I thought that the object called 'local' does not exist after the invocation of function called 'copylocal2globalref()'. But I could print out the reference which points to 'local' object! How?!? By the way Is the "reference points to an object" a good expression? It sounds like the refrence would be a pointer but it is obviously not!
I am trying to define a type for function pointers, in c++. However, once I define the pointer funcp as a fptr, I cannot redefine the pointer as a different function, times. This program does not even compile. Is this because I have to delete the nullptr before reassigning times to funcp?
My code:
#include <iostream>
using namespace std;
typedef int (*fptr)(int, int);
int times(int x, int y)
{
return x*y;
}
fptr funcp = nullptr;
funcp = times;
int main()
{
return 0;
}
The problem is that you are trying to do that outside of any function. Try that:
int main()
{
funcp = times;
return 0;
}
Your question has nothing special for C++17. To make it a little more modern and easier to read you may use the using instead of typedef:
using fptr = int (*)(int, int);
How come when I run the following code it outputs 3 not 5? I was under the impression that passing a pointer to a function changes the original variable.
#include <iostream>
using namespace std;
void addTwo(int* b){
b +=2;
}
int main() {
int a = 3;
int* ptr = &a;
addTwo(ptr);
cout<<*ptr<<endl;
return 0;
}
You need to add two, to the contents of what b points at. What you are doing is incrementing the pointer. You need to increment *b:
#include <iostream>
using namespace std;
void addTwo(int* b){
*b +=2;
}
int main() {
int a = 3;
int* ptr = &a;
addTwo(ptr);
cout<<*ptr<<endl;
return 0;
}
#include <iostream>
using namespace std;
void addTwo(int& b){
b +=2;
}
int main() {
int a = 3;
addTwo(a);
cout<<a<<endl;
return 0;
}
First thing, you should use references in C++ instead of pointers. It really is easier in the long run.
#include <iostream>
using namespace std;
void addTwo(int* b){
*b +=2;
}
int main() {
int a = 3;
int* ptr = &a;
addTwo(ptr);
cout<<*ptr<<endl;
return 0;
}
Second thing, you simply forgot the * in addTwo(), incrementing the pointer adress and not the content itself.
I am trying to implement a Polynomial structure using a linked list of Terms (the linked list is implemented separately).
When I run my main program, I get a (Thread 1: EXC_BAD_ACCESS code=2) error on the line
coeff = x; in the definition my setCoeff function.
I tried commenting out that specific function call, but it gives me the same error for the setX() and setY() functions.
I think I have my files and functions set up properly, I cannot figure out why it is not letting me use these functions.
Please help !
In order, I have included: Polynomial.h, Polynomial.cpp, and main.cpp.
#ifndef __Polynomial__Polynomial__
#define __Polynomial__Polynomial__
#include <stdio.h>
class Term {
private:
int coeff;
int deg_x;
int deg_y;
public:
Term();
int getCoeff();
int getX();
int getY();
void setX(int);
void setY(int);
void setCoeff(int);
};
#endif /* defined(__Polynomial__Polynomial__) */
___________________________
#include "Polynomial.h"
Term::Term() {
coeff = NULL;
deg_x = NULL;
deg_y = NULL;
}
int Term::getCoeff(){
return coeff;
}
int Term::getX() {
return deg_x;
}
int Term::getY() {
return deg_y;
}
void Term::setX(int x){
deg_x = x;
}
void Term::setY(int x){
deg_y = x;
}
void Term::setCoeff(int x){
coeff = x;
}
__________________________
#include <iostream>
#include <fstream>
#include "Polynomial.h"
int main() {
Term* t1;
t1->setCoeff(4);
t1->setX(3);
t1->setY(6);
}
You never create an object. You have Term* t1, which is an uninitialized pointer to random memory, then you try to use it with t1->setCoeff(4) which is trying to use an object that was never created. That's definitely gonna go wrong.
Do this instead..
auto t1 = std::make_unique<Term>();
Or if you don't need it to be a pointer, you can create a simple stack variable and access it with '.' operator like this ...
Term t1;
t1.setCoeff(4);
t1.setX(3);
t1.setY(6);
I am learning classes and OOP, so I was doing some practice programs, when I came across the weirdest bug ever while programming.
So, I have the following files, beginning by my class "pessoa", located in pessoa.h:
#pragma once
#include <string>
#include <iostream>
using namespace std;
class pessoa {
public:
//constructor (nome do aluno, data de nascimento)
pessoa(string newname="asffaf", unsigned int newdate=1996): name(newname), DataN(newdate){};
void SetName(string a); //set name
void SetBornDate(unsigned int ); //nascimento
string GetName(); //get name
unsigned int GetBornDate();
virtual void Print(){}; // print
private:
string name; //nome
unsigned int DataN; //data de nascimento
};
Whose functions are defined in pessoa.cpp
#include "pessoa.h"
string pessoa::GetName ()
{
return name;
}
void pessoa::SetName(string a)
{
name = a;
}
unsigned int pessoa::GetBornDate()
{
return DataN;
}
void pessoa::SetBornDate(unsigned int n)
{
DataN=n;
}
A function, DoArray, declared in DoArray.h, and defined in the file DoArray.cpp:
pessoa** DoArray(int n)
{
pessoa* p= new pessoa[n];
pessoa** pointer= &p;
return pointer;
}
And the main file:
#include <string>
#include <iostream>
#include "pessoa.h"
#include "DoArray.h"
#include <cstdio>
using namespace std;
int main()
{
//pessoa P[10];
//cout << P[5].GetBornDate();
pessoa** a=DoArray(5);
cerr << endl << a[0][3].GetBornDate() << endl;
cerr << endl << a[0][3].GetName() << endl;
return 0;
}
The weird find is, if I comment one of the methods above, "GetBornDate" or GetName, and run, the non-commented method will run fine and as supposed. However, if both are not commented, then the first will run and the program will crash before the 2nd method.
Sorry for the long post.
Let's look into this function:
int *get()
{
int i = 0;
return &i;
}
what is the problem with it? It is returning pointer to a local variable, which does not exist anymore when function get() terminates ie it returns dangling pointer. Now your code:
pessoa** DoArray(int n)
{
pessoa* p= new pessoa[n];
return &p;
}
do you see the problem?
To clarify even more:
typedef pessoa * pessoa_ptr;
pessoa_ptr* DoArray(int n)
{
pessoa_ptr p= whatever;
return &p;
}
you need to understand that whatever you assign to p does not change lifetime of p itself. Pointer is the same variable as others.