Challenging Question! I've to point char* to an int

Jun 23, 2013 at 3:45pm
1
2
3
4
5
6
7
8
char* f(){
      char* result;//I have to return 'result' at the end of this function, where result should be containing i (i.e. int i=97) in it.
      int i = 97;

      //Do whatever you can

      return result;
}
Last edited on Jun 23, 2013 at 3:55pm
Jun 23, 2013 at 4:11pm
<nolyc> You're trying to do X, and you thought of solution Y. So you're asking about solution Y, without even mentioning X. The problem is, there might be a better solution, but we can't know that unless you describe what X is.
Jun 23, 2013 at 6:56pm
1
2
3
4
5
char* f()
{
      char* result = new char( 97 );
      return result;
}
Jun 23, 2013 at 7:10pm
closed account (o1vk4iN6)
That's not the ideal solution though, it is error prone and the user would have to look up documentation to see what kind of pointer is being outputted and whether they need to handle deallocation or not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

void f(char* out)
{
    *out = 97;
}

// ...

char a;
char* b = new char;

f(&a);
f(b);

delete b;
Last edited on Jun 23, 2013 at 7:13pm
Jun 23, 2013 at 7:35pm
@OP: Why do you need to return a char*?
Do you need to return multiple types of variables, are you forced to return char*, or...?
Topic archived. No new replies allowed.