/
Arrays as pointers and other stuff Arrays as pointers and other stuff

Arrays as pointers and other stuff - PowerPoint Presentation

tatyana-admore
tatyana-admore . @tatyana-admore
Follow
382 views
Uploaded On 2018-02-20

Arrays as pointers and other stuff - PPT Presentation

COP3275 Programming using c Diego J RiveraGutierrez Administrative stuff Quiz 6 was Quiz 7 this Friday Will tell you what to expect on Wednesday Homework 5 Any questions Functions ID: 633355

array return function float return array float function int pointers type statements

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Arrays as pointers and other stuff" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Arrays as pointers and other stuff

COP3275 – Programming using c

Diego J. Rivera-GutierrezSlide2

Administrative stuff

Quiz #6 was ??

Quiz #7 this Friday.

Will tell you what to expect on Wednesday.

Homework #5

Any questions?Slide3

FunctionsSlide4

Declaring functions

Declaration

Return type

Name/identifier

Parameters

Function content

<return type> <identifier>(<parameters>) {

<function content>

}

Example:

int

multiply(

int

a,

int

b) {

return a*b;

}

Declaring a function is “

creating

a hammer” but is

not

“using the hammer”

No code gets executed from defining a functionSlide5

Calling a function

How do we use the “hammer”?

Once we have one defined:

int

multiply(

int

a,

int

b) {

return a*b;

}

We can call it by it’s name and giving values to it’s parameters:Slide6

Calling a function

How do we use the “hammer”?

Once we have one defined:

int

multiply

(

int

a,

int

b) {

return a*b;

}

We can call it by it’s name and giving values to it’s parameters:

multiply(3,4);Slide7

Returning values

We can give a function as many return statements as we want.

For example:

char

toUpperCase

(char c) {

switch(c) {

case 'a': return 'A';

case

‘b':

return

‘B';

case

‘c':

return

‘C';

case

‘d':

return

‘D';

case

‘z':

return

‘Z';

}

}

That’s a very inefficient function (only used for demonstration of the concept)

They only have to have the same type (matching the return type)Slide8

Returning values

A call to a function will return the value next to the return statement that it’s execution reaches.

What does that mean?

If we execute

toUpperCase

('k');

then it’s value will be 'K'

Then if we do:

char uppercase =

toUpperCase

('h');

//uppercase will have a value of 'H'Slide9

Returning on a function with “void” as return type

We talked about void being used when we don’t want to return anything.

Functions returning void can still have return statements.

They don’t get a value

They are simply:

return;

These return statements just stop the execution of the function

Useful to handle errors.Slide10

Returning on a function with “void” as return type

void

printPositiveInt

(

int

a) {

if(a < 1 ) {

return;

}

printf

("%

i

", a);

}Slide11

What can the content of a function be?

Statements

Variable declarations

Variable assignments

If statements

Switch statements

Loops

Return statements

Calls

to other functions

What

cannot

be part of the content?

A

declaration

of another functionSlide12

Arrays as pointersSlide13

Arrays vs. pointers

I have said it before but in terms of their

structure in memory:

Arrays and pointers are the same thing.

Notation can be interchanged and C won’t complain about it.

For example:

float array[100];

array[0] = 3.14159;

printf

(“%.5f\n", (*array)); //This prints 3.14159

Why?Slide14

Arrays vs. pointers

Our array definition is technically a pointer to the first element of the array.

So when I write

(*array)

on the previous example, it goes and grabs the first element.

Actually

(*array)

and

array[0]

are the exactly same piece of code.

Think about them as synonyms in a way…

Is there a synonym for array[2]?

Yes!

*(array+2)

The parenthesis are important…

What does it mean to add 10 to a pointer? Slide15

Pointer arithmetic and offsets

Remember that pointers are memory addresses

Those memory addresses are usually in bytes.

If we added 2, what exactly does that mean?

Would it be 2 bytes?

Nope – Moving one byte would lead to numbers that don’t really make sense.

Pointers do have a type.

In our example we have a float array, so the type is float*

Floats have a certain size

4 bytes.

So adding 1 to a pointers moves the pointer to the next float (not the next byte)Slide16

Pointer arithmetic

float array[100];

array[0] = 3.14159f;

array[2] = 0.17170f;

printf

("%p: %.5f\n" , array, *(array));

printf

("%p: %.5f\n" ,

array+2,

*(

array+2));Slide17

Does that have an implication for malloc

?

malloc

: Dynamic memory allocation

float *

float_ptr

= (float*)

malloc

(

sizeof

(float));

Now… what happens if I write it like this:

float *

float_arr

= (float*)

malloc

(

sizeof

(float

)*100);

Or even better

int

size = 100;

float

*

float_arr

= (float*)

malloc

(

sizeof

(float

)*size);