Pointers vs. Multi-dimensional Arrays in C

By: Charles Emailed: 1650 times Printed: 2121 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Newcomers to C are sometimes confused about the difference between a two-dimensional array and an array of pointers. Given the definitions
   int a[10][20];
   int *b[10];
then a[3][4] and b[3][4] are both syntactically legal references to a single int. But a is a true two-dimensional array: 200 int-sized locations have been set aside, and the conventional rectangular subscript calculation 20 * row +col is used to find the element a[row,col]. For b, however, the definition only allocates 10 pointers and does not initialize them; initialization must be done explicitly, either statically or with code. Assuming that each element of b does point to a twenty-element array, then there will be 200 ints set aside, plus ten cells for the pointers. The important advantage of the pointer array is that the rows of the array may be of different lengths. That is, each element of b need not point to a twenty-element vector; some may point to two elements, some to fifty, and some to none at all.

Although we have phrased this discussion in terms of integers, by far the most frequent use of arrays of pointers is to store character strings of diverse lengths, as in the function month_name. Compare the declaration and picture for an array of pointers:

   char *name[] = { "Illegal month", "Jan", "Feb", "Mar" };

with those for a two-dimensional array:

   char aname[][15] = { "Illegal month", "Jan", "Feb", "Mar" };

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

could you guys maybe give an example using a 3 dimensonal array
and a 3rd dgree pointer i.e ***pointer vs array[][][]


View Tutorial          By: Steve at 2011-06-08 12:14:13

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Charles
arraycopy example in Java
Action listeners in JSF
Automatically Including Preludes and Codas in JSP
Using realloc() Function in C
Handling multiple buttons in HTML Form in Struts
Fopen and Getc implementation program in C
Pointers to Structures example program in C
Pointers vs. Multi-dimensional Arrays in C
getch and ungetch in C
Bitwise Operators in C
Variables and Arithmetic Expressions in C
Differences between JavaServer Faces technology and Struts
assert() example program in C++
'double buffering' Sample program in J2ME
Converting Default Function Arguments in C++ to Java

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Open, Creat, Close, Unlink system calls sample program in C
Using memset(), memcpy(), and memmove() in C
UNIX read and write system calls sample program in C
Printing a simple histogram in C
Using free() Function in C
perror() Function - example program in C
Character Arrays in C
External Variables and Scope in C
Listing Files and Directories sample program in C
Using realloc() Function in C
File Inclusion in C
lseek() sample program in C
typedef example program in C
Error Handling - Stderr and Exit - sample program in C
Using calloc() Function in C
Most Emailed Articles (in C)
Multi-dimensional Arrays in C (Explained using date conversion program)
Macro Substitution using #define in C
Open, Creat, Close, Unlink system calls sample program in C
Formatting with printf in C
Unicode and UTF-8 in C
Character Arrays in C
Pointer Arrays and Pointers to Pointers in C
UNIX read and write system calls sample program in C
Symbolic Constants using #define in C
Arguments - Call by Value in C
File Inclusion in C
Pointers vs. Multi-dimensional Arrays in C
Using Bit-field in C
lseek() sample program in C
Fopen and Getc implementation program in C