|
|
|
|
|
Ordinary C arrays | Dynamic C arrays |
---|---|
#include <stdio.h> |
#include <stdio.h> |
Output | Output |
a[0]=1, a[1]=2, a[2]=5, a[3]=10 |
*(p+0)=1, *(p+1)=2, *(p+2)=5, *(p+3)=10 |
The two notations, index and pointer, are mostly interchangable, even in the same program. To illustrate this, I took the two programs above and interchanged the function definitions. Notice that I kept the prototypes the same, so they don't seem to match the function definition any more. Everything works perfectly, and even lint is happy with this code.
Functions Interchanged, Prototypes Not | |
---|---|
#include <stdio.h> |
#include <stdio.h> |
Output | Output |
*(p+0)=1, *(p+1)=2, *(p+2)=5, *(p+3)=10 |
a[0]=1, a[1]=2, a[2]=5, a[3]=10 |