Upload
isaiah-partin
View
212
Download
0
Embed Size (px)
Citation preview
Senem KUMOVA METİN CS115 2008-2009 FALL
1
POINTERS &&
ARRAYS
CHAPTER 6
Senem KUMOVA METİN CS115 2008-2009 FALL
2
POINTERS
POINTER is a programming language data type whose value refers directly to
(or "points to") another value stored elsewhere in the computer memory using its address
Senem KUMOVA METİN CS115 2008-2009 FALL
3
POINTERS int x= 5 is a variable and
5 is the value of itprintf(“value of x : %d”, x);
&x is the location or address of the xprintf(“address of x in memory : %d”, &x);
Pointers are declared in programs and then used to take addresses !!!
int x=5; // some stored valueint * p; // a pointer that can hold the address of an integerp=&x;
x= 5&x
….
….
…..
MEMORY
Senem KUMOVA METİN CS115 2008-2009 FALL
4
Pointers: & operator
55 0x123456780x12345678xxint int xx = = 55;;
pp
int *pint *p;;
pp = & = &xx;;
// adress of x // adress of x is // stored in pis // stored in p
0x123456780x12345678
p is a pointer: it contains the p is a pointer: it contains the address of a piece of dataaddress of a piece of data
MemoryMemory
AddressAddress
&x&x
Senem KUMOVA METİN CS115 2008-2009 FALL
5
POINTERSint * p; // A pointer that can hold an
address // of an integer integer pointer
int a =0;p= &a;
double * q // A pointer that can hold an address // of a double double pointer
double b=1.2;q= &b;
char * t // A char pointerchar c=‘D’;t = &c;
Senem KUMOVA METİN CS115 2008-2009 FALL
6
POINTERSmain(){ int * p; // integer pointer
int a =0;p= &a; // a equals to *p
printf(“%d \n”, a);printf(“%d \n”, *p);
*p=6;
printf(“%d \n”, a);printf(“%d \n”, *p);
a++;
printf(“%d \n”, a);printf(“%d \n”, *p); }
a=0
p=&a
*p
Senem KUMOVA METİN CS115 2008-2009 FALL
7
POINTERSint a=1, b=2, *p;
p=&a;
&a &b &p
a=1 b=2 ??will point somewhere in memory
&p
1 2 p=&a
&a &b
Senem KUMOVA METİN CS115 2008-2009 FALL
8
POINTERSb=*p;
// equivalent to b=a
&a &b &p
a=1 b=1
( the value of a )
p=&a
THE VALUE POINTED BY A POINTER CAN BE CHANGED BY
* name_pointer
*p
Senem KUMOVA METİN CS115 2008-2009 FALL
9
Pointers : EXAMPLE 1
#include <stdio.h>void main(void){
int i = 7, j=3 , *k, *m; k = &i; // k ??? *k ??? j=*k; // k ??? *k ???
m=&i; // i ??? *i ???(*m) ++; // m ??? *m ???printf(“ %d %d %d %d”, i, j, *k, *m);
}
Senem KUMOVA METİN CS115 2008-2009 FALL
10
Pointers : EXAMPLE 2
#include <stdio.h>void main(void){
char a = ‘D’; char *p = &a;
*p=*p+2;printf(“%c %c”, a, *p);
a=a+1;printf(“%c %c”, a, *p); }
Senem KUMOVA METİN CS115 2008-2009 FALL
11
CALL BY VALUE/* Whenever variables are passed as arguments to a function, their
values are copied to the function parameters */
void main(){ int a=20;
incr1 (a);printf(“%d: “, a);
incr2 (a);printf(“%d: “, a);}
void incr1(int x) { x++; }
void incr2(int a) { a++; }
R E M E M B E R !
Senem KUMOVA METİN CS115 2008-2009 FALL
12
CALL BY REFERENCE/* Pointers are passed as arguments to a function, their addresses are assigned to
the function parameters defined as pointers:*/
void main()
{ int a=20;
incr (&a);
printf(“%d: “, a); }
void incr(int * x)
{ (*x)++; }
Senem KUMOVA METİN CS115 2008-2009 FALL
13
CALL BY REFERENCE : Example 1
void addi(int *a, int b);
void main(){ int a=20, int b=30;
addi (&a , b)printf(“%d %d :“, a, b);}
void addi(int *a, int b) { int tmp = *a +b;
*a= tmp;b= b++;}
OUTPUT??
Senem KUMOVA METİN CS115 2008-2009 FALL
14
void main(){ int a=20; int b=30;
swap1 (a, b);swap2 (&a, &b);printf(“%d %d: “, a, b);}
void swap1(int x, int y) { int tmp;
tmp=x;x=y;y=tmp; }
void swap2(int *x, int *y) { int tmp;
tmp = *x; // get value pointed by x.*x = *y; // assign value pointed by y to x*y = tmp; }
CALL BY REFERENCE Example 2 : SWAP
Senem KUMOVA METİN CS115 2008-2009 FALL
15
What is an Array?
int grade0, grade1, grade2;
int grade [3];
Arrays make it possible to represent large number of homogenous values
grade0 grade1 grade2
grade[0] grade[1] grade[2]
Senem KUMOVA METİN CS115 2008-2009 FALL
16
One Dimensional Arrays type name_of_array[size]
EXAMPLE
int grade[5];
data
datagrade[1]
datagrade[2]
data
data
grade[0]
grade[3]
grade[4]
MEMORY
first element in array
fift ( the last) element in array
starts from 0
ends at size - 1
Senem KUMOVA METİN CS115 2008-2009 FALL
17
One Dimensional Arraysint grade[5], x=9, y;
grade[0]= 0; grade[3]= 4; grade[2]= -1;
grade[1]=grade[3]+grade[2];
grade[4]= x;
x= grade[2];y= grade[grade[3]];
03grade[1]-1grade[2]49
grade[0]
grade[3]grade[4]
MEMORY
x ?
y ?
Senem KUMOVA METİN CS115 2008-2009 FALL
18
INITIALIZATION float f1[]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration
without the size */
float f2[4]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration with size */
char z[] =“abc”; // char arrays char z[] = {‘a’, ‘b’, ‘c’, ‘\0’}
int a[80]={0};
Senem KUMOVA METİN CS115 2008-2009 FALL
19
EXAMPLE1:/* array1.c*/main(){
int x[10], k;
for(k=0; k<10; k++){
x[k]=0;printf(“x[%d] = %d\n”, k, x[k]); //what will be the output ???
}}
Senem KUMOVA METİN CS115 2008-2009 FALL
20
EXAMPLE2:/* array2.c*/main(){
int x[10], k;
for(k=0; k<10; k++){
x[k]=k;printf(“x[%d] = %d\n”, k, x[k]); //what will be the output ???
}}
Senem KUMOVA METİN CS115 2008-2009 FALL
21
EXAMPLE3:/* array3.c*/main(){
int x[10], k, sum=0;
for(k=0; k<10; k++)scanf(“%d”, &x[k]);
for(k=0; k<10; k++){ printf(“%d”, x[k]); sum=sum + x[k]; }
printf(“sum is %d”, sum);}
Senem KUMOVA METİN CS115 2008-2009 FALL
22
Relationship between Pointers and Arrays
/*An array name by itself is an adress or a pointer value */
int a[5] , *p;p=&a[0];
// Equals to
p=a;
Senem KUMOVA METİN CS115 2008-2009 FALL
23
Relationship between Pointers and Arrays
&a[0] equals to a then a[0] equals to *a&a[1] equals to a+1 then a[1] equals to *(a+1)&a[2] equals to a+2 then a[2] equals to *(a+2)………………………………………………………………………………&a[i] equals to a+i then a[i] equals to *(a+i)
EXAMPLE:int a [5]={1,2,3,4,5};int * p;printf(“%d”,a[0]); printf(“%d”,*a);printf(“%d”,a[2]); printf(“%d”,*(a+2));p=&a[4];P=a+4;
Senem KUMOVA METİN CS115 2008-2009 FALL
24
Relationship between Pointers and Arrays : Example1
main(){ int x[10], k;
for(k=0; k<10; k++){ x[k]=k;
printf(“%d\n”, x[k]); }
}
main(){ int x[10], k;
for(k=0; k<10; k++){ *(x+k)=k;
printf(“%d\n”, *(x+k)); }
}
Senem KUMOVA METİN CS115 2008-2009 FALL
25
Relationship between Pointers and Arrays : Example2
main(){ int x[10], k;
for(k=0; k<10; k++){
*(x+k)=0;printf(“%d\n”,
*x+k); }
}
main(){ int x[10], k;
for(k=0; k<10; k++){
*(x+k)=0;printf(“%d\n”,
*(x+k)); }
}
Senem KUMOVA METİN CS115 2008-2009 FALL
26
Pointer ArithmeticEXAMPLEdouble a[2],*p,*q;
p=a; // p=&a[0]q=p+1; // q=&a[0]+1= &a[1]
printf(“%d\n”, q-p); /* difference in terms of array elements */
printf(“%d\n”, (int) q-(int) p); /* difference in memory locations */
Senem KUMOVA METİN CS115 2008-2009 FALL
27
Arrays as Function Argumentsmain(){ int x[9], r;
r=sum(x,9); //sum(&x[0],9)
}double sum( int a[], int n){ int i;
double result =0.0;for (i=0; i<n; i++)
result+=a[i];return result;
}
main(){ int x[9], r;
r=sum(x,9); // sum(&x[0],9)
}double sum( int * a, int n){ int i;
double result =0.0;for (i=0; i<n; i++)
result+=*(a+i);return result;
}
Senem KUMOVA METİN CS115 2008-2009 FALL
28
Dynamic Memory Allocation
Calloc : contiguous memory allocation Malloc : memory allocation
Senem KUMOVA METİN CS115 2008-2009 FALL
29
CALLOC calloc(n, el_size) an array of n elements, each element
having el_size bytesint main(){ int *a; //will be used as an array
int n; // size of array....a=calloc(n, sizeof(int)); /* get space for a , and
initialize each bit to zero */free(a); /* each space opened dynamically must be returned */
}
Senem KUMOVA METİN CS115 2008-2009 FALL
30
MALLOC/* malloc does not initialize memory */int main(){ int *a; //will be used as an array
int n; // size of array....a=malloc(n*sizeof(int)); /* get space for a */....free(a);
}
Senem KUMOVA METİN CS115 2008-2009 FALL
31
DYNAMIC ARRAY : Example
main(){
int * x, k,s;printf(“give array size”);scanf(“%d”,&s);
x=malloc(s*sizeof(int));
for(k=0; k<s; k++) x[k]=k;for(k=0; k<s; k++) printf(“%d ”,x[k]);
}
Senem KUMOVA METİN CS115 2008-2009 FALL
32
NEXT WEEK
MULTIDIMENSIONAL ARRAYS
SORTING ALGORITHMS : BUBBLE SORT MERGE SORT
STRINGS && STRING_HANDLING FUNCTIONS
ARRAYS OF POINTERS
ARGUMENTS TO MAIN