You are on page 1of 9

Pointers Simplified

Pointers, Arrays, Strings made easy


Author : Vikas Nagpal Learning Consultant B Tech (Computer Science), IITD

Agenda
Pointers Arrays Strings

What is a pointer?
Pointer is simply a variable holding an address of some other data E.g. struct node *ptr;
struct node

ptr

0x12354320

How to learn pointers effectively?


Learn Pointers, Arrays, Strings, Dynamic Memory allocation together Write lots of programs Use a memory purifier like valgrind, purify etc

Size of Pointers
Should it depend on the type of data pointed to? Contemplate on sizeof_ptr1.c

Pointer versus Array


What are the similarities? Differences?
char name[MAX_NAME_LEN] = Anuttara Learning"; char *ptr = name; /* Think : will sizeof() be same or different ? */ Refer Program ptr_vs_array_size.c

Pointer versus Array


What are the similarities? Differences?
char name[MAX_NAME_LEN] = Anuttara"; char *ptr = name; ptr++ ; /* Think: will it compile? */ name++; /* Think: will it compile? */ Refer Program ptr_vs_array_size.c

Array
What if less number of initializers?
int arr[6] = { 10, 20, 30};

/* Think : what is value of remaining 3 elements*/ printf("arr[3]=%d\n", arr[3]);

Refer Program arr_less_elements.c

Thank you
Note: It is only a sample, and does not cover the subject exhaustively Feedback invited at http://vikasnagpal.wordpress.com/

You might also like