1

I am new to C programming. Take a look at my code & point me where i am doing wrong? Here is my code to create a range function which should return int array.

#include<stdio.h>
int * range(int a, int b);

main()
{
    int *r;
    int j;
    r = range(0,5);
    printf("%d",r[0]); // getting the first one to check array but not working
                       // instead showing segmantation fault
}

int * range(int a, int b) {

    int last[b];
    int i,j;

    for(i=a;i<b;i++){
        last[i] = i;
    }

    return last;
}
bruceg
  • 2,433
  • 1
  • 22
  • 29
Rishad
  • 141
  • 2
  • 9
  • 2
    Possible duplicate of [Returning an array using C](https://stackoverflow.com/questions/11656532/returning-an-array-using-c) – Eugene Sh. May 23 '18 at 20:22
  • 1
    Possible duplicate of [Function returning address of local variable error in C](https://stackoverflow.com/questions/22288871/function-returning-address-of-local-variable-error-in-c) – Weather Vane May 23 '18 at 20:24
  • int last[b]; what's this? this is fault – amin saffar May 23 '18 at 20:26
  • @aminsaffar `int last[b];` is a VLA (variable length array). It's only a fault if the compiler does not support it. – Weather Vane May 23 '18 at 20:27

4 Answers4

3

You are returning the address of a local array whose lifetime ends with the function.

Change

int last[b];

to

int *last = malloc(sizeof(int) * b);

Don't forget to call free(r); after the printf

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
2

Here is the working code

#include<stdio.h>
int * range(int a, int b);

int main()
{
     int *r;
     int j;
     r = range(0,5);
     printf("%d",r[1]); // Outputs 1
     free(r);
     return 0;
}

int * range(int a, int b){
    int *last = malloc(sizeof(int) * b);
    int i,j;
    for(i=a;i<b;i++){
        last[i] = i;
    }
    return last;
}

You can run in Ideone https://ideone.com/OzaYoL

Md Johirul Islam
  • 5,042
  • 4
  • 23
  • 56
2

Your immediate problem is that the last array ceases to exist as soon as the range function exits, so the pointer being returned is no longer valid.

If you want range to set aside the memory for the array as well as initialize its content, then you'll have to allocate the memory dynamically:

int *range( int a, int b )
{
  int *last = malloc( sizeof *last * b );
  if ( last )
  {
    for ( size_t i = a; i < b; i++ )
      last[i] = i;
  }
  return last;
}

You will need to remember to free that memory when you're done with it.

A quick comment - it's not clear to me how you intend range to work when it comes to setting values. If you called range(3,6), you'd get a 6-element array, but you'd only set the values of last[3], last[4], and last[5], leaving last[0] through last[2] containing indeterminate values. If that's what you intended, great. If not, you'll want to re-think this.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

This happens because your function int * range(int a, int b); returns address of local variable. So in order to correct it you have to dynamically allocate memory to last .

Try this code :

int *last=(int *)malloc(sizeof(int)*b);

This will work.

anoopknr
  • 3,177
  • 2
  • 23
  • 33