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

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<

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…

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …