Understanding Pointers in C Programming: Concepts, Syntax, and Usage
This document explores the fundamental concept of pointers in the C programming language, highlighting their significance and complexity. It explains how pointers contain the memory address of variables, details the structure of variable attributes, and provides syntax for pointer declarations and initializations. Additionally, examples of using the `scanf` function for input and demonstrating pointer functionalities are included, illustrating how to effectively manage variables and memory addresses in C programming.
Understanding Pointers in C Programming: Concepts, Syntax, and Usage
E N D
Presentation Transcript
Pointer ? • One of the features of the C language that gives C language its sophistication, power, and complexity • Every byte in computer memory has an address and the address of the first allocated byte of the variable is called a pointer to the variable • a variable that holds an address is called a pointer variable.
65 is the ASCII code for letter A ch address is 1060 1060 65 1 byte allocated to ch 1061 num address is 1062 1062 10 1063 2 byte allocated to num 1064 Conceptual View of a variable in Memory int num =10; char ch = ‘A’; Memory
Address operator, & • To obtain the address of a specifies variable • Examples int num = 10; char ch = ‘A’; &num - address of the variable num called pointer to num &ch - address of the variable ch called pointer to ch NOT VALID - & 60, & ‘A’, & (K+2), &(Index/6)
More about variables • Every variable is associated with 5 attributes • variable type • variable name • variable contents • variable address • variable size
type(int) name(num) size is 2 bytes sizeof(int) contents(value) Address is 1062 (&num) Attributes of a Variable int num = 10; 1060 1061 1062 1063 1064
Examples #include <stdio.h> int main( ) { int num; num = 10; printf(“The address of num is %p\n”, &num); return 0; }
Input Function scanf( ) • Used to read input data from the keyboard and store it in the specified storage. • One of the function in stdio.h library • Syntax scanf(“control string”, arg1, arg2,….) • Examples int num; /* declare variables */ scanf(“%d”, &num); /* read from keyboard */
declaration statement conversion specification address of (pointer to) num assumed num memory location scanf ( ) function int num; scanf(“%d”, &num); num 1062
Examples /* Definition: This program asks for a name and 2 integer numbers. It displays the name and sum, product of the numbers. */ #include <stdio.h> /* include the stdio.h header file */ int main ( ) { int num1, num2 ; /* declare the variables */ printf ( " \nEnter two numbers and press the Enter key: " ) ; /* read 2 integer numbers from the keyboard */ scanf ( "%d %d", &num1, &num2 ) ; printf ( " \n %d + %d = %d \n ", num1, num2, num1+ num2 ) ; printf ( " \n %d - %d = %d \n ", num1, num2, num1 - num2 ) ; printf ( " \n %d * %d = %d \n ", num1, num2, num1 * num2 ) ; return 0 ; }
Program Output Enter two numbers and press the Enter key : 10 20 10 + 20 = 30 10 - 20 = -10 10 * 20 = 200
Conversion specification conversion description Example %c interpret input as a character scanf(“%c”, &ch); %d or %I interpret input as a signed integer scanf(“%d”, &inum); %f interpret input as floating-point scanf(“%f”,&fnum); number %e interpret input as floating-point scanf(“%e”,&fnum); number, e notation %s interpret input as a character string scanf(“%s”,name);
Character Strings • C doesn’t provides data type for strings • declare an array of char type to store character strings • Examples char Last_Name [40]; scanf(“%s”, Last_Name);
x x \0 String x character ‘x’ the character x “x” the string x null character, terminating the string
Program Example /* Definition: This program asks for a name and displays the name with a smile. */ #include <stdio.h> int main ( ) { char name [ 40 ] ; /* declare the variables */ printf ( " \nEnter your name and press the Enter key: " ); scanf ( "%10s", name ) ; /* read name from the keyboard */ printf ( " \n :-) Hi %s! \n ", name ) ; /* display the last name */ return 0; } Program Output Enter your name and press the Enter key : Unimas :-) Hi Unimas !
Formatting Input Data char name [40]; scanf(“%10s”,name); in this case scanf() stops after reading 10 characters or encountering a whitespace character, whichever comes first
Conversion Specification Char ch, name[40]; int inum; float fnnum; Statement Input data Read by scanf( ) scanf(“%s”, name); Unimas Sarawak Unimas scanf(“%9s”, name); Unimas Sarawak Unimas scanf(“%4s”, name); Unimas Sarawak Unim scanf(“%3d”,&inum); 123456 123 scanf(‘%3.3f”,&fnum); 123.3 123.300
Pointer Declarations Syntax data type *identifier • data type is any valid C data type such as int, or float • *(asterisk) the pointer indicator • identifier is any valid variable name Examples int *pint; /* declare a pointer variable of int type */
More example short *sp; /* the pointer variable sp points to short integer type */ int *ip; /* the pointer variable ip points to an integer type */ char *cp; /* the pointer variable cp points to a character type */ float *fp; /* the pointer variable fp points to a floating point type */ double *dp; /* the pointer variable dp points to a double precision type */
Pointer Initializations VALID int num; int *pnum=# NOT VALID int *pnum=# int num;
pnum num num address 2 bytes are accessed Storing Addresses int *pnum, num; /*declaring a pointer and an int type variables */ pnum = # /*assigning the address of the variable num to the pointer variable pint */ declaration statements storing the address of num in pnum int *pnum; int num; pnum=#
Indirection Operator * • Used for manipulating data by referring to variables by their addresses rather than their names. • Called indirection or dereferencing operator, is placed before a pointer variable name to obtain the value of the object that the pointer is pointing to • indirection operator * is a unary operator and can only be applied to pointer variables • used both to declare a pointer variable and to access the variable whose address is stored in a pointer
Examples int *pnum; /* declare a pointer variable */ int num=100, value; /*declare int type variables */ pnum = # /*assign the address of the variable num to the pointer variable pnum */ value = *pnum; /*use the indirection operator to obtain the contents of the variable that is pointed to by pnum */
Address 2000 2002 100 2004 2006 2002 2008 2120 Conceptual View Declaration and initialization statements int num = 100; int *pnum; storing the address of num in pnum pnum=# ppnum=&pnum num &num pnum *pnum &pnum ________________________________________ 100 2002 2002 100 2008
Direct and Indirect Methods of Accessing a Variable /* direct method */ /* pointer method */ int num 5; int num = 5; int value; int value; int *pnum = # value = num; value = *pnum;
Common Errors • Attempting to obtain the address of a constant value and constant value doesn’t have an address ptr = &45; /* invalid assignment */ ptr = &(Index + 10); /* invalid assignment */ • initializing pointer incorrectly int *ptr = 5; /* invalid initialization */
Multiple Indirection • Pointer point to another pointer - multiple indirection • have to declare a pointer to pointer variable before we can use it int **newpointer Example int num, *pnum, **ppnum; num=100; /* store 10 in num */ pnum=# /* store num address in pnum */ ppnum=&pnum; /* store pnum address in ppnum */
Address 2000 100 num 2002 2004 pnum 2006 2000 2008 2006 2010 ppnum Conceptual View Declaration and initialization statements int num = 100; int *pnum, **pnum; storing the address of num in pnum, and address of pnum ppnum pnum = # ppnum = &pnum; num &num pnum *pnum **ppnum *ppnum &pnum &ppnum 100 2000 2000 100 100 2006 2006 2010