-1

I am a basic level programmer. I wrote a function in a c program returning an array (through a pointer,link of program attached) but there is some error. The output should be 12 but the output is 10.

Can someone help me identify the mistake(if there is a concept error)

#include<stdio.h>
int *pointer()
{
    int A[10];
    A[0] = 1;
    A[1] = 2;
    return A;
}

int main()
{
    int *p;
    p = pointer();
    printf("%d", *p);
    printf("%d", *(p+1))
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    We need the code to be able to help you – betaros Dec 20 '17 at 13:46
  • 1
    Hello, Can you please provide your code (or a relevant part of your code) and your error so that we can understand the error and help you ? Each error is different so without information we have no clue to help you ! – NatNgs Dec 20 '17 at 13:46
  • 1
    The array `A` is a *local variable* which will go out of scope and disappear immediately when the function returns. – Some programmer dude Dec 20 '17 at 13:48
  • please, do not put the code as image or link, put it as text using code style – Angen Dec 20 '17 at 13:49
  • Possible duplicate of [Returning pointer to array doesn't give expected output in c](https://stackoverflow.com/questions/42473184/returning-pointer-to-array-doesnt-give-expected-output-in-c) – Bo Persson Dec 20 '17 at 13:54
  • @Someprogrammerdude is almost right, and it's probably best to think of it as he says. Unfortunately, what happens is implementation specific, and rather than the variable "immediately disappearing", you will get odd behavior that sometimes works as expected and other times does not. – William Pursell Dec 20 '17 at 14:21
  • @WilliamPursell -- attempts to access the object referenced by `A` within the function after control has returned to `main()` are not implementation-specific, but lead to undefined behavior. – ad absurdum Dec 20 '17 at 14:36
  • @DavidBowling You are correct. Perhaps I should say "depend on the implementation", since I am trying to avoid getting bogged down in technical language. "Implementation specific" and "undefined behavior" have technical meanings which I don't think matter terribly much to the OP. Either should be avoided. – William Pursell Dec 20 '17 at 14:49
  • @WilliamPursell -- agree that it would be easy to get bogged down with technical details here, and the take-away for OP should be "don't do this." I just wanted to refine your point a bit: the array referenced by `A` may or may not disappear immediately (implementation-dependent), but the later access attempt is UB regardless. – ad absurdum Dec 20 '17 at 15:18

1 Answers1

2

Within the function pointer the array A is a local object of the function that will not be alive after exiting the function. The memory occupied by this array can be overwritten by other objects created in the program.

So returning pointer to a local object with the automatic storage duration of a function results in undefined behavior.

You could make the program valid the following way

#include <stdio.h>

int * pointer( int a[] )
{
    a[0] = 1;
    a[1] = 2;

    return a;
}

#define N   10

int main(void) 
{
    int a[N];

    int *p = pointer( a );

    printf( "%d\n", *p );
    printf( "%d\n", *( p + 1 ) );

    return 0;
}

The program output is

1
2

Another approach is to declare a local array within the function but with the static storage duration.

For example

#include <stdio.h>

#define N   10

int * pointer( void )
{
    static int a[N];

    a[0] = 1;
    a[1] = 2;

    return a;
}

int main(void) 
{
    int *p = pointer();

    printf( "%d\n", *p );
    printf( "%d\n", *( p + 1 ) );

    return 0;
}

In this case the array will be alive even after exiting the function.

Take into account that according to the C Standard the function main without parameters shall be declared like

int main( void )
          ^^^^

also the function pointer without parameters shall be also declared (when it is defined) like

int * pointer( void )
               ^^^^
{
    //...
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335