return an array doesn't work even with static

Hi all, for some reason when I call the function append(m[0], m[1]); it gives my an error : incompatible types in assignment of 'char*' to 'char* [50]'
it copies all the strings in it. So if you output m[0] it'll give you "you" and so on.. but it doesn't pass the strings to the function. Any ideas why?

#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;


char * append(char *str, char *add);



char *append(char *str, char *add)
{

static char buffer[50];
char *p = buffer;

while(*p++ = *str++) p--;
while(*p++ = *add++) p--;
cout << buffer << endl;
return buffer;
}



int main() {

int i = 0;
typedef char* CharArrayPntr;
CharArrayPntr *m = new CharArrayPntr[5];
char* finalString[50];

for (i = 0; i < 5; i++){
m[i] = new char[20];

}
strcpy(m[0], "you");
strcpy(m[1], "are");
strcpy(m[2], "my");
strcpy(m[3], "best");
strcpy(m[4], "friend");


finalString = append(m[0], m[1]);



return 0;
}
For starters use code tags when posting code: http://www.cplusplus.com/articles/jEywvCM9/
In your while loops you probably meant to use == (comparison) instead of = (assignment). For that matter, what are those while loops meant to do?
Hi, thank you for your quick response, they copy the content of "str" to buffer. After all elements are copied, the "add" is appended to the end of the resulting buffer. I decrement p such that there is no space in between them.

For example, if I pass m[0] and m[1] (which are basically "you" and "are"), it would return me : youare


sorry this is my best explanation
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iomanip>
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;


char * append(char *str, char *add);



char *append(char *str, char *add)
{

static char buffer[50];
char *p = buffer;

while(*p++ = *str++) p--;
while(*p++ = *add++) p--;
cout << buffer << endl;
return buffer;
}



int main() {

int i = 0;
typedef char* CharArrayPntr;
CharArrayPntr *m = new CharArrayPntr[5];
char* finalString[50];

for (i = 0; i < 5; i++){
m[i] = new char[20];

}
strcpy(m[0], "you");
strcpy(m[1], "are");
strcpy(m[2], "my");
strcpy(m[3], "best");
strcpy(m[4], "friend");


finalString = append(m[0], m[1]);



return 0;
}

char* finalString[50];

This is not a pointer to char or an array of char. This is an array of 50 pointers to char.

You can't assign a pointer to char to an array of pointers to char. Nor could you assign it to an array of char.

You are also copying the strings "you", "are", "my", "best" and "friend" to memory that you don't own.

while(*p++ = *str++) p--; is equivalent to:
while (*p = *str++) ;

Might want to review pointers and arrays a bit and quit trying to do so much at once.
Last edited on
EDIT: I have been busted for slow typing again !!

@Lachlan Easton

Those while loops are almost C idioms for copying char arrays - the assignment is necessary.

Here is the proper version from K&R (I added braces & comment):

1
2
3
4
5
6
 /* strcpy: copy t to s; pointer version 3 */
void strcpy(char *s, char *t) {
    while (*s++ = *t++) {
         ; // null statement
    }
}


The code for strcat is a little different, but fairly obvious.

http://zanasi.chem.unisa.it/download/C.pdf


@ktk0541

Why do you want to write your own append function? Why not use the strcat function?

If you are including the <string> header why not do this :

1
2
3
4
5
6
7
8
9
std::string finalString = "";
std::string m[5] = {"you", "are", "my", "best", "friend"};

for (i = 0; i < 5; i++) {
    finalString += m[i] ;
    finalString += " ";
}

std::cout << finalString  << std::endl;


The thing is, if you are going to write C code - then do that, don't mix it up with C++ code.

Hope all goes well.
Last edited on
@ktk0541

Also, if you are going to declare your function, I don't see a lot of point in defining it straight after. Put the function definition after main().
Those while loops are almost C idioms for copying char arrays

Whoa what? I've never seen that before. Looking at that version it makes more sense, I think the p-- was putting me off.
As to why returning buffer didn't work:

1
2
3
4
int someFunc(){
   int a = 1;
   return a;
}

When this function is called, it creates the variable a on the stack. Then it grabs the value of a, destroys a, and sends the value to whoever called the function.

1
2
3
4
int* someFunc() {
    int a[20] = {0};
    return a;
}

This will create 20 variables in consecutive memory. The address of the first variable is stored in a (a is actually a pointer). When we return, we grab the value of a (the address of the first element), free up all of that memory just like in the first case, then send the address of a to whoever called the function.

The problem is, we just sent the address of memory which has been freed. Nothing exists at that address anymore!

A work-around? (Not a good idea, but this is for the purpose of explanation):
1
2
3
4
int* someFunc(){
    int* a = new int[20]; 
    return a;
}

The new keyword allocates the memory on the heap instead of the stack. The difference is that the memory is not freed automatically when a goes out of scope. It will do exactly what you wanted above. Now here's the problem: this memory will NEVER be freed unless you explicitly delete[] it. It means that you have to start manually managing memory allocation which adds a layer of complexity that is best not to go into. Use "pass by reference" methods as suggested by people above.
Last edited on
Topic archived. No new replies allowed.