51
Data structures and algorithms Instructor: Masoud Asghari Web page: http://www.maser.ir/lectures/dsas 2017sut/ Ch 0.2 (Preliminary Pointers) Advanced programming, By Masoud Asghari, Ch: 3. 1

Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Data structures and algorithmsInstructor: Masoud Asghari

Web page: http://www.maser.ir/lectures/dsas2017sut/

Ch 0.2 (Preliminary Pointers)

Advanced programming, By Masoud Asghari, Ch: 3. 1

Page 2: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

In this Slide Series:

1. C++ Mainly from http://www.cplusplus.com/doc/tutorial/

• Advanced• Pointers

• Functions (Arguments passed by value and by reference)

• Dynamic Memory

• Data structures

Advanced programming, By Masoud Asghari, Ch: 3. 2

Page 3: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Introduction

• variables have been explained as locations in the memory • which can be accessed by their identifier (their name).

• This way, the program does not need to care about the physical address of the data in memory;

• it simply uses the identifier whenever it needs to refer to the variable.

• the memory of a computer • is like a succession of memory cells,

• each one byte in size, and

• each with a unique address.

• data representations larger than one byte • Can occupy memory cells

that have consecutive addresses

• each cell (byte of memory) • can be easily located in the memory

by means of its unique address

Advanced programming, By Masoud Asghari, Ch: 3. 3

Page 4: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

• When a variable is declared,• the memory needed to store its value is assigned a specific location in

memory (its memory address).

• operating system • decides the particular memory locations on runtime.

• However, it may be useful for a program to be able to obtain the address of a variable during runtime

Advanced programming, By Masoud Asghari, Ch: 3. 4

Page 5: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers

• Address-of• The address of a variable (beginning cell of where its value is saved)

• can be obtained by preceding the name of a variable with an (&) sign

• known as address-of operator.

• We need a variable who can save a physical address of a type.• This variable is called a pointer.

• It only saves the address of a single memory cell (Byte)

• To declare a pointer :

• type is the data type pointed to by the pointer.

• This type is not the type of the pointer itself, but the type of the data the pointer points to.

Advanced programming, By Masoud Asghari, Ch: 3. 5

Page 6: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers

Advanced programming, By Masoud Asghari, Ch: 3. 6

• How does above code work• Line1:

• Your program asks O.S. for 2 consecutive bytes at memory (short type)

• O.S. allocates 2 consecutive bytes and returns their first cell memory address

• Assuming a 32 bit computer, the returned memory address is a 32bit memory address = 32/8= 4Byte address

• Assuming a 64 bit computer, the returned memory address is a 64bit memory address = 64/8= 8Byte address

• For example O.S. returns 0x00000ba0

• Memory cells of (0x00000ba0 to 0x00000ba1) are reserved for aaa variable

• From now on, wherever, you use aaa variable, the program replaces it with its memory address which is 0x00000ba0

Address (4Byte) Content (1Byte)

0x00000b9d 0x??

0x00000ba0 0x??

0x00000ba1 0x??

0x00000ba2 0x??

aaa

Page 7: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers

Advanced programming, By Masoud Asghari, Ch: 3. 7

• How does above code work• Line2:

• Your program asks O.S. for 4 (or 8) consecutive bytes at memory depending on 32 bit (or 64 bit) computer architecture

• These memory cells will be used to save address of other memory cells that contains short type value

• O.S. allocates 4 (or 8) consecutive bytes depending on 32 bit (or 64 bit) computer architecture, and returns their first cell memory address

• For example O.S. returns 0x00000ba2

• Memory cells of (0x00000ba2 to 0x00000ba5) are reserved for aaap pointer

• From now on, wherever, you use aaap, the program replaces it with its memory address which is 0x00000ba2

Address (4Byte) Content (1Byte)

0x00000b9d 0x??

0x00000ba0 0x??

0x00000ba1 0x??

0x00000ba2 0x??

0x00000ba3 0x??

0x00000ba4 0x??

0x00000ba5 0x??

0x00000ba6 0x??

aaa

aaap

Page 8: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers

