Skip to main content

Array and Pointers in C

Pointers and arrays are inseparably related, but they are not synonymous.
An array is a non empty set of sequentially indexed element having the same type of data. Each element of the array has a unique identifying index number. Changes made to one element do not affect the other elements. An array occupies contiguous block of memory.
Array notation is a form of pointer notation. The name of an array is the beginning address of the array, called the base address of the array. It is the address of the zeroth element of the array. The array name is the address constant.So *a will get the zeroth element and *(a+1) will get you the first element and so on.The indirection operator * implies that value at address a[i] is equivalent to *(a+i).

Execute this program to know the concept
#include <stdio.h>
int main()
int a[]={10,20,30,40,50};
printf(“%d  %u %u %d %u %u %d”,a[0],a,&a[0], *a,&a[1],(a+1),*(a+1));
Note that a[0] and *a will print the zeroth element that is 10.
a and &a[0] will print the base address.
&a[1] and (a+1) will print the address of first element.
*(a+1) will print the first element ie;20.

It is possible to assign an array to a pointer.
int a[10];
int *p;
p=a   or  p=&a[0]

The pointer to an array does not always point to the first element of the array. It can point to any element of the array.
p=a+3 will point to the third element of the array.

The array elements can be access using pointers in the usual way

p[0],p[1] or *p, *(p+1)

It seems that array name and pointer, which points to the base address of the array are equivalent. But it is not true.
When the memory is allocated for array, the starting address is fixed ie; it cannot be changed during the program execution. Therefore the array name is the address constant, the value contained in it should not be changed.That is array cannot be used as lvalue but a pointer can be.

 Difference between Arrays and Pointers

Arrays allocate space automatically
It cannot be resized
It cannot be re assigned
sizeof(arrayname) gives the number of bytes occupied by the array
It is explicitly assigned to point to an allocated space.
It can be resized using realloc().
It can be re assigned.
sizeof(p) returns the number of bytes used to store the pointer variable p.

The following program will read 5 numbers and print even numbers from it using pointer to array.
#include <stdio.h>
void main()
 int a[5],i;
 int *p;
 printf("\nenter 5 numbers...\n");
 printf("\neven numbers..\n");
 for (i=0;i<5;i++)
 if (*(p+i)%2==0) printf("%d\n",*(p+i));


Popular posts from this blog

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark

2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Strings in C

Strings in C Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Declaration of strings: Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string. charstr_name[size]; In the above syntax str_name is any name given to the string variable and size is used define the length of the string, i.e the number of characters strings will store. Please keep in mind that there is an extra terminating character which is the Null character (‘\0’) used to indicate termination of string which differs strings from normal character arrays. Initializing a String: A string can be initialized in different ways. We will explain this with the help of an example. Below is an example to declare a string with name as str and initialize it with “cek”. 1. charstr[] = "cek"; 2. charstr[50] = "cek"; 3. charstr[] = {'c','…