380 likes | 494 Views
Friday, January 19, 2007. Anyone who has never made a mistake has never tried anything new. -Albert Einstein. new delete Static array: Size must be known at compile time int size; cin>>size; int array[size]; //Not allowed. new delete Static array: Size must be known at compile time
E N D
Friday, January 19, 2007 Anyone who has never made a mistake has never tried anything new. -Albert Einstein
new • delete Static array: Size must be known at compile time int size; cin>>size; int array[size]; //Not allowed
new • delete Static array: Size must be known at compile time int size; cin>>size; int array[size]; //Not allowed Dynamically allocated array: i.e On the fly, at runtime int size; cin>>size; int* array=new int[size]; //OK
pointers - dynamic allocation int size; cin>>size; int* array=new int[size]; int i; for (i=0; i<size; i++){ array[i]=2*i; } for (i=0; i<size; i++){ cout<<array[i]<<endl; } delete []array;
Calling Functions with Arrays • When an array is an argument to a function, only address of the first element of the array is passed,not a copy of the entire array. • Array name without an index is a pointer to first element in the array.
Calling Functions with Arrays void display(int num[]); void fill_in(int num[]); /* C++ will automatically convert it to an integer pointer */ int main() { int t[10],i; fill_in(t); display(t);//pass array t to a function return 0; }
Calling Functions with Arrays void fill_in(int num[]) { int i; for(i=0; i<10; i++) num[i]=i; } void display(int num[]) { int i; for(i=0; i<10; i++) cout << num[i] << ' '; }
Calling Functions with Arrays void display(int num[10]); void fill_in(int num[10]); /* C++ will automatically convert it to an integer pointer */ int main() { int t[10],i; fill_in(t); display(t);//pass array t to a function return 0; }
Calling Functions with Arrays void fill_in(int num[10]) { int i; for(i=0; i<10; i++) num[i]=i; } void display(int num[10]) { int i; for(i=0; i<10; i++) cout << num[i] << ' '; }
Calling Functions with Arrays void display(int *num) /* This method is most commonly used in professionally written C++ programs when dealing with single dimensional numeric arrays*/ { int i; for(i=0; i<10; i++) cout << num[i] << ' '; }
Calling Functions with Arrays void display(int *num); int main() { int t[10], i; fill_in(t); display(t); int *pt=t; display(pt); cout<<*pt; return 0; }
Calling Functions with Arrays void display(int *num) { int i; for(i=0; i<10; i++) cout << *(num++) << ' '; cout<<endl; }
SELF TEST: Calling Functions with Arrays void cube(int *n, int num); int main() { int i, nums[10]; for(i=0; i<10; i++) nums[i] = i+1; cout << "Original contents: "; for(i=0; i<10; i++) cout << nums[i] << ' '; cout << '\n'; cube(nums, 10); // compute cubes cout << "Altered contents: "; for(i=0; i<10; i++) cout << nums[i] << ' '; return 0; }
SELF TEST: Calling Functions with Arrays void cube(int *n, int num) { while(num) { *n = (*n) * (*n) * (*n); num--; n++; } }
SELF TEST: Functions void sqr_it(int *i); // prototype int main() { int x; x = 10; sqr_it(x); return 0; }//ERROR? void sqr_it(int *i) { *i = (*i) * (*i); }
2-Dimensional Arrays • 2-Dimentional Arrays as arguments to functions • column size must be declared in prototype
void print2D(int twoD[][2]){ int i,j; for(i=0; i<2; i++){ for(j=0; j<2; j++){ cout<<twoD[i][j]<<endl; } } twoD[1][1]=100; } int main(){ int twoD[2][2]={1,2, 3,4}; print2D(twoD); print2D(twoD); return 0; }
1 2 3 4 1 2 3 100
Initialization of strings char str1[80]="hello"; char str2[80]={"hello"}; char str3[80]={'h','e','l','l','o','\0'};
When the compiler encounters a string constant, it stores it in the program string table and generates a pointer to the string. char *s; s = "Pointers are fun to use.\n"; cout << s;
Strings What is wrong with following? char str1[80]; str1="cs192";
Strings What is wrong with following? char str1[80]; str1="cs192"; //Error Use strcpy(str1, “cs192”); We can also initialize at the time of declaration.
Arrays of Pointers int *pi[4]; int var=10; pi[2] = &var; /* assign address of an integer var to third element of pointer array */ To find value of var: *pi[2] Make a memory drawing!
Initializing Arrays of Pointers int main( ){ char *fortunes[5] = { "Soon, you will come into some money.\n", “You will see an old friend today.\n", "You will live long and prosper.\n", "Now is a good time to invest for the future.\n", "A close friend will ask for a favor.\n" }; cout<<fortunes[2];//what is the output??
Self Test: Arrays of Pointers int chance, input, i; cout << "To see your fortune, enter a random integer: "; cin>>input; // randomize the random number generator for (i=0; i<input; i++) rand(); chance = rand(); chance = chance % 5; cout << fortunes[chance]; return 0; }
Calling Functions with Arrays int main(void){ char* entries[]={ {"ambidextrous"}, {"fata morgana"}, {"infrangible"}, {"serene"}, {"tousle"} }; printentries(entries); return 0; }
Calling Functions with Arrays void printentries (char* ent[]){ int i; for(i=0; i<5; i++){ cout<<ent[i]<<endl; } } int main(void){ char* entries[]={ {"ambidextrous"}, {"fata morgana"}, {"infrangible"}, {"serene"}, {"tousle"} }; printentries(entries); return 0; }
const int rows=13, cols=4; int i; char *Harvest[rows][cols]= { {"unknown month", " ", " ", " "}, {"sugar beet","cabbage","cauliflower", "Brussels sprouts"}, {"turnip", "radish", "caraway", "carrot"}, {"apple", "plum", "pear", "bean"}, {"peas", "clover", "lettuce", "onion"}, {"oat", "asparagus", "barley", "rye"}, {"maize", "wheat", "rice", "sugar cane"}, {"cotton", "cocoa", "tea", "grape"}, {"raspberry","hazelnut", "garlic", "ginger"}, {"potato", "sesame", "rye", "green gram"}, {"olive", "peppermint", "millet", "parsely"}, {"root mustard", "tomato", "jojoba", "fig"}, {"pecan", "thyme", "lime", "parsnip"} };
cout<<Harvest[0]<<endl; cout<<Harvest[0][0]<<endl; cout<<Harvest[0][0][0]<<endl;
cout<<Harvest[0]<<endl; cout<<Harvest[0][0]<<endl; cout<<Harvest[0][0][0]<<endl; 0x0012FDE4 unknown month u
for (i=0; i<cols; i++){ cout<<Harvest[2][i]<<"\t"; } cout<<endl; for (i=0; i<cols; i++){ cout<<Harvest[7][i]<<"\t"; } cout<<endl;
turnip radish caraway carrot cotton cocoa tea grape
int dept1=50, dept2=60, dept3=100, dept4=30; int *production[2][2]= {{&dept1, &dept2}, {&dept3, &dept4} }; for (i=0; i<2; i++){ cout<<production[1][i]<<"\t"; } cout<<endl; for (i=0; i<2; i++){ cout<<*production[1][i]<<"\t"; }
0x0012FEB0 0x0012FEA4 100 30
void ptrprint2D(int *ptr2D[][2]){ int i,j; for(i=0; i<2; i++){ for(j=0; j<2; j++){ cout<<*ptr2D[i][j]<<endl; } }} int main(){ int a1=3, a2=4, a3=5, a4=6; int* ptrtwoD[2][2]={&a1, &a2, &a3, &a4}; ptrprint2D(ptrtwoD); return 0;}
3 4 5 6
char* myFunction(char *strA, char *strB){ while (*strB){ *strA=*strB; strA++; strB++; } return strA; } int main(void) { char stringa[]={"Rather Long String“}; char stringb[]={"bit short“}; char *pa=stringa; char *pb=stringb; char *pret; pret=myFunction(pa, pb); cout<<pa<<endl<<pb<<endl<<pret<<endl; return 0;} Functions that return pointers
bit shortng String bit short ng String