0
#include<stdio.h>
#include <stdlib.h>
void  deal_queries(int *arr, int limit, int numQueries, int filled) {
    // Write your code here
    for(int i=0;i<filled;i++)
    {
        printf("%d ",*(arr+0));
    }
}
int * read_input(int N,int n){
    //declare dynamic array of size N
    // take input n integers and store them in the array and return pointer to this
    int i;
    int *ptr;
    int array[N];
    ptr=array;
    for(i=0;i<n;i++)
    {scanf("%d ",ptr);ptr++;}
    ptr=array;
    return (ptr);
}
int main()
{
    int N,Q,n; 
    scanf("%d %d %d",&N,&Q,&n);
    int* arr=read_input(N,n);
    printf("%d ",*(arr+0));
    deal_queries(arr,N,Q,n);
    return 0;
}

when I print arr elements in main function I get correct values but if I pass them into deal with queries function I get random values can anyone explain why is this happening?

DuP-491
  • 7
  • 2
  • Maybe you want `printf("%d ",*(arr+i));` instead of `printf("%d ",*(arr+0));` in the function `deal_queries`? – MikeCAT May 21 '21 at 11:29
  • Please don't use trailing spaces in your `scanf` formats. That could lead `scanf` to hand waiting for a non-space character to it knows when the spaces ends. – Some programmer dude May 21 '21 at 11:33
  • Please see [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 21 '21 at 11:33

4 Answers4

0

The variable array is local to read_input, so its life ends when returing from the function and accessing it after that is illegal.

Instead of that, you should allocate an array on the heap and use that.

#include<stdio.h>
#include <stdlib.h>
void  deal_queries(int *arr, int limit, int numQueries, int filled) {
    // Write your code here
    for(int i=0;i<filled;i++)
    {
        printf("%d ",*(arr+0));
    }
}
int * read_input(int N,int n){
    //declare dynamic array of size N
    // take input n integers and store them in the array and return pointer to this
    int i;
    int *ptr;
    int *array=malloc(sizeof(*array)*N); /* allocate an array on the heap */
    if(array==NULL) return NULL; /* check if allocation succeeded */
    ptr=array;
    for(i=0;i<n;i++)
    {scanf("%d ",ptr);ptr++;}
    ptr=array;
    return (ptr);
}
int main()
{
    int N,Q,n; 
    scanf("%d %d %d",&N,&Q,&n);
    int* arr=read_input(N,n);
    if(arr==NULL) return 1; /* check if allocation succeeded */
    printf("%d ",*(arr+0));
    deal_queries(arr,N,Q,n);
    free(arr); /* free allocated array */
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • but I had one doubt about calling read_input function call ends , after that if I try to print arr pointer the value of arr in main it gives correct values so that means arr is pointing to addresses of array in read_input – DuP-491 May 21 '21 at 14:06
0

You return the pointer to the local automatic storage variable which is UB as the variable stops existing when the function returns.

int array[N];
ptr=array;
for(i=0;i<n;i++)
{scanf("%d ",ptr);ptr++;}
ptr=array;
return (ptr);

You need to make it static (bad way) or dynamically allocate the memory

static int array[N];
ptr=array;

or better

//int array[N];
ptr=malloc(sizeof(*ptr)*N);
0___________
  • 60,014
  • 4
  • 34
  • 74
0

The error is in read_input where you have:

int array[N];
ptr=array;
return (ptr);

The variable array is a local variable in the function. And the assignment to ptr makes it point to the first element of this local array. This pointer will become invalid as soon as the function returns and the life-time of array ends.

You need to allocated the array dynamically using malloc instead:

int *array = malloc(N * sizeof *array);

int *ptr = array;
// Fill array using the ptr variable...

return array;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

The pointer isn't a problem, it's what it points to. Here is the issue:

int * read_input(int N,int n) {
    ...
    int array[N]; // This array is in automatic memory,
    ptr=array;    // so it is valid only inside read_input
    ...
    return (ptr); // you are returning a pointer to it
}

In order to do what you want to do you need to use malloc:

int* ptr = malloc(sizeof(int)*N);

Use another pointer in the loop, or apply indexing to ptr, like this:

for (i = 0 ; i < n ; i++) {
    scanf("%d ", &ptr[i]);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523