You've said that, but in the code you've shown us, a is quite clearly NOT inside another function. Which is it?
Can you pass a by reference?
1 2
char* a;
myfunction(a);
where myfunction is:
1 2 3 4
void myfunction(char *& a)
{
a = newchar[200];
}
I feel I would be remiss if I did not say that I hope this is just example code; C++ comes with a self-managing class made for being a better replacement for an array of char; string
So in the function below, when I delete "x" in "myfunction" and then point it to "new char[400]", does this actually happen to the variable "a"?
I interpret it as "x" becomes a pointer to "a", then the assign to "x" is applied to the variable that "x" points to?
1 2 3 4 5 6 7
char * a = newchar[200];
myfunction(a);
void myfunction(char *& x) {
delete[] x;
x = newchar[400];
}
I feel I would be remiss if I did not say that I hope this is just example code; C++ comes with a self-managing class made for being a better replacement for an array of char; string
I tried std::string for images/videos, which didnt work well when certain characters where involved in the file. Char array has worked good for this so far
#include <iostream>
void fnc( char *& x)
{
delete[] x;
// ensures (hopefully) that x points later to a different
// storage place when memory get reallocated
char * y = newchar[10];
x = newchar[10];
for( int i = 0; i < 9; ++i)
{
x[i] = i + '0';
}
x[9] = '\0';
}
int main()
{
char * a = newchar[20];
for( int i = 0; i < 19; ++i)
{
a[i] = i + 'a';
}
a[19] = '\0';
std::cout << "before: " << a << '\n';
fnc( a );
std::cout << "after: " << a << '\n';
delete[] a;
}
output:
before: abcdefghijklmnopqrs
after: 012345678
*edited - the pointer must be passed into fnc as reference