C Mistakes – 1D array



Array is one of the important concept in c programming, while learning or practicing 1D Array most of the beginners unknowingly follow incorrect or invalid syntax of array. This tutorial will list out 1D Array : Mistakes in C Programming

C Programming 1D Array : Mistakes

Mistake 1 : Constant Expression Require

#include<stdio.h>
void main()
{
int i=10;
int a[i];
}

Consider the above example of 1D Array,

  1. We are going to declare an array whose size is equal to the value of variable.
  2. If we changed the value of variable then array size is going to change.
  3. According to array concept, we are allocating memory for array at compile time so if the size of array is going to vary then how it is possible to allocate memory to an array.

i is initialized to 10 and using a[i] does not mean a[10] because ‘i’ is Integer Variable whose value can be changed inside program.

Value of Const Variable Cannot be changed

we know that value of Const Variable cannot be changed once initialized so we can write above example as as below –

#include<stdio.h>
void main()
{
const int i=10;
int a[i];
}

or

int a[10];

Recommanded Article : Compile Time Initializing an Array

Mistake 2 : Empty Valued 1D Array

#include<stdio.h>
void main()
{
int arr[];
}

Consider the above example, we can see the empty pair of square brackets means we haven’t specified size of an 1D Array. In the example array ‘arr’ is undefined or empty.

Size of 1D Array should be Specified as a Constant Value.

Instead of it Write it as –

#include<stdio.h>
void main()
{
int a[] = {1,1};
}

above syntax will take default array size equal to 2. [Refer Article : Section B]

#include<stdio.h>
void main()
{
int a[] = {}; // This also Cause an Error
}

Mistake 3 : 1D Array with no Bound Checking

#include<stdio.h>
void main()
{
int a[5];
printf("%d",a[7]);
}
  1. Here Array size specified is 5.
  2. So we have Access to Following Array Elements – a[0],a[1],a[2],a[3] and a[4]
  3. But accessing a[5] causes Garbage  Value to be used because C Does not performs Array Bound Check.

If the maximum size of array is “MAX” then we can access following elements of an array –

Elements accessible for Array Size "MAX" = arr[0]
                                         = .
                                         = .
                                         = .
                                         = arr[MAX-1]

Mistake 4 .Case Sensitive

#include<stdio.h>
void main()
{
int a[5];
printf("%d",A[2]);
}

Array Variable is Case Sensitive so A[2] does not print anything it Displays Error Message : “Undefined Symbol A

C Programming 1D Array Tips

Tip 1 : Use #define to Specify Array Size

#include<stdio.h>
#define MAX 5
void main()
{
int a[MAX];
printf("%d",a[2]);
}
  • As MAX is replaced by 5 for every Occurrence , So In due Course of time if you want to increase or decrease Size of array then change should be made in
#define MAX 10

Tip 2 : a[i] and i[a] are Same

Visit this article which will explain how to access array randomly using a[i] and i[a].