Pointers and Arrays in C


Arrays in C, C++, and Java: Arrays behave mostly the same in C as in Java (and C++). These notes will point out the significant differences. First study the following:


Declaring local arrays (on the run-time stack):

Arrays in Java can only be declared dynamically, and we will learn later how to do this in C. In C and C++, but not in Java, arrays can also be declared just as local variables are -- at the start of a function. This takes the form:

Now somefunction has an array of 5 ints and an array of 15 doubles declared inside it. These arrays are created (allocated) as the function is called and are destroyed (deallocated) on function return. The size of the array (inside square brackets) has to be a constant known at compile time. This is the normal way in C to create an array.


Program Illustrating Arrays:


Indexes versus pointers for arrays in C:


Pointer Arithmetic:

Created By Dr. Neal Wager : The University of Texas at San Antonio