Char pointers assign


Hello, hope you all are doing well!

How do I make "a" ("a" is inside another function) equal to "c"? (I cant use a return value for this)


1
2
3
4
5
6
7
8
9
10
char * a;

myfunction(a);


void myfunction(char * b) {

   char * c = new char[200];

}



Last edited on
"a" is inside another function


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 = new char[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
Last edited on
as above, or you can return the value so you can say
a = myfunction(); //it may not need a parameter, depends on what you are doing.


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 = new char[200];
myfunction(a);

void myfunction(char *& x) {
   delete[] x;
   x = new char[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
Last edited on
Could you use std::byte rather than char?

https://en.cppreference.com/w/cpp/types/byte

It has some limitations, but could be ok for imagery, depending on what you may want to do with it.

You really should consider avoiding new / delete , use smart pointers instead: std::unique_ptr; or std::shared_ptr if there is shared access.

Good Luck !!
Last edited on
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"?
Absolute! Consider that if 'a' is a pointer, it could him any new address assigned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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 = new char[10];

     x = new char[10];
     for( int i = 0; i < 9; ++i)
     {
         x[i] = i + '0';
     }
     x[9] = '\0';
}

int main()
{
    char * a = new char[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
Last edited on
does this actually happen to the variable "a"?

That is what references do:
1
2
3
4
5
6
7
8
9
#include <iostream>
 
int main()
{
    int a = 7;
    int& x = a;
    x = 42; // modify a via reference
    std::cout << "after:  " << a << '\n';
}


Note:
1
2
char * a = new char[20];
delete [] a; // nothing "happens to a" here 
I tried std::string for images/videos, which didnt work
String is designed for printed text. You might have better luck with vector<char> or vector<byte> instead.
Registered users can post here. Sign in or register to post.