320 likes | 324 Vues
Pointers. Variable Memory Snapshot. int nrate = 10; The variable is stored at specific memory address A variable is nothing more than a convenient name for a memory location. Address Operator. A variable can be referenced using the address operator & example: scanf(“%f”, &x);
E N D
Variable Memory Snapshot • int nrate = 10; • The variable is stored at specific memory address • A variable is nothing more than a convenient name for a memory location
Address Operator • A variable can be referenced using the address operator & example: scanf(“%f”, &x); • This statement specifies that the value read is to be stored at the address of x
Pointer Variables • A pointer is a variable that holds the address of a memory location • If a variable p holds the address of another variable q, then p is said to point to q • If q is a variable at location 100 in memory, then p would have the value 100 (q’s address) 100 200 100 q p
How to declare a pointer variable • pointer variables are declared using an asterisk ( * ) The asterisk is called the indirection operator or the de-referencing operator). example: • int a, b, *ptr; ptr is a pointer to an integer • when a pointer is defined, the type of variable to which it will point must be specified. (i.e. a pointer defined to point to an integer cannot also point to a floating point variable.)
Example int *iPtr; double* dPtr; • the variable iPtr is declared to point to an int • the variable dPtr is declared to point to a double • neither variable has not been initialized in the above example • declaring a pointer creates a variable capable of holding an address
Why Pointers? • They allow you to refer to large data structures in a compact way • They facilitate sharing between different parts of programs • They make it possible to get new memory dynamically as your program is running • They make it easy to represent relationships between data items
Example a ? iPtr s dPtr int a, *iPtr;char* s;double *dPtr; ? ? ?
More about declaring pointers • When using the form int* p, q; the * operator does not distribute. • In the above example p is declared to be a pointer to int. q is declared to be an int.
Assigning values to a pointer • the assignment operator (=) is defined for pointers • the right operand can be any expression that evaluates to the same type as the left • the operator & in front of an ordinary variable produces the address of that variable. • The operator * in front of a pointer variable returns the contents stored at that address
Example int i=6, j; int *iPtr; iPtr = &i; j = *iPtr; i 6 iPtr j 6
Exercise name address memory int a, b; int *c, *d; a = 5; c = &a; d = &b; *d = 9; print c, *c, &c print a, b a 5 9 b c 1 d 2 c=1 *c=5 &c=4 a=5 b=9
Exercise Give a memory snapshot after each set of assignment statements int a=1, b=2, *ptr; ptr = &b; int a=1, b=2, *ptr=&b; a = *ptr;
NULL pointer • A pointer can be assigned or compared to the integer zero, or, equivalently, to the symbolic constant NULL, which is defined in <stdio.h>. • A pointer variable whose value is NULL is not pointing to anything that can be accessed
Example iPtr s dPtr int *iPtr=0;char *s=0;double *dPtr=NULL;
Pointer Assignments A pointer can point to only one location at a time, but several pointers can point to the same location. /* Declare and initialize variables. */ int x=-5, y = 8, *ptr1, *ptr2; /* Assign both pointers to point to x. */ ptr1 = &x; ptr2 = ptr1; The memory snapshot after these statements are executed is ptr1 ptr2 x y -5 8
Pointer Arithmetic • Four arithmetic operations are supported • +, -, ++, -- • only integers may be used in these operations • Arithmetic is performed relative to the variable type being pointed to Example: p++; • if p is defined as int *p, p will be incremented by 4 (system dependent) • if p is defined as double *p, p will be incremented by 8(system dependent • when applied to pointers, ++ means increment pointer to point to next value in memory
Accessing Arrays using Pointers #include <stdio.h> int main() { int a[5]={1,3,5,7,9}; int *ptr; int i=0; ptr = a; while (i<5) { printf("%d\n",*(ptr+i)); i++; } }
Comparing Pointers • You may compare pointers using relational operators • Common comparisons are: • check for null pointer (p == NULL) • check if two pointers are pointing to the same object • (p == q) Is this equivalent to • (*p == *q) • compare two pointers that are pointing to a common object such as an array.
Call by Value void swap(int a, int b) { int temp; temp = a; a = b; b = temp; return; }
Call by Value int x = 2, y = 3; printf("X = %d Y = %d\n",x,y); swap(x,y); printf("X = %d Y = %d\n",x,y); Changes made in function swap are lost when the function execution is over
Call by reference void swap2(int *aptr, int *bptr) { int temp; temp = *aptr; *aptr = *bptr; *bptr = temp; return; }
Call by reference int x = 2, y = 3; int *ptr1, *ptr2; ptr1 = &x; ptr2 = &y; swap2(ptr1,ptr2); printf("X = %d Y = %d\n",*ptr1,*ptr2); x = 2; y = 3; swap2(&x,&y); printf("X = %d Y = %d\n",x,y); Version 1 Version 2
Exercise • Write a function to compute the roots of quadratic equation ax^2+bx+c=0. void comproots(int a,int b,int c,double *dptr1, double *dptr2) { *dptr1 = (-b - sqrt(b*b-4*a*c))/(2.0*a); *dptr2 = (-b + sqrt(b*b-4*a*c))/(2.0*a); return; }
Exercise int a,b,c; double root1,root2; printf("Enter Coefficients:\n"); scanf("%d%d%d",&a,&b,&c); computeroots(a,b,c,&root1,&root2); printf("First Root = %lf\n",root1); printf("Second Root = %lf\n",root2);
Exercise • Implement a function increaseanddouble(…) which takes 2 integers and increases the value of first by 1 and doubles the value of the second x=3;y=5; … printf(“x=%d y=%d\n“,x,y); increaseanddouble(…); printf(“x=%d y=%d\n“,x,y); OUTPUT x=3 y=5 x=4 y=10
Dynamic Memory Allocation • Dynamically allocated memory is determined at runtime • A program may create as many or as few variables as required, offering greater flexibility • Dynamic allocation is often used to support data structures such as stacks, queues, linked lists and binary trees. • Dynamic memory is finite • Dynamically allocated memory may be freed during execution
Dynamic Memory Allocation • Memory is allocated using the: • malloc function (memory allocation) • calloc function (cleared memory allocation) • Memory is released using the: • free function • The size of memory requested by malloc or calloc can be changed using the: • realloc function
malloc and calloc • Both functions return a pointer to the newly allocated memory • If memory can not be allocated, the value returned will be a NULL value • The pointer returned by these functions is declared to be a void pointer • A cast operator should be used with the returned pointer value to coerce it to the proper pointer type • #include <stlib.h> to use these functions
Example of malloc int n = 6, m = 4; double *x; int *p; /* Allocate memory for 6 doubles. */ x = (double *)malloc(n*sizeof(double)); To free the space allocated use free() free(x); X
calloc (allocate memory for arrays) int m = 4; int *p; /* Allocate memory for 4 integers. */ p = (int *)calloc(m,sizeof(int)); p[0]=2; p[1]=3; To free the space allocated use free() free(x); p
Realloc (change size of memory) int m = 4; int *p; /* Allocate memory for 4 integers. */ p = (int *)calloc(m,sizeof(int)); /* change the size of memory block pointed by p */ q = realloc(p,2*m*sizeof(int)); To free the space allocated use free() free(x); p