1

SS4SS of 3rd Problem I want to create the program to take the input from user until he presses 1. I'm using dynamic memory allocation using function and after running this code, this program takes only 4 input and it does not show any output output

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void input(int **arr)
{
  int n=1,i=0;
  *arr=(int *)malloc(sizeof(int));
  int ch;
  do
  {
    printf("\nEnter '1' To Enter A Value in array or else enter '0'");
    scanf("%d",&ch);
    if (ch==1)
    {
      if (!*arr)
      {
        printf("\nInsufficient Memory!");
        return;
      }
    printf("\nEnter the value\t:\t");
    scanf("%d", arr[i]);
    *arr=realloc(*arr,sizeof(int)*(++n));
    *arr[++i]=INT_MIN;
  }
  else if (ch!=1&&ch!=0)
  {
    printf("\nInvalid input!");
    continue;
  }
} while(ch!=0);
free(arr); 
}
void display(int **arr)
{
  for (int i = 0; i < 3; i++)
  printf("\n%d", **(arr+i));
  free(arr); 
}
int main()
{
  int *arr;
  input(&arr);
  display(&arr);
  free(arr); 
  return 0;
}
1
  • you never free the memory you allocate, its not the cause of your problems its just an observation. Commented Nov 26, 2018 at 20:16

1 Answer 1

1

First question

With !*arr[i] you are checking the logical value contained in *arr[i], which can have a random value before you assign a value to it. In your case, the value contained in *arr[i] is 0, triggering the condition !*arr[i].

The correct way of checking if realoc() was succesful, is checkin it's returned value. If it's null, the request failed. In your case, it would be replacing

if(!*arr[i])

by

if(!*arr)

Second question

In this line *arr[++i]=INT_MIN; the index [] operator has precedence over pointer * operator. You have to write parenthesis:

(*arr)[++i]=INT_MIN;

And also here

scanf("%d", arr[i]);

you are saying that arr is an array, when it is a pointer to an array. You should replace it with:

scanf("%d", *arr + i);

Third question

You are also doing free() before you access the values in the array, which triggers the error. You should remove the free() calls at the end of input() and display() and leave only the one of the end of main().

You have still to replace the printf() in display with

printf("\n%d", (*arr)[i]));

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

6 Comments

Thank You! For the help. But now I'm facing another issue, As I had edited my question you can check it out again.
I've included an answer to your second question. You should still leave your first question in case someone has the same issue and finds this post, so it can help them too.
Ok I will do it later, but now there is once again an another issue is arrived, now I can take inputs but in output, for the the 1st input the output is correct and for the rest all inputs it gives garbage value.
Your printf has to be printf("\n%d", (*arr)[i]));
It Didn't worked. Still I'm Getting the same output.
|

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.