lab | c代写 | C语言代写 | 编程代做 – UNIX C Unix

UNIX C Unix

lab | c代写 | C语言代写 | 编程代做 – 该题目是关于c语言编程相关的任务,属于基础的lab代写的题目

lab代写 代写lab

UNIX C Unix

UNIX C Unix

UNIX C Unix

UNIX C Unix

UNIX C Unix

UNIX C Unix

EECS

Software Tools

######## Dr. Marzieh Ahmadzadeh

Outline

######## Last week

 Pointers
 Definition & manipulation
 Pointer Arithmetic
 Arrays as pointers

######## Today:

 Composite data type
 Struct
 Struct andpointers
What data type is used to store?

######## A students name?

######## The courses that a student has taken?

######## All the grades that a student received for all the taken courses?

######## a students discipline/major?

######## Can we group all under one name?

 Name
 All the courses they have taken
 grade
 discipline
Compound Data Type

######## Syntax:

struct name_of_the_struct {
// here goes the definition
// of the components
};

######## Convention: name of the struct starts with a

######## upper-case letter.

######## Variable declaration:

struct name_of_the_struct variable_name;

####### Access: using dot operator

Memory Allocation

######## A memory is allocated only when a variable of struct is declared.

1000
<letterGrade, char>
1001
<grade, double>
1002
1003
1004
1005
1006
<st1, Struct Student>
<st2, Struct Student>
<st3, Struct Student>
Aliasing

######## To have a shorter name for a compound data type, an alias is created.

Functions & Struct (1)

######## A struct can be passed as an input parameter.

1000
<letterGrade, char>
1001
<grade, double>
1002
1003
1004
1005
1006
<st1, Student>
<st, Student>
Functions & Struct (2)

######## A struct can be returned from a function, which let us return

######## more than one value.

1000
<letterGrade, char>
1001
<grade, double>
1002
1003
1004
1005
1006
<st1, Student>
<st, Student>
<return, Student>
What will be printed?
1000
<letterGrade, char>
1001
<grade, double>
1002
1003
<incr, int>
1004
<return, Student>
<st1, Student>
<st, Student>
Struct & arrays (1)

######## It is possible to have an array as a component of a struct.

######## Variable declaration follows the usual rule.

######## Variable initialization follows the usual rule. (use of dot

######## operator)

1000
<suit, char[]>
.....
1010
<rank, char[]>
....
1020
1021
1022
<myCard, Card>
Struct & arrays (1)

######## It is possible to declare an array of struct data type.

######## Initialization: choose the correct index and use dot

######## operator to set the value of the struct variable.

Activity

######## The following struct and constants are defined:

######## Three variables, also defined:

######## 1. On a piece of paper, write a

######## code snippet that initializes

######## the deck with

[0] One (of) Diamonds
[1] Two (of) Diamonds
[2] Three (of) Diamonds
....
[51] King (of) Clubs
Design Tips

######## Use of alias for struct (i.e.typedef) is strongly

######## recommended for readability purpose.

######## You can separate the definition of struct data

######## type from the code by placing it in .h file.

 You need to include .h file in your C program.
 To include you should use  .

######## The memory space allocated to a struct is not

######## necessarily equal to the total of its members

######## size (due to boundary alignment requirement)

 Therefore, the structs cannot be compared with == or !=
 Question: how do you compare two structs?

STRUCT & POINTERS

Recall

######## Structs are passed by value.

1000
<letterGrade, char>
1001
<grade, double>
1002
1003
1004
1005
1006
<st1, Student>
<st, Student>

######## A

  1. 0

######## A

  1. 0
A compound data type may require a large space

If the struct requires a large space, moving data will be slow Wasting memory spaces is possible (in case passnecessary) -by-value is not

1000
<st1 , Student>
2000
<st , Student>
Is there any way by which we can
send a struct by its reference?
Compound Data Types & Pointers

######## With pointers, a compound data type is passed by its reference

 And therefore, less space is allocated to the variable.

######## When a pointer of a compound data type is defined, -> operator is used to access the

######## components.

######## Compare these two code snippets:

Dot vs Arrow Operator
-> operator, dereferences the pointer to get access to
its value/location.
These two codes are the same.
1500
<st1, Student*>
1000
<st, Student>
Memory Model
1500
<st1, Student*>
1000
<st , Student>
2000
<st , Student*>
1000
1000
<st , Student>
A
100
A
100
A
100
....
.....
.....
1000
Dereferencing st
Another Example
1000
<mv1 , Movie>
Another Example
1000
<mv1 , Movie>
Another Example
1000
<mv1 , Movie>
Titanic
Another Example
1000
<mv1 , Movie>
Titanic
Another Example
1000
<mv1 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
Another Example
1000
<mv1 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
Another Example
1000
<mv1 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
2000
Titanic
Kate Winslet
Leonardo DiCaprio
......
2
Another Example
1000
<mv1 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
2000
Titanic
Kate Winslet
Leonardo DiCaprio
......
2
Another Example
1000
<mv1 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
2000
Titanic
Kate Winslet
Leonardo DiCaprio
......
2
Struct & pointers

######## Compare these two code snippets:

Memory Model
500
<mv2 , Movie>
Memory Model
2000
<mvPtr , Movie*>
500
500
<mv2 , Movie>
Memory Model
2000
<mvPtr , Movie*>
500
500
<mv2 , Movie>
Memory Model
2000
<mvPtr , Movie*>
500
500
<mv2 , Movie>
Titanic
Memory Model
2000
<mvPtr , Movie*>
500
500
<mv2 , Movie>
Titanic
Dereference mvPtr
Memory Model
2000
<mvPtr , Movie*>
500
500
<mv2 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
Memory Model
2000
<mvPtr , Movie*>
500
500
<mv2 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
Memory Model
2000
<mvPtr , Movie*>
500
<mv , Movie*>
500
<actNo, int>
2
500
<mv2 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
Memory Model
2000
<mvPtr , Movie*>
500
<mv , Movie*>
500
<actNo, int>
2
500
<mv2 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
Memory Model
2000
<mvPtr , Movie*>
500
<mv , Movie*>
500
<actNo, int>
2
500
<mv2 , Movie>
Titanic
Kate Winslet
Leonardo DiCaprio
What is wrong with this code?

######## This code generates a runtime error Segmentation fault (core dumped). How do you

######## solve the problem

Challenging: What is wrong with this code?

######## This code generates a runtime error Segmentation fault (core dumped). How do you

######## solve the problem

Memory Reservation

######## Whats the difference between these two lines of

######## code?

######## How do you reserve space for the mv2s members

######## (i.e. name and actor)?

 Using array index
 Using pointer arithmetic
What is wrong?

######## This code issues malloc():

######## corrupted top size,

######## Aborted (core dumped)error.

######## Why?

######## Solution?

######## What does the corrected code do?

ENUMERATION TYPE

Enumeration type

######## A set limited integer enumeration constants

######## can be represented by an identifier.

######## The keyword to define an enumeration type

######## is enum.

######## Type definition:

typedef enum name_of_enum {
// names for the values goes here
} alias_name ;

######## Can be used to define a variable.

######## Can be used in switch-case statement or

######## as a counter of for-loops.

Expectations

######## Expectations

 Should be comfortable to write a code that requires struct type.
 Should be comfortable to write a code that requires enumeration data type.

######## Reading

 9.1 9.6

######## Assignment

 W7_PA
 All PAandCAin9.1-9. 6
 W7_Lab
  lab 19. 2