Returning Char Array doesn't work

Why doesn't this code return the proper characters? I'm trying to return a character array from a function. In testChar(), reply has the right values, but random garbage is returned to Main(), especially between the two sets of five characters; I would have thought at least one of the printf's would print "hello".

104
101
108
108
111
 !@
 !@
32
33
64
0
0



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
#include <iostream>
#include <stdio.h> 

void testChar( char* result ){
   result = "hello";
   for (int i = 0; i < 5; i++){
      printf("%d\n", result[i]);
   }
}

int main()
{   
	
   char reply[5];

   testChar(reply);

   cout << reply << endl;
   printf("%s\n", reply);

   for (int i = 0; i < 5; i++){
      printf("%d\n", reply[i]);
   }
}
You are using %d, which is printing the ASCII representations of the word "hello" in the function. Back in main, char replay[5] has not been initialized so you have garbage data in it, and it would not be able to hold "hello" even if it did get initialized with it.
Do you mean this? And why would it not be able to hold "hello?" (null termination character?) Also, I thought it didn't matter if it wasn't initialized, since I'm writing data written to it.

Anwyays, this still doesn't work:
char reply[20] = {0};

All it prints is:
104
101
108
108
111


0
0
0
0
0


(also, printing the ascii representation was intentional, but thanks for the heads up on that in case it wasn't).
When you pass reply to the function, you're not passing the array, you're passing a pointer to the array. In the function, the formal parameter result is a copy of this pointer. When you assign to it on line 5, you're not touching the original array, you're just changing the pointer local to the function. You're doing this:
1
2
3
4
5
6
7
char reply[5];
std::cout <<(void *)reply<<std::endl;
char *p=reply;
std::cout <<(void *)p<<std::endl;
p="hello"; //By the way, if this actually worked the way you think it should
           //work, you'd still be overflowing reply by one character.
std::cout <<(void *)p<<std::endl;

The string literal "hello" is really just a pointer to static data. Assigning it to p doesn't copy character by character the array into reply. It merely makes p point to a different array.
This is a common mistake people who come from higher level languages make. In C/++, you simply cannot copy arrays around with a single assignment. You have to copy them element by element yourself (or call a function to do it for you, if one exists).

Here's your program fixed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cstring>

void testChar(char *result){
    strcpy(result,"hello");
}

int main(){
    char reply[6]; //note the 6
    testChar(reply);
    std::cout <<reply<<std::endl;
    printf("%s\n",reply);
    for (int i = 0; i < 5; i++)
        printf("%d\n",reply[i]);
    return 0;
}
You are passing the pointer by value. Therefore when testChar returns the reply array still hasn't changed and is uninitialized. Also you do not want to try and modify the address of reply anyway. It is illegal. You just can't do that. I'm not sure what would happen if you passed a reference to the pointer in that case. It would either give you a compile error or it would result in undefined behavior. What you need to do in this case is to copy the array with strcpy.

You are better off with one of these constructs though.
1
2
std::string reply("hello");
const char* replyPtr = "hello";
Ahhh! Thanks guys, that makes sense. It is indeed working now.
Topic archived. No new replies allowed.