-2

when i scanned 't' value is 1 but by the end of the loop its value is changed to 2 without any incrementation

#include <stdio.h>

int main()
{
int i,t,n,a[]={},b[]={},k=0,p;
printf("enter the number ");
scanf("%d",&t);
for(p=0;p<t;p++)
{


    scanf("%d",&n);
    for(i=0;i<n-1;i++)
    {
        scanf("%d",&a[i]);
    }

    for(i=0;i<n-2;i++)
    {
       // printf("\nhello");
        if((a[i+1]-a[i])!=1)
        b[k]=a[i];
        k++;
    }
    printf("\n%d",t);
}
return 0;
}

output: enter the number 1
4
1
2
4

2

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 3
    please read the tags description when you ask a question – jhamon Feb 24 '20 at 13:58
  • 2
    `int a[] = {}` - what do you think this does ? How many items does this array have ? – Sander De Dycker Feb 24 '20 at 13:59
  • @SanderDeDycker That's simply a syntax error. It will not compile cleanly on a comforming C compiler. – Lundin Feb 24 '20 at 14:06
  • @Lundin - sure, but that wasn't the intent of my comment. I was trying to point out to the OP the huge red flag of a dimensionless array, which should have been noticed *before* the compiler even got involved. – Sander De Dycker Feb 24 '20 at 14:16
  • @SanderDeDycker It's a common problem that sloppy teachers tell students to compile with gcc as-is. And it defaults to non-standard goo. – Lundin Feb 24 '20 at 14:19
  • @Lundin : all the more reason to train your good old eyeball to detect these issues, rather than rely solely on the compiler. Hence my comment. – Sander De Dycker Feb 24 '20 at 14:23

2 Answers2

1

int a[]={}; is not valid C, details here. The reason it compiles is because you are using the gcc compiler in non-standard mode.

Unfortunately gcc comes like that out of the box. To use gcc to compile code according to the C language specification, you need to use gcc -std=c11 -pedantic-errors instead. Where C11 is the current mainstream version of the standard and -pedantic-errors gives compiler errors when your code doesn't follow that standard.

Advanced: the reason why gcc allows {} in non-standard mode is related to zero-length arrays and pre-standard flexible array members - old non-standard things that pre-date the older C standard called C99, released over 20 years ago. These old features are not something anyone, least of all a beginner, should use or care about.

The GNU C language extensions don't mention what will happen when you access such an array out of bounds. Apparently it crashes just like a standard array.

What you should do instead if the array size isn't known at compile-time:

  • Always compile with gcc -std=c11 -pedantic-errors -Wall -Wextra.
  • Take the array size as input.
  • After you know the size, allocate space for it. Preferably with dynamic memory allocation:

    int* array = malloc(n * sizeof(*array)); // where n is the number of items
    ...
    free(array);
    
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

The arrays a and b are not given an explicit size and their initializer lists are empty. This means the arrays have a size of 0, and attempting to read or write to those arrays will invoke undefined behavior.

Move the definitions of the array after a value has been read for n, then use n as the array size.

int i,t,n,k=0,p;
printf("enter the number ");
scanf("%d",&t);
for(p=0;p<t;p++)
{
    scanf("%d",&n);
    int a[n], b[n];
    ...
dbush
  • 205,898
  • 23
  • 218
  • 273