Pointer Help

Hello, as an assignment I was given some code and asked to modify it to print the following concatenated array {'a', 'x', 'b', 'd', 'g', 'f', 'v', 't', 'r', 'k', 'X', 'G', 'H', 'J', 'L', 'T', 'V', 'D','B', 'Q'}. There is a part of the code I don't understand, lines 18 and 19. *temp points to a[0] (which is 'a') then the pointer function *ch1Toch2 returns the address of temp[0]? I understand that there is suppose to be additional code in between this but I don't understand the logic behind it. Where does ch3[] come into play here? Any help is appreciated.. here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream> 
using namespace std; 
const int SIZE=10; 
char ch3[SIZE*2]; 
char *ch1TOch2(char*, char*); // IntiToInt2 (int*, int*,int* ); 
void Pri(char[]);                      // Pri( int *); 
int main() { 
   char ch1[SIZE] = {'a', 'x', 'b', 'd', 'g', 'f', 'v', 't', 'r', 'k'}; 
   char  ch2[SIZE] = {'X', 'G', 'H', 'J', 'L', 'T', 'V', 'D','B', 'Q'}; 
 cout<<"The concantnated array is: "<<'\n'; 
 Pri(ch1TOch2(&ch1[0], &ch2[0])); 

 system("pause");
   
return 0; 
} 
char  *ch1TOch2(char *a, char *b){ 
char *temp = &a[0] ; 
return &temp[0];  
} 
void Pri(char c[]){ 
   
return; 
}
Last edited on
&a[0] is the address of the first element of the array, which is the same as just a. Same with temp and others. I don't see why those lines are there.
The point of ch1TOch2() should probably be to copy the contents of ch1 to ch3 and append to them ch2, then return ch3.
You could do that with two functions from cstring header, or two for loops.
Last edited on
Topic archived. No new replies allowed.