
Understanding Pointers in C Programming
Explore the fundamental concepts of pointers in C programming, including their role in achieving efficient code, working with complex data structures, and dynamic memory allocation. Learn about memory basics, address manipulation, dereferencing operators, and more from this information-rich content.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
COM101B LECTURE 12: POINTERS PART1 ASST. PROF. DR. HACER YALIM KELE REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
POINTERS The most sophisticated feature of C They lead to more efficient and compact code Pointers enable us to, achieve parameter passing by reference deal concisely and effectively with arrays represent complex data structures work with dynamically allocated memory REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
BASICS Memory is an oredered sequence of consecutively numbered storage locations A data item is tored in one or more adjacent locations in memory Every data item has a starting address the address of a data item is the address of its first storage locations The address can be stored in pointers and can be used to access data for reading for writing The address of an item is considered as a pointer to that item REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
ip is a pointer, which contains the address of the variable i: REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
ADDRESS AND DEREFERENCING OPERATORS C provides two unary operators: &: address of operator *: dereferencing operator & operator returns the address of an item * operator returns the content of the data item that is pointed to by a pointer it fetches the value of the item that is pointed by an address REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
DEREFERENCING Accessing an object through a pointer is called dereferencing & operator can be applied to only lvalues. &10 & C &(x+4) : invalid if type of an item is T, &T has a pointer to T type. int i; &i : pointer to int * operator can only be applied to pointes j = *ip + 10; //ip is a pointer to an integer REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
POINTER TYPE DECLARATION type *identifier; declares the identifier as a pointer to type Example: char* cp; // pointer to char int* p; // pointer to integer double * dp; // pointer to double REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
POINTER ASSIGNMENT A pointer may be assigned to another pointer of the same type Example: int i=1, j, *ip; ip = &i; j = *ip; *ip = 0; // assigns 0 to i REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
POINTER INITIALIZATION type *identifier = initializer; initializer needs to be an address of a previously defined data of appropriate type or NULL , i.e. 0. Example: #include <stdio.h> float* fp; short s; short *sp = &s; char c[20]; char* cp = &c[4]; REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
Example: #include <stdio.h> int main(){ int i,j=1; int *jp1, *jp2 = &j; jp1 = jp2; i = *jp1; *jp2 = *jp1 + i; printf( i=%d, j=%d, *jp1=%d, *jp2=%d\n ,i,j,*jp1,*jp2); return 0; } REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992
Result: i=1, j=2, *jp1=2, *jp2=2 REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992