tuesday, january 23, 2007 "we can't solve problems by using the same kind of thinking we...

Post on 19-Dec-2015

220 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Tuesday, January 23, 2007

"We can't solve problems by using the same kind of thinking we used when we created them."

-Albert Einstein

Handout 2Assignment 4

int* function_returns_ptr(int *a){

return (a+4);

}

int main(void){

int array[8]={1,2,3,4,5,6,7,8};

int *ptrArray=array;

int *result=function_returns_ptr(ptrArray);

cout<<*result<<endl;

return 0;

}

int* function_returns_ptr(int *a){

return (a+4);

}

int main(void){

int array[8]={1,2,3,4,5,6,7,8};

int *ptrArray=array;

int *result=function_returns_ptr(ptrArray);

cout<<*result<<endl; //prints 5

return 0;

}

What is wrong here?

int* function_returns_ptr(void){

int array[8]={1,2,3,4,5,6,7,8};

int *ptrArray=array;

return (ptrArray+4);

}

int main(void){

int *result=function_returns_ptr();

cout<<*result<<endl;

return 0;

}

What is wrong here?

int* function_returns_ptr(void){

int array[8]={1,2,3,4,5,6,7,8};

int *ptrArray=array;

return (ptrArray+4); //dangerous

}

int main(void){

int *result=function_returns_ptr();

cout<<*result<<endl;

return 0;

}

What is wrong here?

int* f2_returns_ptr(int *a, int *b){

int c=*a+*b;

int d=*a-*b;

if (c>d)

a=&c;

else

a=&d;

return a;

}

What is wrong here?

int* f2_returns_ptr(int *a, int*b){

int c=*a+*b;

int d=*a-*b;

if (c>d)

a=&c;

else

a=&d;

return a; //very serious mistake

}

char *get_substr(char *sub, char *str);

int main() {

char *substr;

substr = get_substr("two", "one two three four");

if(substr)

cout << "substring found: " << substr<<endl;

else

cout << "substring not found"<<endl;

return 0;

}

Functions that return pointers

char *get_substr(char *sub, char *str) {

int t;

char *p, *p2, *start;

for(t=0; str[t]; t++) {

p = &str[t]; // reset pointers

start = p;

p2 = sub;

while(*p2 && *p2==*p) { // check for substring

p++;

p2++;

} /* If at end of p2 (i.e., substring), then a match has been found. */

if(!*p2)

return start; // return pointer to beginning of substring

}

return '\0'; }// no match found

substring found: two three four

int strcmp ( const char *s1, const char *s2 );

strcmp will accept two strings. It will return an integer.

This integer will either be:

Negative if s1 is less than s2.Zero if s1 and s2 are equal. Positive if s1 is greater than s2.

Strings

char *strcat ( char *dest, const char *src ); strcat is short for string concatenate, which means to add to the end, or append. It adds the second string to the first string.

It returns a pointer to the concatenated string.

Beware this function, it assumes that dest is large enough to hold the entire contents of src as well as its own contents.

Strings

char *strcpy ( char *dest, const char *src );

strcpy is short for string copy, which means it copies the entire contents of src into dest.

It returns a pointer to the copied string.

The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0.

Strings

size_t strlen ( const char *s );

strlen will return the length of a string, minus the terminating character ('\0').

Treat size_t as an integer that cannot be negative.

Strings

char str1[80]={"one"}, str2[80]={"twenty"};

char *str3=strcpy(str1,str2);

cout<<str1<<endl;

cout<<str2<<endl;

cout<<str3<<endl;

A function can be declared to return any valid data type, except that a function cannot return arrays.

Example - PalindromesA palindrome is a word (noon) , sentence (Draw a level award) or number (18781) that reads the same backward or forward. Write a function that takes a string as input and determines if the string is a palindrome. (Note: white space is ignored when determining if a word, sentence or number is a palindrome.)

Self Test: do it with pointers (account for white spaces also)int is_palindrome(char string[]) { int is_pal, begin=0, end; end = strlen(string) -1; is_pal = 1; while(is_pal && begin <=end){ if(string[begin] != string[end])

{ is_pal = 0; } begin++; end--; }

return(is_pal); }

References

Safer and simpler use of memory addresses are references

When a reference parameter is used, the address of an argument is automatically passed to the function.

Within the function the operations on reference parameter are automatically de-referenced.

With call by reference, the caller gives the called function the ability to directly access the caller’s data and to modify that data if the called function so chooses.

To indicate that a function parameter is passed by reference, simply follow the parameter’s type in the function prototype by an ampersand &.

e.g. void myFunction( int &x);

Calling Functions

int squareByValue(int a);

void squareByReference(int &aref);

Calling Functions

int main() {

int x = 2, z = 4;

cout << "x = " << x << " before squareByValue" ;

squareByValue(x) ;

cout<< "x = " << x << " after squareByValue" ;

cout << "z = " << z << "before squareByReference”;

squareByReference(z);

cout << "z = " << z << " after squareByReference";

return 0;

}

Calling Functions

int squareByValue(int a)

{

return a *= a; //caller's argument not modified

}

void squareByReference(int &cRef)

{

cRef *= cRef; // caller's argument modified

}

Calling Functions

Function to swap two numbers

top related