Advanced programming, By Masoud Asghari, Ch: 3. 8

• How does above code work• Line3:

• Your program sees an assignment operation.

• Right hand side is 0x0001

• And the left hand side is a variable name aaa

• As told, aaa is replaced with its memory address 0x00000ba0

• Thus lines 3 says to computer that put 0x0001in memory cells starting from 0x00000ba0 to 0x00000ba1 addresses

• In particular, put 0x00 in 0x00000ba0 address

• And put 0x01 in 0x00000ba1 address

• This order changes depending on little or big Endian architecture

Address (4Byte) Content (1Byte)

0x00000b9d 0x??

0x00000ba0 0x00

0x00000ba1 0x01

0x00000ba2 0x??

0x00000ba3 0x??

0x00000ba4 0x??

0x00000ba5 0x??

0x00000ba6 0x??

aaa

aaap

Page 9: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers

Advanced programming, By Masoud Asghari, Ch: 3. 9

• How does above code work• Line4:

• Your program sees an assignment operation.

• Right hand side is and address of operator followed by a variable

• address of operator tells your programto reflect the actual address of the variable

• Here, &aaa equals 0x00000ba0

• And the left hand side is a pointer name aaap

• As told, aaap is replaced with its memory address 0x00000ba2

• Thus lines 4 says to computer that put 0x00000ba0 in memory cells starting from 0x00000ba2 to 0x00000ba5 addresses

• As the figure shows

Address (4Byte) Content (1Byte)

0x00000b9d 0x??

0x00000ba0 0x00

0x00000ba1 0x01

0x00000ba2 0x00

0x00000ba3 0x00

0x00000ba4 0x0b

0x00000ba5 0xa0

0x00000ba6 0x??

aaa

aaap

Page 10: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers

• Example 2

Advanced programming, By Masoud Asghari, Ch: 3. 10

Page 11: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers

• Dereference operator (*)• A pointers can be used to access the

variable it points to directly.

• This is done by preceding the pointer name with the dereference operator (*).

• The operator itself can be read as "value pointed to by".

• The reference and dereference operators are thus complementary:• & is the address-of operator, and can be

read simply as "address of"

• * is the dereference operator, and can be read as "value pointed to by"

Advanced programming, By Masoud Asghari, Ch: 3. 11

Page 12: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers

• Declaring pointers• Why do we need a type for declaring a pointer?

• Due to the ability of a pointer to directly refer to the value that it points to,

• a pointer has different properties when it points to a char than when it points to an int or a float.

• Once dereferenced, the type needs to be known.

• And for that, the declaration of a pointer needs to include the data type the pointer is going to point to.

• the first one points to an int, the second one to a char, and the last one to a double.

• Therefore, although these three example variables are all pointers,

• they actually have different types: int*, char*, and double* respectively, depending on the type they point to

Advanced programming, By Masoud Asghari, Ch: 3. 12

Page 13: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers• Note that the asterisk (*) used when declaring a pointer only means that it is a

pointer (it is part of its type compound specifier)

• This should not be confused with the dereference operator, which is also written with an asterisk (*).

• They are two different things represented with the same sign.

Advanced programming, By Masoud Asghari, Ch: 3. 13

Page 14: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointer arithmetic

• only addition and subtraction operations are allowed on pointers• both addition and subtraction have a different behavior with pointers, according to

the size of the data type to which they point.

• We know that different types have different sizes. • the exact size of different types, dependents on the system.

• For example, assume, char takes 1 byte, short takes 2 bytes, and long takes 4.• Suppose now that we define three pointers:

• and they point to the memory locations 1000, 2000, and 3000, respectively.

• if we write:

• Or

• mychar would contain the value 1001.

• But myshort would contain the value 2002, and mylong would contain 3004, even though they have each been incremented by one.

• The reason is that, when adding one to a pointer, the pointer is made to point to the element of its type, and, therefore, the size in bytes of the type it points to is added to the pointer.

Advanced programming, By Masoud Asghari, Ch: 3. 14

Page 15: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointer arithmetic

Advanced programming, By Masoud Asghari, Ch: 3. 15

