1

I'm trying to figure out how returning an array works out in C. This is the code. What I'm trying to do is save an array and then print it by using a function return. What am I doing wrong here?

#include <stdio.h>
#define DIM 50


char *func (char input[]);

int main(void)
{
   char input[DIM];

   printf("input? ");
   fgets(input, DIM, stdin);

   printf("output: %s", func(input));

   return 0;
}

char *func(char input[])
{
   int i;
   char output[DIM];

   for(i = 0; i < DIM; i++)
   output[i] = input[i];
   return &output;
}
2
  • 2
    You are returning address of a array local to function, which does nto exist beyond the function scope. Commented May 24, 2013 at 15:52
  • printf("output: %s", func(input)); maybe changing it with a pointer format: printf("output: %p", func(input)); can do it since you get a pointer. Commented May 24, 2013 at 15:53

3 Answers 3

1

One problem is already stated by the other answers:

  • It is not safe to return the address of a local (non-static) variable from a function.

The other problem is that given:

char output[DIM];

the type of:

return &output;

is 'pointer to array of char' and not char *. If you wrote:

return output;

you would be OK — if you sorted out the duration of the returned value. Similarly, if you defined the function as returning a pointer to an array, you'd be alright:

char (*func(char input[]))[]
{
    static char output[DIM];
    ...
    return &output;
}

Note that using the static variable like that renders the function non-reentrant (unsafe for use in threaded applications) unless the array is in fact constant (in which case, the definitions should say it is constant).

Sign up to request clarification or add additional context in comments.

Comments

1

In func you are returning a pointer to a local variable here:

return &output;

which will not exist once you leave the function and as Jonathan pointed out, you are actually returning a pointer to an array of char, if you wanted to return the pointer you would have just done return output but that would still be wrong.

If you can not use dynamic allocation then you would need to pass the output as an argument.

4 Comments

This is part of an exercise that was required before dynamic allocation was introduced to us. I think the exercise is wrong and there's no way to do it(without dynamic allocation) unless you pass the array as a parameter. Right?
you can also dereference to the first element and reference this adress, looks nasty but works also. Like this &(output[0])
you can also make the array global, although standard practice would be to pass inthe output array
Or make it static.
1

It is possible to return the adress, but not that way.

Try this

char *func(char input[])
{
int i;
char *output = (char*)malloc(sizeof(char) * DIM);

for(i = 0; i < DIM; i++)
    output[i] = input[i];



    return output;
}

An other way is to do it like 'Call by reference'. Looks like this

void func(char *input, char *output)
{
int i;

for(i = 0; i < DIM; i++)
    output[i] = input[i];

}

And your main should look like this

int main()
{
char *input = (char*)malloc(sizeof(char) * DIM);
char *output = (char*)malloc(sizeof(char) * DIM);

printf("input? ");
fgets(input, DIM, stdin);
func(input, output);

printf("output: %s", output);
free(input);
free(output); // after you finished your work with this variable

return 0;
}

2 Comments

Please free() the memory you allocated! Otherwise you're teaching a bad habit.
And check that the malloc() calls succeeded before using the null pointers, and check that the fgets() succeeded... Also, in this context, I think that it would be much simpler to simply declare char input[DIM]; and char output[DIM]; in the main() function. There really isn't a benefit to dynamic memory allocation here unless DIM is so enormous (multiple megabytes) that it blows the stack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.