hello,
I'm exercising on to how manipulate with pointer addresses,
this piece of code work's fine and it show's the values when exiting function named "f", but I get an error at the end... that error line is commented.
now as it is here work's ...
what if I don't wanna use any of standard functions?
I mean how to initialize it just like that?
maybe using some for loop... I'll try that...
#include <iostream>
usingnamespace std;
void f(char**, int**);
int main() {
char* pass = nullptr;
int* net = nullptr;
f(&pass, &net); // see comments in f()
cout << pass << endl << *net;
delete[] pass; // you cannot delete pass since it now points to "test" which
// you did not allocate on the heap.
// the new char[10]; below is a memory leak
delete net;
return 0;
}
void f(char** x, int** y) { // x points to pass
char* a = newchar[10];
int* b = newint(99);
a = "test";
*x = a; // now pass points to "test"
*y = b;
}
char* a = new char[10]; //a point to the memory that allocated by new.
a = "test"; //now,a is changed, a point to a string constant.
//and the memory that allocated by new was dropped.
delete[] pass; //because pass are equal to a,so you are trying to release a string constant memory.That's why the program fail.