• Remembering operator precedence rules, • postfix operators, such as increment and decrement, have higher precedence

than prefix operators, such as the dereference operator (*).

• Parentheses reduce confusion by adding legibility to expressions

Page 16: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers and arrays• The concept of arrays is related to that of pointers.

• In fact, arrays work very much like pointers to their first elements, • brackets [ ] were explained as specifying the index of an element of the array.

• in fact They dereference the variable they follow just as * does, but they also add the number between brackets to the address being dereferenced.

• These expressions are equivalent and validnot only if a is a pointer, but also if a is an array

• an array can always be implicitly converted to the pointer of the proper type. • For example:

• The following assignment operation would be valid:• After that, mypointer and myarray would be equivalent and would have very

similar properties.• The main difference is that mypointer can be assigned a different address, whereas

myarray can never be assigned anything, and will always represent the same block of 20 elements of type int.

Advanced programming, By Masoud Asghari, Ch: 3. 16

Page 17: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers and arrays

• an example that mixes arrays and pointers

• Pointers and arrays support the same set of operations, with the same meaning for both.

• The main difference is that pointers can be assigned new addresses, while arrays cannot.

• Also, arrays allocate memory at the time of definition (with fixed amount), but pointers need allocated memory to point to.

Advanced programming, By Masoud Asghari, Ch: 3. 17

cout<< p[n] << “, “; //the samecout<< *(p+n) << “, “; //the same

cout<< *(numbers+n) << “, “; //the same

Page 18: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers and string literals

• Problem with array of characters (Slide 2.53)• once myword has already been declared as below

• Its sequence of values cannot be changed as above.

• Only value of each single element can be changed

• Using pointers as a solution• Remember: pointers can be assigned new addresses, while arrays cannot

Advanced programming, By Masoud Asghari, Ch: 3. 18

Veryimportant

Page 19: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers and string literals

• string literals• are arrays containing null-terminated character sequences.

• When you write

• You define an array of chars with 3 elements: {'H', 'i', '\n'}

• So basicly "Hi" returns a pointer (array) pointing to its first element (containing address of the first element in the memory)

• This address then is used by cout to get the characters

• cout accepts following as a strings:• string literals such as "Hi"

• A char such as 'H'

• Name of array of chars such as x that defined as: char x[10];

• An element of this array such as x[2]

• Name of pointer of type char* such as p that defined as: char* p;

• A single element of this pointer such as *(p+2)

• cin accepts following as a strings:• Name of array of chars (replaces its elements value)

• Name of pointer of type char* (memory should be allocated first)

Advanced programming, By Masoud Asghari, Ch: 3. 19

Page 20: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers and string literals

• Example

Advanced programming, By Masoud Asghari, Ch: 3. 20

Page 21: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers and const

• Pointers can be used to access a variable by its address,• this access may include modifying the value pointed.

• But it is possible to declare pointers that can access the pointed value to read it, but not to modify it.

• For this, it is enough with qualifying the type pointed to by the pointer as const.

• For example:

• Pointers can also be themselves const.

• And this is specified by appending const to the pointed type (after *)

Advanced programming, By Masoud Asghari, Ch: 3. 21

Page 22: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers to pointers

Advanced programming, By Masoud Asghari, Ch: 3. 22

Page 23: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

void pointers• The void type of pointer is a special type of

pointer. • void represents the absence of type. • Therefore, void pointers are pointers that point to

a value that has no type (and thus also an undetermined length and undetermined dereferencing properties).

• This gives void pointers a great flexibility:• by being able to point to any data type,

from an integer value or a float to a string of characters.

• This gives void pointers a great limitation: • the data pointed to by them cannot be directly

dereferenced • (since we have no type to dereference to), • Thus any address in a void pointer needs to be

transformed into some other pointer type that points to a concrete data type before being dereferenced.

• This can be done by explicit type casting

Advanced programming, By Masoud Asghari, Ch: 3. 23

Page 24: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Invalid pointers and null pointers

• Pointers can actually point to any address,• including addresses that do not refer to any valid element.

