Skip to main content

Array of Pointers


Array of pointers is simply an array which can store address.
Like we declare an array , array of pointers can be declared very easily.
 int *p[10]; // will create an array of 10 pointers, each of which points to an integer.
We can make them point to integer variable
Let int a=10,b=20,c=30;
p[0]=&a;p[1]=&b;p[2]=&c; this will make the pointer array element p[0],p[1] and p[2] points to integer variables a,b and c respectively.

In general an array of pointers can be used to point to an array of data items, with each element of the pointer array pointing to an element of the data array. The advantage of an array of pointers is that the pointers can be reordered without moving the data items. Reordering pointers is relatively fast compared to reordering large data items such as data records or strings.Sorting an array of strings requires swapping the strings which is complex. The solution is to use to pointers and order the pointers based on the strings order.

The following program illustrate the use of array of pointers
#include <stdio.h>
void main()
{
 char name[100][50];
 char *ptr[100];
 char *temp;
 int i,j,n;
 printf("enter number of names...");
 scanf("%d",&n);
 fflush(stdin);
 printf("enter names\n");
 for(i=0;i<n;i++)
 {
  gets(name[i]);
  ptr[i]=name[i];
 }
 for (i=0;i<n-1;i++)
 for(j=i+1;j<n;j++)
 {
  if (strcmp(ptr[i],ptr[j])>0)
  {
   temp=ptr[i];
   ptr[i]=ptr[j];
   ptr[j]=temp;
  }
 }
 printf("names in sorted order\n");
 for(i=0;i<n;i++)
 printf("%s\n",ptr[i]);
 }


Comments

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

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
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<

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 …