2-D Array Of Characters
These are similar to the 2-D array of integers For EX:- In arr ,2 are the number of rows and 3 defines the size of each array. Here array can be of Integer type or character no matters. Below is the program shows the property of two dimension array of character.
printf(“%s by 1st methodn”,&name[i]);
printf(“%s by 2nd methodn”,name);
Here you can see how we declare and define 2-D array of character name by two methods,where first method is simple method of declaration while second method is known as arrays of pointers to String. Both methods have their own advantages and disadvantages. Let us discuss them one by one.
Simple Method :
Here we declare and define an array of character name as
char name, it says that length of each name is 10 NOTE:Where it is not mandatory to define the number names/rows.
Upper declaration is same mean as char name,if total number of names in 2-D array are 5.
These are same as we deal matrices/matrix in mathematics .
As we can see length of each array is 10 in above method while it is not necessary that each name contains 10 character FOR EX: kush has only 4 characters ,it means 6 bytes go waste. Similarly ,for each name,there is some amount of wastage.
This can be avoided by the below method:
Array of Pointers to String:
As we tell you in previous post that pointer variable always contains an address ,so that array of pointer contains number of addresses .
FOR EX: In above code declaration of char *names, names is an array of pointer. It contain starting address of each name in array i.e., base address of ravi is stored in name.
We can’t use scanf() function to receive and store the string in array of pointers to string. For this we use another function malloc(). We will tell you about this function in our next post.
Previous C Programming Next