• examples are uninitialized pointers and pointers to nonexistent elements of an array:

• Neither p nor q point to addresses known to contain a value, but none of the above statements causes an error.

• What can cause an error is to dereference such a pointer. Accessing to the place that such a pointer points, causes undefined behavior, ranging from an error during runtime to accessing some random value.

• If you want to explicitly point nowhere, • a special value that any pointer type can take: the null pointer value.

• null pointer value can be expressed by 0 or NULL:

Advanced programming, By Masoud Asghari, Ch: 3. 24

Page 25: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Functions (Arguments passed by value and by reference)

• In the functions seen earlier, arguments have been passed by value.• when calling a function,

• what is passed to the function are the values of these arguments on the moment of the call,

• which are copied into the variables represented by the function parameters.

• any modification of these variables within the function has no effect on the values of the original variables outside it

• arguments can be passed by reference, instead of by value.• Changes on parameters inside the function also changes the value of

arguments calling it from the outside of function

• How pass arguments by reference to an function• You can define function’s parameters as pointers and pass the address of

arguments when calling the function

• Or declares function’s parameters as references.

• references are indicated with an (&) following the parameter type

• Call the function as usual

Advanced programming, By Masoud Asghari, Ch: 3. 25

Page 26: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Functions (Arguments passed by value and by reference)

Advanced programming, By Masoud Asghari, Ch: 3. 26

Examples of three methods of passing arguments

Page 27: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Functions (Arguments passed by value and by reference)

• Remember, arrays are in fact references• So you should declares function’s parameters as pointers to pass an array or a

pointer

• Call with only name of array or pointer as arguments

Advanced programming, By Masoud Asghari, Ch: 3. 27

Page 28: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Dynamic memory

• When declaring primitive types or arrays• Compiler allocates memory for them

• This memory allocation is static

• As you have seen, pointers have no where to point at the declaration• They so far referred to already allocated memories (static)

• To allocate memory at runtime (dynamically) • We need memory allocation operators

• and to handle them, we use pointers

• Examples: unknown length of arrays before running

• You need to include <new> library at the beginning

Advanced programming, By Masoud Asghari, Ch: 3. 28

Page 29: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Dynamic memory

• Operators new and new[]• Dynamic memory is allocated using operator new.

• new is followed by a data type specifier and,

• if a sequence of more than one element is required, the number of these within brackets [ ].

• It returns a pointer to the beginning of the new block of memory allocated.

• Its syntax is:

• Example:

• the system dynamically allocates space for five elements of type int

• and returns a pointer to the first element of the sequence, which is assigned to foo (a pointer).

• Therefore, foo now points to a valid block of memory with space for five elements of type int.

Advanced programming, By Masoud Asghari, Ch: 3. 29

Page 30: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Dynamic memory

• Operators delete and delete[]• once the memory is no longer needed,

• it can be freed so that the memory becomes available again for other requests of dynamic memory.

• Also if you know that the block of memory will not be pointed

• You should free (delete) it to prevent memory leaks

• Dynamic memory is freed using operator delete.

• delete is followed by a pointer,

• if a sequence of more than one element is allocated by new, use [ ] after delete.

• Its syntax is:

Advanced programming, By Masoud Asghari, Ch: 3. 30

Page 31: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Dynamic memory

• Example

Advanced programming, By Masoud Asghari, Ch: 3. 31

Page 32: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Dynamic memory in C

• C++ integrates the operators new and delete for allocating dynamic memory. • But these were not available in the C language;

• instead, it used a library solution, with the functions malloc, calloc, realloc and free,

• defined in the header <cstdlib> (known as <stdlib.h> in C).

• The functions are also available in C++ and can also be used to allocate and deallocatedynamic memory.

Advanced programming, By Masoud Asghari, Ch: 3. 32

Page 33: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Dynamic memory in C• malloc

• Allocates a block of size bytes of memory,

• returns a pointer to the beginning of block.• size_t is an unsigned integral

type.• Return Value

• On success, a pointer to the memory block allocated by the function.

