• Non ci sono risultati.

PROGRAMMAZIONE PROCEDURALE

N/A
N/A
Protected

Academic year: 2021

Condividi "PROGRAMMAZIONE PROCEDURALE"

Copied!
16
0
0

Testo completo

(1)

PROGRAMMAZIONE PROCEDURALE

A.A. 2020/2021

(2)

WHY POINTERS?

(3)

WHY POINTERS

A pointer is a reference to a data object or a function.

Pointers have many uses:

 defining “call-by-reference” functions, and

 implementing dynamic data structures such as chained lists and trees, to name just two examples.

Very often the only efficient way to manage large

volumes of data is to manipulate not the data itself, but pointers to the data.

If you need to pass a large array to a function, it’s more

economical to pass a pointer to the array than to pass the

record contents, even if the function doesn’t modify the

contents.

(4)

EXAMPLE

#include <stdio.h>

int fun(int b) { return (b+1);

}

int main(){

int a= 3;

a= fun(a);

}

a b

34 3

Call by value

(5)

EXAMPLE

#include <stdio.h>

void fun(int a[], long int n) { for ( int i = 0; i < n; ++i )

a[i] = i;

}

int main(){

int a[1000000];

printf(“%lu”, sizeof(a));

fun(a, 1000000);

} This array is 4000000 bytes

a[0]=?

a[0]=? a[1]=?a[1]=? …… a[999999]=

?

a[999999]=

?

Call by reference

0

0 11 …… 999999999999

(6)

CALL BY VALUE AND

CALL BY REFERENCE

(7)

CALL BY REFERENCE

Call by reference is an easy way to work in the same memory segment from different functions

How to program a function that swaps the value of two variables?

a == 6 b == 10

a == 10 b == 6

int a= 6;

int b= 10;

a= b;

b= a;

int a= 6;

int b= 10;

int temp= 0;

temp= a;

a= b;

b= temp;

(8)

WRONG

#include <stdio.h>

void swap(int p, int q) { int tmp= 0;

tmp = p;

p = q;

q = tmp;

}

int main( void ) { int i =3, j =5;

swap(i, j);

printf(”%d %d\n”, i, j);

return 0;

}

3 5

3 3

5 5 i

j p

q

3

5

tmp

Memory

0 5

3 i and j are passed by value!

3

(9)

COMMA (WRONG)

#include <stdio.h>

int swap(int p, int q) { int tmp;

tmp = p;

p = q;

q = tmp;

return (p, q);

}

int main( void ) {

int i =3, j =5, res= 0;

res= swap(i, j);

printf(”%d %d\n”, i, j);

return 0;

}

3 3

5 5 i

j

res p

q

3

5

tmp

Memory

3 5

3

30 3 5

(10)

GLOBAL

#include <stdio.h>

int i= 3, j= 5;

void swap(void) { int tmp= 0;

tmp = i;

i = j;

j = tmp;

}

int main( void ) { swap();

printf(”%d %d\n”, i, j);

return 0;

}

It forces you to define two variables as global: it is not a good solution, you need two more support

variables, they are not the

“real” exchanged variables.

Moreover, they are global, they can be changed by other functions on the same file.

(11)

WHEN NOT TO USE GLOBAL VARS

Just because you can create global variables doesn't mean you should. The basic rule of thumb is:

Use them as less as possible

Some of the reasons are

 Using local variables allows you to focus you attention on smaller more manageable pieces of the program. Globals force you to keep track of how they're being used throughout the entire system

 You're working with other people, you must coordinate who's

creating which global variables. It is not valid to create two globals of the same name

 Localizing the effects of algorithm steps reduces the cases of unexpected behavior

 Localizing the effects of algorithm steps makes the program

easier for others to understand

(12)

BUT…

So when is it reasonable to use global variables?

 If the entire program is built around some central data and if

access to that data is needed in nearly every function and

particularly if that is a large data structure, then it makes

sense to make such data global

(13)

RIGHT WAY

#include <stdio.h>

void swap(int *p, int *q) { int tmp= 0;

tmp = *p;

*p = *q;

*q = tmp;

}

int main( void ) { int i =3, j =5;

swap(&i, &j);

printf(”%d%d\n”, i, j);

return 0;

}

i

j p

q

1000

1004

tmp

Memory

0

1000

1004 3

5 3

5

3

5 3

i and j are passed by reference!

(14)

POINTERS TO FUNCTION

(15)

FUNCTIONS ARE POINTERS

The name of a function is a constant pointer to the function

Its value is the address of the function in memory

#include <stdio.h>

void swap(int *p, int *q) { int tmp= 0;

tmp = *p;

*p = *q;

*q = tmp;

}

int main( void ) { int i =3, j =5;

swap(&i, &j);

printf(”%d%d\n”, i, j);

return 0;

}

(16)

EXAMPLE

#include<stdio.h>

int main( ) {

int (*pfunc) (const char*)= puts;

(*pfunc)("Any Question?");

}

MacBook-Francesco:ProgrammI francescosantini$ ./a.out Any Question?

Riferimenti

Documenti correlati

Since we are comparing two polynomials, it follows that the identity holds for all x

Solution proposed by Roberto Tauraso, Dipartimento di Matematica, Universit`a di Roma “Tor Vergata”, via della Ricerca Scientifica, 00133 Roma, Italy.. We will show a more

(American Mathematical Monthly, Vol.118, January 2011). Proposed by Kieren MacMillan (Canada) and Jonathan

Solution proposed by Roberto Tauraso, Dipartimento di Matematica, Universit`a di Roma “Tor Vergata”, via della Ricerca Scientifica, 00133 Roma, Italy.. Let π ∈

Thus c stays inside D and since it reaches the maximal distance from the boundary, it belongs to

Find the characteristic function φ X (t) of a random variable X with uniform distribution over the interval [−1, 1]. Shetch the graph of this

Some topics on modular functions, elliptic functions and transcendence theory. Sheet of

In a classical paper, J´ onsson [J] provided a characterization of those vari- eties in which every algebra has a distributive congruence lattice (from now on, for short,