Begin the session by explaining the objectives of the session.
Pointers contain addresses of variables of a specific type. Hence, the pointer and the variable to which it is pointing should be of the same type. The number of bytes used by the pointer to hold an address does not depend on the type of the variable the variable it is pointing to. The number of bytes used by a pointer is generally 4 bytes, or 2 bytes in some implementations. Type casting may be used to initialize a pointer with the address of a variable of a different type. For example, the following code is valid : char *ptr ; int i = 4 ; ptr = (char *) (&i) ; though this is seldom done. Pointers to type void can contain addresses of any data. It does not make sense to do any arithmetic with these pointers.
Use this slide to test the student’s understanding on declaring pointers.
Preceding a pointer with ampersand will yield the address of the pointer itself, and can be stored in a pointer to a pointer. Consider the following code: char *ptr, **pptr, chr; ptr = &chr; /* address of a character */ pptr = &ptr; /* address of a pointer */
Use this slide to test the student’s understanding on declaring and initializing pointers.
Generally, only the operators + and – are used with pointers. This results in the address in the pointer getting altered by a value equal to the size of the data type it is associated with. The following declaration is for a pointer to an array of 10 elements: char (*ptr) [10] ; /* note, it is not an array of pointers */ If the pointer ptr is initialized and contains the address 100 , ptr++ results in the address in ptr being scaled by 10. ptr will now contain 110.
Use this slide to test the student’s understanding on pointers arithmetic.
Use this slide to test the student’s understanding on one-dimensional arrays and pointers.
Use this slide to test the student’s understanding on 2-D arrays and pointers.
Use this and the next slide to summarize the session.