-1

I have tried the below code, but it doesn't work. Shows error that "initializer element is not constant" if I give const .. it still doesn't work. Please help.

#include<stdio.h>

int* ret()
{
    int x=0;
    int y=0;
    int b[]={x,y};
    return b;
}
int main()
{
    printf("My name is sudha\n\n");
    int* m=ret();
    for(int i=0;i<2;i++)
    printf("%d",m[i]);
    return 0;
}
alk
  • 69,737
  • 10
  • 105
  • 255
M.S.Sudha
  • 11
  • 6
  • Possible duplicate of [Returning an array using C](https://stackoverflow.com/questions/11656532/returning-an-array-using-c) – WolfLink Jul 27 '17 at 01:00
  • 1
    This compiles and runs for me, however you're accessing unallocated memory when you printf(), this is undefined behaviour. Once ret() returns int b[] no longer exists. – Zebrafish Jul 27 '17 at 01:02
  • Does anyone know why this compiles for me in Visual Studio C++? Is there a major difference with how arrays can be initialised between C and C++. I always new variable length arrays weren't allowed, but my compiler lets me initialise the values of the array even with a random number. – Zebrafish Jul 27 '17 at 01:10
  • GCC compiler compiles it. The only problem is ret() function returning a pointer to a local variable which is undefined behavior in c. Also, it won't compile in C89 as for loop initializes i. – MCG Jul 27 '17 at 01:18
  • @MCG Does that mean the answer is correct in saying it's an error? By the standard I mean. Also, is this the same case in C++? My Visual Studio C++ compiles it fine, even with a rand() initialiser. – Zebrafish Jul 27 '17 at 01:22
  • 1
    @MCG: Not if you use the standard compliant mode. – too honest for this site Jul 27 '17 at 02:33
  • Sidenote: Functions which dont take arguments should be declared like `f(void)`, not with an empty parameter list. The latter is an obsolescence feature and to be removed in a future version of the standard. As a beginner you better learn it correctly from the beginning. – too honest for this site Jul 27 '17 at 02:38

1 Answers1

4

There are two errors in your code:

  • You are using non-const expressions in initializer, and
  • You are returning a pointer to local from a function

This can be fixed by returning a malloc-ed array that is initialized by hand:

int* ret() {
    int x=0;
    int y=0;
    int *b = malloc(2 * sizeof(*b));
    b[0] = x;
    b[1] = y;
    return b;
}

You need to free the result after you are done with it:

int* m=ret();
for(int i=0;i<2;i++)
    printf("%d",m[i]);
free(m); // <<=== Add this call
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • using `malloc` does not change the array "initialisation", as there is noi initialiser possible for `malloc`ed arrays. One could use the same for automatic/static arrays. The only point to **possibly** use `malloc` is the lifetime of the array. And there are other ways to achieve this. – too honest for this site Jul 27 '17 at 02:36
  • @Olaf To me, this is a "help me fix this code" question. I understand initialization in a broader sense of providing initial values. If there are other ways besides `malloc` to retain lifetime of an array beyond the function where it has been allocated, without also making the array `static` or global, I would be curious to find out what it is. – Sergey Kalinichenko Jul 27 '17 at 02:50
  • You can wrap the array inside a struct, and return the struct. – Nisse Engström Jul 27 '17 at 05:46
  • @dasblinkenlight: Yes, the term is ambigous. But here it is clearly used in the standard's sense: Setrting up values with the definition. I used quotation marks intentionally in my comment to signify I meant normal initial assignments. And I did not exclude static array as option (there are no "global" variables in C; they are simply static). – too honest for this site Jul 27 '17 at 11:02
  • Thank you for your responses.. But the code @dasblinkenlight does not work still – M.S.Sudha Jul 28 '17 at 23:45
  • @M.S.Sudha: I commented on part of the text, not the code per se. If you can't make it run, reading a C book and learning the language properly (including terminology) would be good (not that it should not have been done before/in parallel). – too honest for this site Jul 28 '17 at 23:50