• date post

21-Jan-2017
• Category

## Education

• view

132

7

Embed Size (px)

### Transcript of Η ΓΛΩΣΣΑ C - ΜΑΘΗΜΑ 8 -...

• H C

8:

.

1.

2.

3.

4. ( &)

5. ( *)

6.

.

1.

2.

3. !

4. "# \$% %

&. \$

1. \$

2. \$

3. \$ '

.

1.

2. \$ NULL

A. 1. !"

#

\$ \$ % ! !&" " ' !

.

"' ( ) "'!!"

%"

"

( " \$\$'&.

) ( " 1 byte (\$ \$ 8 bits).

'

"

( ", ' '

!&" *& ( '! \$" ).

#" ' '

! , '

'

!&" , ' '! 1 byte:

1Byte

0

1 2 1000 1001

10000 10001

2x109

1Byte 1Byte 1Byte 1Byte 1Byte 1Byte 1Byte

A. 1. !"

+\$

"" ' \$ ( "

, (.

- " "", \$"

( !

, ' "".

#" ! \$! main "" '

!. ('"&

f),

! &"

" (" "" ' ( "

.

/ ( \$"

! ! &'" '

, '

( ,' 0 ! "",

! &" . .)

1000 1001

15448 15449

1002

(! f (! main

• A. 1. !"

- \$ (

, ' " "",

! &" '!

( "" ! "

, .

#" .. f \$ ( " ( \$ &"

, ( \$ '

, ', '"&

x y (&

'

, \$"

4 bytes & "0"

"

!

)

1 *

1014 1015

(! f (! main

1016 1017 1057 1058 1059 1060

x y

A. 1. !"

!

#" !.

x=1 y=5

' (" " "( f,

"

, ' " '.

/ " " \$\$ "" & ( ( \$ \$%'

!%

!

.

.0 ,"

, " & \$

\$\$': \$\$'& (! *'

" bytes \$"

" )

( byte " '. .. x *'& ' 1

byte 1014, ( *!& ' " bytes 1014-1017.

1014 1015

(! f (! main

1016 1017 1057 1058 1059 1060

x y

1 5

A. 2. #

"

#" , ' '

" ,

, '

' ( " ("

'!

, " bytes)

!&"

\$" 1 byte \$"

.

"! & \$" ' ,

&"

" "\$ " C. 2 ! \$!

\$ ("

, "

\$"

, !

1014

(! f (! main

1057

x y

1 5

A. 2. #

(

\$ ("

, ('"&

ptr) "

.. \$"

, x.

, , & \$" &

, (

0 \$:

\$ ptr ' & \$"

, x. 3 '

!

! , \$ ptr \$ "

, x

1014

(! f

(! main

1057

)% x ptr

1 1014

• A. 3. /"

#

# \$ \$ (

\$ &":

(

' \$

/_ \$ "

,

\$\$'& 1_\$'&

\$!:

+\$( \$ (

' \$ " '

p.

+\$( \$ (

' \$ " double

ptr

"(" ' \$ ' \$\$'&. \$

, ' \$" .

/ \$\$'& ", (int *) (double *) " " \$ (" \$

.

_ *_

int *p;

double *ptr;

A. 4. \$" 1 "

\$%

' ' \$ ' """

, , & '

,

\$ "

, . !

* :

/ " & ( \$,0

\$" ) "

,

"'% \$" .

2:

x ' .. " \$" 10333,

&x (\$" x) 10333,

p

" 10333.

int x;int *p;

p=&x; // p x.

A. 5. \$" 1 '"& (/ " *)

\$\$

/ " * ( 0 " '

" \$"\$") ' \$ ":

+\$

( \$ ("

, \$.

\$ " *:

" , ,

* " ' \$, %" "

, \$ \$.

4 '

\$!:

\$! ," '

\$ ! '

"," "

, x:

x (

" ",")

'"& \$ \$ " x ('

" ",")

int x;int *p;

p=&x;

*p=5; // *p ( x) 5

A. 6. \$! 5" (

\$

! &", '" !

:

/* pointers.c: Deixnei tin vasiki xrisi twn deiktwn */

#include

main(){

int x;int *ptr;

ptr=&x;

x=5;printf("x=%d\t *ptr=%d\t &x=%d\t ptr=%d",x,*ptr,&x,ptr);*ptr=8;printf("\nx=%d\t *ptr=%d\t &x=%d\t ptr=%d",x,*ptr,&x,ptr);x=9;printf("\nx=%d\t *ptr=%d\t &x=%d\t ptr=%d",x,*ptr,&x,ptr);

}

• 4. 1. 1 -

\$

/ \$ " " "\$"

!

", \$ * " ' \$

\$\$'&

\$\$'& .

"" ' \$ \$

*

.

& "

' \$ \$ "

*

:

/ \$ \$ " ( '

.

/ \$ ptr *!

(" \$( "

,

\$ \$ " ( "

"" " !&0

( .

ptr=&pinakas[0]

int pinakas[100];int *ptr;

ptr=pinakas;

int pinakas[100];int *ptr;

ptr=&pinakas[0];

4. 2. " "

\$

8, \$

" " \$\$' '" .

#%" ( \$"("

.

+\$

\$ &":

! ' 4 \$\$( '"& "

&0

' &pin[0],&pin[1],&pin[2],&pin[3] \$" &

4

, ( .

1 !

\$% \$ &

, ' '

" \$\$' '" .

int pin[4];

(!

pin[0] pin[1] pin[2]

pin[3]

4. 2. " "

\$

! &", '"

" !

:

/* matrix_pointers.c: Deixnei oti oi theseis mnimis enos pinaka einaidiadoxikes */

#include

#define N 4

main(){

int pin[N];int i;

for (i=0; i

• 4. 3. (

\$"

1 & !

\$ &

\$'*

" '"&

, \$ !/* pointer_calc.c: Pws exoume prosvasi sta stoixeia enos pinaka mesw deikti */

#include

#define N 10

main(){

int pin[N];int i;int *ptr;

ptr=pin;for (i=0; i

• . "2. '" /" " " '"& 1

\$

6 ", \$

"" ' \$ (

, ( " .

"

!

\$ ' ' !

.

/*byvalue.c: Perasma orismatwn mesw timis */#include

void swap(int a,int b);

main(){int a=5,b=10;printf("\nMain: a=%d,b=%d",a,b);swap(a,b);printf("\nMain: a=%d,b=%d",a,b);

}

void swap(int a, int b){int k;k=a;a=b;b=k;printf("\nSwap: a=%d,b=%d",a,b);

}

. "2. '" /" " " '"& 1

& \$ !

.

" "" swap,

, ' a b ' ' a=5

b=10.

( ! " swap, " a, b "" swap '

" ' 5 10.

:

% " ' swap, 0 ' & a b " swap,

\$ ' '

, ' " swap & a,b " main \$ 0!

:

a b

(! main

5

(! swap

a b

510 10

a b

(! main

5

(! swap

a b

1010 5

. "2. '" /" " " '"& 1

,

" '"&

, (, !' ! "

, '

\$

& ", \$ * %" ' !

!

"'. " " "".

! "

' " " "" '! \$'"

" " "" '"& (by value) ' " ""

'

" '

, \$

, .

+ %' " ""

0

,

'" " "" \$ " "".

\$ ! "

' " " "", '!

\$'" " " "" '"& % (by reference) & \$

" "' \$ \$

& "

, , \$ "

, !

. "3. '" /" " " '"& %

2

"" & *:

/*byreference.c: Perasma orismatwn mesw anaforas */#include

void swap(int *ptrA, int *ptrB);

main(){int a=5,b=10;printf("\nMain: a=%d,b=%d",a,b);swap(&a,&b);printf("\nMain: a=%d,b=%d",a,b);

}

void swap(int *ptrA, int *ptrB){int k;k=*ptrA;*ptrA=*ptrB;*ptrB=k;printf("\nSwap: a=%d,b=%d",*ptrA,*ptrB);

}

• . "3. '" /" " " '"& %

8 \$

& \$ !

.

" "" swap,

, ' a b ' ' a=5

b=10.

'"

\$" a 1000 b 1012.

&a, !0 " 1000, ( &b !0 " 1012.

( 0 ' " swap :

a b

(! main

5

(! swapptrA

10

a b

(! main

5

(! swap

100010

1000 1012

ptrB

1000 1012

ptrA ptrB

1012

. "3. '" /" " " '"& %

!

\$

( ' ' " swap:

k=*ptrA; //' "

, k

, \$ ptrA

*ptrA=*ptrB; ' "

, \$ ptrA \$ ptrB

*ptrB=k; ' "

, \$ ptrB k

a b

(! main

5

(! swap

1000101000 1012

ptrA ptrB

1012

k

5

a b

(! main

10

(! swap

1000101000 1012

ptrA ptrB

1012

k

5

a b

(! main

10

(! swap

100051000 1012

ptrA ptrB

1012

k

5

. "3. '" /" " " '"& %

"

1

:

-

'

" "" " \$

, ' main!

2 ! ! , !'

"

, ' ' "

main!

.0:

'

*

, " "" ' "

\$ " " "", \$

" '"&

% (\$ \$ '"& \$)

( \$

" '"& .

+ ':

'

"" '

"%

'".

1 ' "%

' "

return '"&

\$!

9

\$ "%

' '"&

\$.

. "1. '" & -" " "

&":

+\$ ""' \$

' & ", !

%' " !&" \$ "

, , C

\$ \$:

!.

&:

8 !.

&:

/\$ \$ & ""

'

\$

' ".

void print(int *pinakas, int n);

void print(int pinakas[], int n);

," & * " '" " "" !

'"& %.

2 " \$ ! ! " ' \$

&

", ' " " "".

'

"

!% & " " "", '

""

( ' !% .

• . "2. NULL

#

# ""' &

" ' \$ \$ '

"""

, . .. (\$:

3 \$!" "% ' "! 1 p \$ \$ ,

'"

\$ 0

, , !

'

"\$ "%!

( \$ (

' \$, ' ""0

,

' " .

3 * (" '

\$ ' . !

" C "' ", ", NULL,