Top Banner
Programming Review: Functions, pointers and strings
27

Programming Review: Functions, pointers and strings.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Review: Functions, pointers and strings.

Programming

Review:

Functions, pointers and strings

Page 2: Programming Review: Functions, pointers and strings.

Pointers

int nums[] = {1, 2, 3};

char str[] = "moshe";

int* q = nums;

char* p = str;

1 2 3

q

num m o s h e \0

p

str

Page 3: Programming Review: Functions, pointers and strings.

Pointers

int nums[] = {1, 2, 3};

char str[] = "moshe";

int* q = nums;

char* p = str;

1 2 3

(q+1) (p+3)

m o s h e \0str

Page 4: Programming Review: Functions, pointers and strings.

Pointers

p[i] *(p+i)

1 2 3 m o s h e \0

q p

q[1] q[2]q[0] p[1] p[4]

p[0] *p

Page 5: Programming Review: Functions, pointers and strings.

Pointers and Functions

If we want to change a variable inside a function, we must pass it a pointer to the variable (its address)

The function will “fill” this address with the right value

Example: Swap

void swap(int *x, int *y){ int temp = *x; *x = *y; *y = temp;}

Page 6: Programming Review: Functions, pointers and strings.

Pointers and Functions

Variables that are defined inside the function

“die” when the function ends!!!

char* func(){ char str[LENGTH + 1];

...

return str;}

str doesn’t exist outside the function’s body

Page 7: Programming Review: Functions, pointers and strings.

1.What is wrong here?

int main()

{

int n = 3;

multBy3(n);

printf(“n=%d”,n);

}

void multBy3(int n)

{

int num = n;

num *= 3;n = num;

}

Page 8: Programming Review: Functions, pointers and strings.

2.What is wrong here?

int main()

{

int n = 3;

multBy3(&n);

printf(“n=%d”,n);

}

void multBy3(int *n)

{

int num = n;

num *= 3;n = num;

}

Page 9: Programming Review: Functions, pointers and strings.

3.What is wrong here?

int main()

{

int n = 3;

multBy3(&n);

printf(“n=%d”,n);

}

void multBy3(int *n)

{

int num = *n;

num *= 3;n = num;

}

Page 10: Programming Review: Functions, pointers and strings.

4.What is wrong here?

int main()

{

int n = 3;

multBy3(&n);

printf(“n=%d”,n);

}

void multBy3(int *n)

{

int num = *n;

num *= 3;n = #

}

Page 11: Programming Review: Functions, pointers and strings.

References

1. Define a variable in the main

2. Pass its address to the function

3. The function fills the address with a value

4. The main can use it as a normal variable

int main() {

int num;

}

Page 12: Programming Review: Functions, pointers and strings.

References

1. Define a variable in the main

2. Pass its address to the function

3. The function fills the address with a value

4. The main can use it as a normal variable

Int main() {

int num;

multBy3(&num);

}

Page 13: Programming Review: Functions, pointers and strings.

References

1. Define a variable in the main

2. Pass its address to the function

3. The function fills the address with a value

4. The main can use it as a normal variable

void multBy3(int *n) {

(*n) *=3;

}

Int main() {

int num;

multBy3(&num);

}

Page 14: Programming Review: Functions, pointers and strings.

References

1. Define a variable in the main

2. Pass its address to the function

3. The function fills the address with a value

4. The main can use it as a normal variable

Int main() {

int num;

multBy3(&num);

printf(“num=%d”,num);

}

Page 15: Programming Review: Functions, pointers and strings.

Exercise with pointers and strings

Implement the following function:

char* str_any(char *str1, char *str2); Input – two strings str1, str2 Output – pointer to the first occurrence in str1 of

any of the characters contained in str2

Page 16: Programming Review: Functions, pointers and strings.

Exercise (cont.)

Write a program that accepts a string from the userand replaces all punctuation signs (,.;:!?) with spaces

Page 17: Programming Review: Functions, pointers and strings.

Solution (str_any.c)

char* str_any(char* str1, char* str2){ while (*str1 != '\0') { if (strchr(str2, *str1) != NULL) { return str1; } ++str1; }

return NULL;}

Page 18: Programming Review: Functions, pointers and strings.

Solutionint main(void){ char* punc = ".,;:!?"; char s[MAX_LENGTH + 1]; char *p;

printf("Please enter a line of text\n"); scanf("%100s", s);

for (p = str_any(s, punc); p != NULL; p = str_any(p + 1, punc)) { *p = ' '; }

printf("Resulting string is:\n%s\n", s);

return 0;}

Page 19: Programming Review: Functions, pointers and strings.

Command line arguments

Command line arguments are arguments for the main function

Recall that main is basically a function It can receive arguments like other

functions The ‘calling function’ in this case is the

operating system, or another program

Page 20: Programming Review: Functions, pointers and strings.

‘main’ prototype

When we want main to accept command line arguments, we must define it like this argc holds the number of arguments that were

entered by the caller argv is an array of pointers to char – an array of

strings – holding the text values of the arguments

The first argument is always the program’s name

int main(int argc, char* argv[])

Page 21: Programming Review: Functions, pointers and strings.

‘main’ prototypeint main(int argc, char* argv[])

argc : 3

argv :

178\0

progname\0

text\0

Page 22: Programming Review: Functions, pointers and strings.

Example/* This program displays its command-line arguments */

#include <stdio.h>

int main(int argc, char *argv[]){

int i;

printf("The program's command line arguments are: \n");

for (i = 0; i < argc; ++i) {printf("%s\n", argv[i]);

}

return 0;}

Page 23: Programming Review: Functions, pointers and strings.

Specifying the arguments

In Visual Studio:Project Settings Debug, in the ‘program arguments’ field

Page 24: Programming Review: Functions, pointers and strings.

Specifying the arguments

We can also specify the arguments directly, by using the Windows console (StartRun…, then type ‘cmd’ and drag the executable into the window. Then type the arguments and <Enter>)

Page 25: Programming Review: Functions, pointers and strings.

Helper functions – atoi/atof

Command line arguments are received in the form of strings

These functions are used when we want to transform them into numbers

For example – atof(“13.5”) returns the number 13.5.

Must #include <stdlib.h>

int atoi(char s[]);double atof(char s[]);

Page 26: Programming Review: Functions, pointers and strings.

Exercise

Write a program that accepts two numbers as command line arguments, representing a rectangle’s height and width (as floating-point numbers).

The program should display the rectangle’s area and perimeter

Page 27: Programming Review: Functions, pointers and strings.

Solution (args_rectangle.c)int main(int argc, char* argv[]){ double width, height;

if (argc != 3) { printf("Wrong number of arguments!\n"); return 1; }

width = atof(argv[1]); height = atof(argv[2]);

printf("The rectangle's area is %g\n", width * height); printf("The rectangle's perimeter is %g\n", 2 * (width + height));

return 0;}