Podcast
Questions and Answers
Given a structure defined as struct item { int id; float price; };
, and assuming sizeof(int)
is 4 bytes and sizeof(float)
is 4 bytes, what is the size of struct item
and how is the memory allocated?
Given a structure defined as struct item { int id; float price; };
, and assuming sizeof(int)
is 4 bytes and sizeof(float)
is 4 bytes, what is the size of struct item
and how is the memory allocated?
- 8 bytes, with `id` and `price` stored contiguously in memory.
- It depends on the compiler. If it's a 16-bit compiler, it might be smaller than 8 bytes, but on a 64-bit compiler, it will always be 8 bytes.
- Potentially more than 8 bytes due to possible padding added by the compiler to optimize memory access, but `id` and `price` are still contiguous. (correct)
- 4 bytes, with the `id` and `price` members overlapping in memory.
If you have a structure struct point { int x; int y; };
and a pointer struct point *p;
, which of the following is the correct method to access the x
member of the structure pointed to by p
?
If you have a structure struct point { int x; int y; };
and a pointer struct point *p;
, which of the following is the correct method to access the x
member of the structure pointed to by p
?
- Both B and C (correct)
- `p->x`
- `(*p).x`
- `p.x`
Consider a scenario where you need to define a structure representing network packets. Which of the following is the most accurate description of how bitwise structures could be utilized?
Consider a scenario where you need to define a structure representing network packets. Which of the following is the most accurate description of how bitwise structures could be utilized?
- To dynamically allocate memory for each part of a network packet, giving developers more control over where packet data is located in memory.
- To create arrays of structures, which is especially useful for implementing network protocols via look-up tables.
- To pack packet header fields (e.g., flags, sequence numbers) into specific numbers of bits within a larger data type to conserve memory. (correct)
- To define the structure layout so it can be directly mapped to a function's parameters.
You have a structure representing a complex number: struct complex { double real; double imaginary; };
. You want to write a function that adds two complex numbers. Which of the following function signatures is most appropriate, assuming you want to modify the first complex number in-place with the result?
You have a structure representing a complex number: struct complex { double real; double imaginary; };
. You want to write a function that adds two complex numbers. Which of the following function signatures is most appropriate, assuming you want to modify the first complex number in-place with the result?
Suppose you have an array of structures: struct point points[10];
where struct point
is defined as struct point { int x; int y; };
. Which of the following correctly represents how to access the y
coordinate of the 5th element in the array?
Suppose you have an array of structures: struct point points[10];
where struct point
is defined as struct point { int x; int y; };
. Which of the following correctly represents how to access the y
coordinate of the 5th element in the array?
Given the following structure definition typedef struct { int id; char name[20]; float score; } STUDENT;
, and assuming sizeof(int)
is 4 bytes, sizeof(char)
is 1 byte, and sizeof(float)
is 4 bytes, what is the most likely reason for sizeof(STUDENT)
equalling 28 bytes, rather than 28 bytes as one might expect?
Given the following structure definition typedef struct { int id; char name[20]; float score; } STUDENT;
, and assuming sizeof(int)
is 4 bytes, sizeof(char)
is 1 byte, and sizeof(float)
is 4 bytes, what is the most likely reason for sizeof(STUDENT)
equalling 28 bytes, rather than 28 bytes as one might expect?
Consider the code snippet:
typedef struct { int x; int y; } POINT;
POINT p1 = {320, 100};
POINT p2;
p2 = p1;
p1.x = 500;
What are the values of p2.x
and p1.x
after the execution of this code?
Consider the code snippet:
typedef struct { int x; int y; } POINT;
POINT p1 = {320, 100};
POINT p2;
p2 = p1;
p1.x = 500;
What are the values of p2.x
and p1.x
after the execution of this code?
Given the structure:
typedef struct { int id; char name[20]; float score; } STUDENT;
STUDENT s1 = {1234, "John", 88.0};
STUDENT s2 = {5678, "William"};
What is the value of s2.score
after this declaration?
Given the structure:
typedef struct { int id; char name[20]; float score; } STUDENT;
STUDENT s1 = {1234, "John", 88.0};
STUDENT s2 = {5678, "William"};
What is the value of s2.score
after this declaration?
Which of the following statements about typedef
is most accurate?
Which of the following statements about typedef
is most accurate?
Consider the following code snippet:
typedef struct { int x; int y; } POINT;
POINT p1 = {320, 100};
printf("%d,%d", p1.x, p1.y);
What will be the output of the printf
statement?
Consider the following code snippet:
typedef struct { int x; int y; } POINT;
POINT p1 = {320, 100};
printf("%d,%d", p1.x, p1.y);
What will be the output of the printf
statement?
Given the following nested structure definitions:
typedef struct {
char first_name[50];
char last_name[50];
} NAME;
typedef struct {
int year;
int month;
int day;
} DATE;
typedef struct {
int id;
NAME name;
DATE dob;
} PERSON;
PERSON p1;
Which of the following statements correctly initializes the day
field of the dob
member of the PERSON
struct p1
?
Given the following nested structure definitions:
typedef struct {
char first_name[50];
char last_name[50];
} NAME;
typedef struct {
int year;
int month;
int day;
} DATE;
typedef struct {
int id;
NAME name;
DATE dob;
} PERSON;
PERSON p1;
Which of the following statements correctly initializes the day
field of the dob
member of the PERSON
struct p1
?
Consider the following code snippet involving structure pointers:
typedef struct {
int x;
int y;
} POINT;
POINT p1;
POINT *ptr = &p1;
p1.x = 10;
Which of the following expressions is NOT equivalent to accessing the x
member of the p1
structure through the pointer ptr
?
Consider the following code snippet involving structure pointers:
typedef struct {
int x;
int y;
} POINT;
POINT p1;
POINT *ptr = &p1;
p1.x = 10;
Which of the following expressions is NOT equivalent to accessing the x
member of the p1
structure through the pointer ptr
?
What is the primary purpose of a self-referential structure?
What is the primary purpose of a self-referential structure?
Given the following self-referential structure and code:
typedef struct node {
int val;
struct node *next;
} NODE;
NODE n1, n2, n3;
n1.val = 5;
n2.val = 10;
n3.val = 15;
n1.next = &n2;
n2.next = &n3;
n3.next = NULL;
NODE *head = &n1;
What will be the output of the following code snippet?
printf("%d ", head->next->next->val);
Given the following self-referential structure and code:
typedef struct node {
int val;
struct node *next;
} NODE;
NODE n1, n2, n3;
n1.val = 5;
n2.val = 10;
n3.val = 15;
n1.next = &n2;
n2.next = &n3;
n3.next = NULL;
NODE *head = &n1;
What will be the output of the following code snippet?
printf("%d ", head->next->next->val);
When a structure is passed as an argument to a function in C, what is the default behavior regarding how the structure's data is handled?
When a structure is passed as an argument to a function in C, what is the default behavior regarding how the structure's data is handled?
Flashcards
Structure Type
Structure Type
A user-defined data type containing a list of data members (elements), each with its own data type.
Size of a Structure
Size of a Structure
The sum of the sizes of all its members. Stored in contiguous memory cells, one after another.
Structure Type as Composite Data
Structure Type as Composite Data
A composite data type that combines different data types to construct structured hierarchical data types.
Structure Definition Syntax
Structure Definition Syntax
Signup and view all the flashcards
Accessing Structure Members
Accessing Structure Members
Signup and view all the flashcards
Nested Structures
Nested Structures
Signup and view all the flashcards
Structure Pointer
Structure Pointer
Signup and view all the flashcards
ptr->x vs (*ptr).x
ptr->x vs (*ptr).x
Signup and view all the flashcards
Self-Referential Structure
Self-Referential Structure
Signup and view all the flashcards
Passing Structures to Functions
Passing Structures to Functions
Signup and view all the flashcards
What is typedef
?
What is typedef
?
Signup and view all the flashcards
What is a structure?
What is a structure?
Signup and view all the flashcards
What is member access?
What is member access?
Signup and view all the flashcards
Structure assignment
Structure assignment
Signup and view all the flashcards
What are nested structures?
What are nested structures?
Signup and view all the flashcards
Study Notes
- Lecture 9 is about Structure types
- Topics include concepts of structure types, structures and pointers, structures with functions, structures and arrays, and bitwise structures
Concepts of Structure Types
- A structure type (or simple structure) is a user-defined or application-specific data type
- It consists of a list of data members (elements) and each has its own data types
- A structure type data is also called a record in applications
- The size of a structure type is the sum of the sizes of all its members
- A structure type value is stored in contiguous memory cells with its members one after another
- Operations can be applied to each member
- A structure type is a composite data type, combining different data types
- A structure type is used to construct structured hierarchical data types
Structure Type Syntax
- Use the following syntax to define a structure type:
struct struct_name { data_type1 v1; data_type2 v2; ... data_typek vk; };
- data_type1, data_type2, ..., data_typek, can be any defined data types
sizeof(struct struct_name)
is equal tosizeof(data_type1) + sizeof(data_type2) + ... + sizeof(data_typek)
- Structure definition does not allocate memory
- Structure definition defines a template for the compiler that details the types and ordering of structure members
- Use
struct struct_name struct_var
to declare structure type variables - This tells the compiler to allocate a memory block of
sizeof(struct struct_name)
bytes - Use the dot operator (or structure member operator) to represent each member as a variable
- The dot operator can set/get values or get the address of the member
- For example, access members with
struct_var.v1
andstruct_var.v2
- Initialize a structure variable using
struct struct_name struct_var = {constant1, constant2, ...};
- Initializers should match their corresponding member types in the structure definition, with the rest set to 0
Example Structure
struct student
is a struture with:int id
char name[20]
which allocates 4 bytesfloat score
which allocates 20 bytes
struct student s1;
defines a structure type named student- s1 is a variable of the
struct student
type sizeof(struct student)
orsizeof(s1)
is equal tosizeof(int) + sizeof(name) + sizeof(float) = 28 bytes
- The compiler allocates a memory block of 28 bytes to s1
id
has the lowest address, followed by name and score in the memory blocks1.id, s1.name, s1.score
is used to access members id, name, and score- For example,
s1.id = 1234; strcpy(s1.name, "john"); s1.score = 88.0;
typedef
Keyword
- The
typedef
keyword is used to define a type name - For example, rename an existing type
typedef int INT;
INT
defines a new type name for theint
type.INT a = 10;
has the same effect asint a = 10;
Type Name Example
struct student {
int id;
char name[20];
float score;
};
typedef struct student STUDENT;
struct student s1;
STUDENT s2;
sizeof(STUDENT)
gives 28- Examples:
STUDENT s1;
s1.id = 1234;
strcpy(s1.name, "John");
can't usestrcpy
(s1.name = "John”)s1.score = 88.0
STUDENT s1 = {1234, "John", 88.0};
declares and initializess1.id
holds value 1234s1.name
stores string "John"s1.score
holds value 88.0STUDENT s2 = {5678, "William"};
s2.id
holds value 5678s2.name
stores string "William"s2.score
holds value 0
Point Examples
struct point {
int x;
int y;
} p1; // define struct point and declare variable pl
struct point p2; // declare p2
typedef struct {
int x;
int y;
} POINT;
POINT p1, p2;
// what is sizeof(POINT)?
- Declare and initialize with
POINT p1 = {320, 100};
- Declare, set values, and get values:
p1.x = 320; p1.y = 100; printf("%d,%d",p1.x, p1.y); p2.x = 300; p2.y = 300; printf("%d,%d",p2.x, p2.y);
- Structure types support assignment operations
- A structure type variable can be assigned to another structure type variable of the same type and can be used for initialization
- Example:
STUDENT s1 = {1234, "John", 88.0}; STUDENT s3 = s1; // declare and initialize by value of s1 // or STUDENT s3; // declare s3 = s1; // assign value s3.id has value 1234 s3.name stores string "John" s3.score has value 88.0
Nested Structures
- A type of a structure member can be any already defined type
- A previously defined structure type can define members in a new structure type, called a nested structure
- Nested structures enable the construction of hierarchical data types
Example Nested Structures
typedef struct {
char first_name[20];
char mid_name[20];
char last_name[20];
} NAME;
typedef struct {
int dd;
int mm;
int yy;
} DATE;
typedef struct {
int id;
NAME name;
DATE dob;
} PERSON;
PERSON p1;
strcpy(p1.name.first_name, "John");
p1.dob.dd = 15;
p1.dob.mm = 3;
p1.dob.yy = 1990;
Structures and Pointers
- A structure type pointer can be declared to hold the address of a structure type variable
- Use the structure pointer operator "->" to access the member of the structure
Structure and Pointer Syntax
struct struct_name {
data_type1 v1;
data_type2 v2;
};
struct struct_name variable_name;
struct struct_name *ptr = &variable_name;
ptr->v1 is equivalent to variable_name.v1
Point pointer example
typedef struct {
int x;
int y;
} POINT p1;
POINT p1;
- The address of p1 is &p1
- The address of p1.x is &p1.x
POINT *ptr; // declare POINT type pointer
ptr = &p1; // assign the address of p1 to ptr
- Representation equivalence:
ptr->x ↔ (*ptr).x ↔ p1.x
&(ptr->x) ↔ &ptr->x ↔ &p1.x ↔ &((*ptr).x)
Self-Referential Structures
- A structure type can contain a pointer variable of its own type, called a self-referential structure
- Self-referential structures form the basis of many other data structures such as linked lists and trees.
- Example:
struct node { // this defines type name: struct node int val; struct node *next; // this define a pointer of struct node type };
- The
struct node
has two member variables: an intval
andstruct node *next
represents next is a pointer pointing to astruct node
type variable.
Example self-referential Structure
typedef struct node {
int val;
struct node *next;
} NODE;
NODE n1, n2, *p1 = &n1;
n1.val = 1;
n1.next = &n2;
n2.val = 2;
n2.next = NULL;
printf("%d", p1->val); // output: 1
printf("%d", p1->next->val); // output: 2
Structures with Functions
- Passing a structure type value to a function is the same as passing a basic type value
- The value of the structure type variable will be copied into the function as local variables
- Syntax for passing a structure variable to a function:
data_type func_name(struct struct_name argument);
void display_student(STUDENT s) {
printf("id:%d;name:%s;score:%.1f", s.id, s.name, s.score);
}
STUDENT s1={1234,"Johe",88.0};
display_student(s1); // output: id:1234;name:John;score:88.0
// whole s1 (28 bytes) is copied into function.
void display(POINT p) { printf("%d, %d", p.x, p.y); }
POINT p1={2,3};
display(p1);
// output 2,3, whole p1 (8 bytes) is copied into the function
- Passing a structure variable reference to a function is the same as passing a basic type variable to a function.
- Syntax:
data_type func_name(struct struct_name *reference);
void inc(POINT *p) {
p->x = p->x + 1;
p->y = p->x + 1;
}
POINT p1={2,3};
inc(&p1); // the address of p1 is copied to call stack
display(p1); // output: 3, 4
Structure Type as Function Return type
- When a structure type is used as a return type, the structure variable value is copied out from the scope, like a basic type variable
struct struct_name function(parameter list) {
struct struct_name local_variable = {0};
...
return local_variable;
}
struct struct_name external_variable = function(argument list);
- Example
POINT point_inc(POINT p) {
p.x = p.x+1;
p.y = p.y+1;
return p;
}
POINT p1={2,3};
POINT p2 = point_inc(p1);
display(p2); //display 3,4
Structure Arrays
- A structure array is an array in which each element is of structure type
- Representations of structure arrays are the same as basic type arrays
- Arrays of structures are used to represent, store, and process data records in application programs
- To declare a structure array, use the syntax:
struct struct_name name[length];
- Example:
STUDENT studuents[30];
students[0].id = 9;
strcpy(&students[0].name, "Peter");
Pointer Example
POINT p[10];
/* declare an array of point types */
POINT *pt;
/* define a pointer variable for structure point */
pt = &p[0];
/* assign the address of p or p[0] to pt */
pt + 1 is pointing to p[1]
(pt+1) -> x is equivalent to p[1].x
Passing a Structure Array to functions
- Passing a structure array to a function is the same as passing a basic type array
void display_all(POINT p[], int n){
int i;
for (i=0; i<n; i++) printf("%d, %d\n", p[i].x, p[i].y);
}
void display_all2(POINT *p, int n){
int i;
for (i=0; i<n; i++){printf("%d, %d\n", p->x, p->y);p++;}
}
POINT pa [2]={2,3,4,5}; // define array of 2 POINT type
display_all(pa, 2); // pass array name as reference
display_all2(pa, 2); // pass array name as pointer
POINT *p=pa;
display_all2(p, 2); // pass POINT pointer
Structures vs Arrays
- Structures and arrays store data elements in contiguous memory locations
- Arrays have elements of the same data type that are represented by a subscript index
- Structures have elements that may have different data types that are represented by the dot operator
- Structures support assignment, arrays do not
- Structures support return type, arrays do not
- Passing a structure variable to a function copies the structure data to the function's local scope
- Passing an array name to a function copies the address of the array to the function's local scope
- To copy array data to a function through arguments, define a structure type wrapping the array and pass the structure type value to the function instead
Bitwise Structures
- A bit field is the structure member used to store multiple logical values as a short series of bits where each of the single bits can be addressed separately
- A bitwise structure is a structure containing bit fields
Example bitwise structure
typedef struct packet {
unsigned a:2; // has has 2 bits
unsigned b:6; // has has 6 bits
unsigned c:4; // has has 4 bits
unsigned d:4; // has has 4 bits
int i:16; // has 16 bits
} PACKET;
PACKET data;
data.a = 3; data.b = 7; data.c = 9; data.d = 15; data.i = 256;
printf("%d, %d,%d, %d", data.a, data.b, data.c, data.d);
printf("%u, %o, %x, %d", data.a, data.b, data.c, data.d);
- Bit fields are used to represent the sign of int type
- Bit-fields is used to represent single bit flags, each with a flag stored in its own bit
- Packet headers, like the IP4 header, also use bit fields
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.