0

I made a simple program to read the no of values and then, those values from file and storing them in an array and print the values of array.

#include<stdio.h>
#include<stdlib.h>

void main(){
    int i=0,j=0,n,no[n];
    FILE *fp=fopen("input.txt","r");
    if(fp==NULL)
        printf("File input error\n");
    else{
        fscanf(fp,"%d",&n);
        *no=(int *)malloc(n*sizeof(int));
        while(i<n){
            fscanf(fp,"%d",&no[i]);
            printf("%d\t",no[i]);
            i++;
        }
    }
}

My input file was as follows

10 37 21 55 52 68 97 02 00 103 84

and the output I got is

37 21 55 52 68 97 2 0

Why do I see this output?

5
  • 1
    change to int *no;.. no=(int *)malloc(n*sizeof(int)); Commented Jun 11, 2015 at 18:16
  • What output did you expect? Commented Jun 11, 2015 at 18:23
  • All the numbers should be printed? Commented Jun 11, 2015 at 18:28
  • Including the first one (10)? Commented Jun 11, 2015 at 18:28
  • I want numbers stored in the array only. Commented Jun 11, 2015 at 18:33

2 Answers 2

2

The line

    *no=(int *)malloc(n*sizeof(int));

is not right. I am surprised your compiler didn't warn you.

*no is of type int. You are assigning a pointer to an int.

Using gcc, I get the following warning.

soc.c: In function ‘main’:
soc.c:11:12: warning: assignment makes integer from pointer without a cast
         *no=(int *)malloc(n*sizeof(int));

Also, the line

int i=0,j=0,n,no[n];

is not correct. n is not initialized before being used to define no.

Here's an updated version of your program that should work.

#include<stdio.h>
#include<stdlib.h>

void main(){
   int i=0,j=0,n;
   int* no;
   FILE *fp=fopen("input.txt","r");
   if(fp==NULL)
      printf("File input error\n");
   else{
      if ( fscanf(fp,"%d",&n) == 1 )
      {
         no = malloc(n*sizeof(int));
         while(i<n){
            if ( fscanf(fp,"%d", &no[i]) == 1 )
            {
               printf("%d\t",no[i]);
            }
            else
            {
               // Unable to read the number.
               // Get out of the loop.
               break;
            }
            i++;
         }
      }
      else
      {
         // Unable to read n.
         // Print some message to indicate the error
      }
   }
}
Sign up to request clarification or add additional context in comments.

5 Comments

what does 'no' signify here?
@NeerajVerma, no is a variable of type int*. No relation with the English word "no".
Dude! I made this program and I know what is no .
I am asking for malloc allocation of this variable
ok, which part of no = malloc(n*sizeof(int)); didn't make sense?
0

In your code, you are trying to read n elements without checking for EOF. You should add a condition comparing fscanf with number of scanned elements (in this case 1) and therefore avoid reaching EOF unknowingly.

Comments

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.