• The type of this pointer is always void*, which can be cast to the desired type of data pointer in order to be dereferenceable.

• If the function failed to allocate the requested block of memory, a null pointer is returned.

Advanced programming, By Masoud Asghari, Ch: 3. 33

Page 34: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Dynamic memory in C• http://www.cplusplus.com/reference/cstdlib/calloc/

• http://www.cplusplus.com/reference/cstdlib/realloc/

Advanced programming, By Masoud Asghari, Ch: 3. 34

Page 35: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Dynamic memory in C

• free• Deallocates memory block

• with a given pointer to block of memory previously allocated by a call to malloc, callocor realloc is deallocated, making it available again for further allocations.

• If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior.

• Notice that this function does not change the value of ptr itself, hence it still points to the same (now invalid) location.

Advanced programming, By Masoud Asghari, Ch: 3. 35

Page 36: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Data structures

• A data structure• User defined data_type (like int, double, … you define yours)

• is a group of data elements grouped together under one name.

• These data elements, known as members, can have different types and different lengths.

• Declaration syntax:

• type_name is a name for the structure type,

• Within braces { }, there is a list with the data members, each one is specified with a typeand a valid identifier as its name.

• After defining a struct (your data type), you can declare variables of this newly defined type (As usual variables)

• Example:

Advanced programming, By Masoud Asghari, Ch: 3. 36

Page 37: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Data structures

• It is important to clearly differentiate between • what is the structure type name (product),

• and what is an object of this type (apple, banana, and melon).

• Many objects (such as apple, banana, and melon) can be declared from a single structure type (product).

• Once an objects of a determined structure type is declared • its members can be accessed directly by inserting a dot (.) between the

object name and the member name.

• We could operate with any of these elements as if they were standard variables of their respective types

• one of the features of data structures • is the ability to refer to both their members individually

• or to the entire structure as a whole.

• In both cases using the same identifier

• (the name of the structure).

Advanced programming, By Masoud Asghari, Ch: 3. 37

Page 38: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Data structures

Advanced programming, By Masoud Asghari, Ch: 3. 38

Example

Page 39: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers to structures

• Like any other type, • structures can be pointed to by its own type of pointers:

• amovie is an object of structure type movies_t,

• pmovie is a pointer to point to objects of structure type movies_t.

• Therefore, the following code would also be valid:

• The value of the pointer pmovie would be assigned the address of object amovie.

Advanced programming, By Masoud Asghari, Ch: 3. 39

Page 40: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers to structures

• The arrow operator (->)• is a dereference operator that is used exclusively with pointers to objects that have

members• (*pointer_to_an_object).member_name

• equals to

• pointer_to_an_object-> member_name

• Example: Given

• pmovie->title is equivalent to (*pmovie).title

• Both has the value of amovie.title

Advanced programming, By Masoud Asghari, Ch: 3. 40

Page 41: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Pointers to structures

• Nesting structures

Advanced programming, By Masoud Asghari, Ch: 3. 41

Page 42: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Linked Lists (dynamic memory) by struct

Advanced programming, By Masoud Asghari, Ch: 3. 42

Page 43: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Back to the BOOK

Page 44: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Page 16, 20 of the book

Cout<< * np; // de reference operator

Page 45: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Page 20 of the book

Page 46: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Page 21 of the book

Page 47: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

47

Page 48: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

48

Page 49: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

49

Page 50: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

50

Page 51: Data structures and algorithms - maser.ir€¦ · •Advanced •Pointers •Functions (Arguments passed by value and by reference) •Dynamic Memory •Data structures Advanced programming,

Exercises

1. Rewrite program in P31 as follows:• Don’t ask for number of numbers

• Get numbers as entered, and finish getting numbers if 0 is entered.

• There is no limit for the number of numbers

• You can use realloc() or any methods

2. Rewrite program in P42 as follows:• Each node_t includes (node_t * previous) pointer, links back to its parent

• The first node’s previous points to NULL

• Add an extra loop after loop of line 31 to 39 finishes

• Starting from the last node

• Show list scores in reverse order

Advanced programming, By Masoud Asghari, Ch: 3. 51