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

KTU Mandatory C programs for Laboratory and Solutions

LIST OF LAB EXPERIMENTS 1. Familiarization of Hardware Components of a Computer 2. Familiarization of Linux environment – Programming in C with Linux 3. Familiarization of console I/O and operators in C     i) Display “Hello World”     ii) Read two numbers, add them and display their sum     iii) Read the radius of a circle, calculate its area and display it 4. Evaluate the arithmetic expression ((a -b / c * d + e) * (f +g))   and display its solution. Read the values of the variables from the user through console 5. Read 3 integer values, find the largest among them. 6. Read a Natural Number and check whether the number is prime or not 7. Read a Natural Number and check whether the number is Armstrong or not 8. Read n integers, store them in an array and find their sum and average 9. Read n integers, store them in an array and search for an element in the    array using an algorithm for Linear Search 10.Read n integers, store them in an array and sort the elements in t

PROGRAMMING IN C KTU EST 102 THEORY AND LAB NOTES

PROGRAMMING IN C  KTU  EST 102  THEORY AND LAB   COMMON FOR ALL BRANCHES About Me Syllabus Theory Syllabus Lab Model Question Paper EST 102 Programmin in C University Question Papers  and evaluation scheme   EST 102 Programming in C  Introduction( Lab) Introduction to C programming Linux History and GNU How to create a bootable ubuntu USB stick Installing  Linux Install Linux within  Windows Virtual Box and WSL Linux Basic Features and Architecture Basic Linux Commands Beginning C Programming Compiling C programs using gcc in Linux Debugging C program using gdb Module 1: Basics of computer hardware and software          Module-1 Reading Material Basics of Computer Architecture Hardware and Software System Software and Application Software  Programming Languages ( High level, Low level and Machine Language) and Translators ( Compiler, Interpreter, Assembler) Algorithm, Flowcharts and Pseudo code Program Development Structured Programming Basics of hardware ( video) Know about Motherboar

Arrays in C-single and multi dimensional arrays- list and matrix

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list(vector); A two dimensional array is like a table(matrix). We can have more dimensions. Always, Contiguous (adjacent) memory locations are used to store array elements in memory. Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element. Declaring  Single Dimensional Arrays Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for mentioning dimension of the array.  Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.  In C99, dimensions must still be positive integers, but variables can be used, so long as the variable has a positive value at the time the array is declared. ( Space is allo