More On Pointers
More On Pointers
More On Pointers
More on Pointers
14th September
IIT Kanpur
int *p;
int sample[10];
p = sample; // same as p = &sample[0];
int *p;
int sample[10];
p = sample;
p[5] = 100; // Both these statements
*(p+5) = 100; // do the same thing
e.g
int a[3][6][4][8];
int main() {
int x, *p, **q;
x = 10;
p = &x;
q = &p;
{program: pointers.c}
C Course, Programming club, Fall 2008 11
Dynamic Memory Allocation
• To allocate memory at run time.
• malloc(), calloc()
– both return a void*
• you’ll need to typecast each time.
char *p;
p = (char *)malloc(1000); /*get 1000 byte space */
int *i;
i = (int *)malloc(1000*sizeof(int));
int *i;
i = (int *)malloc(1000*sizeof(int));
.
.
.
free(i);