190 likes | 478 Views
Pointers. CSE 2451 Matt Boggus. Language comparison. C has pointers Java has references C++ has pointers and references. Pointers. Values of variables are stored in memory, at a particular location A location is identified and referenced with an address
E N D
Pointers CSE 2451 Matt Boggus
Language comparison • C has pointers • Java has references • C++ has pointers and references
Pointers • Values of variables are stored in memory, at a particular location • A location is identified and referenced with an address • Analogous to identifying a house’s location via an address • Pointer address • Address vs. contents • Pointer variable • Value holds the address of a memory location
Pointers and Addresses • A byte-addressable machine has consecutively numbered or addressed memory cells (bytes) which may be manipulated individually or in contiguous groups. char * ip1; char ip1 short int/long ip2 double double * ip2;
Pointers and Addresses • a pointer array holding addresses of characters; • char * a[2]; • A pointer array holding addresses of integers; • int * b[2];
Pointer syntax • * is used in the declaration of a pointer • int *p -> pointer to an integer • & (unary operator) gives the “address of” an object • p = &c means the address of c is assigned to the variable p
Pointer syntax • * (unary operator) is a dereferencing operator when applied to pointers • Access the variable the pointer points to • * in front of a pointer variable means “get the value at that address” i.e. “contents of” • int a = *p • Get the value at the address designated by p and assign it to a • *p = 1 • Assign the value of 1 to the memory location designated by the address of p
Declaring pointers • int *a; • int* a; • Use the first style, second leads to mistakes • int*b, *c, *d • int*b, c, d • int a = 10; int *p = &a; OR int *p; p = &a;
Pointers and Addresses • “&” vs “*” • “&” is used on a variable (can be char, int, double, array element) to get the address: int i, *ip1, *ip2, a[8]; char c, *cp; ip1 = &i; ip2 = &a[0]; cp = &c;
Pointers and Addresses • “&” vs “*” • “&” can not be used on a constant, array name, register variable and expressions: char * ip1; register int a; char b[10]; int * ip2; ip1=&’y’; /* WRONG */ ip1=&”abcdef”; /*WRONG*/ ip1=“abcedf”; /*RIGHT*/ ip2=&7; /* WRONG */ ip2=&a; /*WRONG*/ ip1=b; /*RIGHT */ ip1=&b; /*WRONG */ ip2=&(ip1+2); /*WRONG*/
Pointers and Addresses • “&” vs “*” • * is used on a pointer to access the variable it points to. int x =1,y[2]={2,3}; int * ip1,*ip2,*ip3; ip1=&x; *ip1=0; //x=0; printf(“%d\t%d\n”,x,y[0]); ip2=&y[0]; *ip2=1; //y[0]=1; printf(“%d\n”,y[0]); ip3=y; //ip3=&y[0]; *ip3=0; //y[0]=0; printf(“%d\n”,y[0]);
Pointers and Addresses • “&” vs “*” • “* pointer” has a datatype which is the same as that of the variable the pointer points to. • int *a; • every pointer points to a specific data type except void *; • void * is used to hold the address of any data type.
Pointers and Function Arguments • Still pass-by-value with pointers • Dereferencing the value of the pointer gives a memory address • The memory’s content can change • Note: • Note argument types when passing parameters • swap (int*, int *); • swap(&x,&y); /*pass addresses of two integer variables*/
Address Arithmetic • Pointers can use arithmetic operations int *ip1, *ip2, a[8]; ip1 = &a[7]; ip2 = &a[0]; What about: ip2 – ip1 + 1 ip1 – ip2 ? ip1 == NULL? ip1 > ip2 ?
Pointer example 1 #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j); return 0; }
Pointer example 2 #include <stdio.h> #include <stdlib.h> main() { int x, *p; p = &x; *p = 0; printf("x is %d\n", x); printf("*p is %d\n", *p); *p += 1; printf("x is %d\n", x); (*p)++; printf("x is %d\n", x); return 0; }
Pointer example 3 #include <stdio.h> int main(void) { char ch = 'c'; char *chptr = &ch; int i = 20; int *intptr = &i; float f = 1.20000; float *fptr = &f; char *ptr = "I am a string"; printf("\n [%c], [%d], [%f], [%c], [%s]\n", *chptr, *intptr, *fptr, *ptr, ptr); return 0; }
Pointers and Function Arguments • C passes arguments to functions by value; • swap(int x, int y); /* change local copies of x, y*/ • swap(int * x, int * y); /*use pointer to change the content of two variables */
Parameter Passing -- Example void swap (int x, int y) { int temp; temp = x; x = y y = temp; } void swap (int *x, int *y) { int temp; temp = *x; *x = *y *y = temp; }