Saturday, August 31, 2019

CS2100: Structure and Array

Array

Array is size fixed and cannot be changed
We declare array like 
int arr[5] = { 1,2,3,4,5}
If we leave the [] empty, compiler will count for us.

In the memory, the name refers to the address of the first element. The elements are also consecutive.
This is similar in java and python.
When passing it to an function, we need to include the size of the array because we find out the array size without loop counting.

Pointers and Arrays

If we assign a pointer to an array, e.g
int ia[] = {1,2,3} //ia = &(ia[0])
int *ptr;

ptr[1] = 333 //we are changing the value 2 -> 333 in index 1

ptr = &(ia[1]); //pointer points to the address of first index
ptr[1] = 4444; // we are changing the index 2 of the array as it offsets from the first index

// we can send parts of the array without changing the code

A function call for an array is equivalent to a pointer
e.g print(int a[]) // int a[] is equivalent to int *a

Character and String

A char is one byte, 8 bit and are interchangeable with numbers.
We can convert a small case to upper case by addition.
e.g
'e' - 'a' + 'A' will change it to 'E' , we count the distance between small letter with small a and add it to large A to get the upper case equivalent
Strings are represented as character arrays
We can find out the length of string with the strlen function but we need to include the string.h library

We use a string.h because a character array is not good due to it being too troublesome to handle each character individually.

We use a special character (null) '\0' at the back of a character array to indicate that it is a string.
This is necessary for strlen function if not it wont work
When declaring the size of string, must always remember to count the terminator

Even an empty string is '\0'

String Functions


Structure

A structure is like a bag to collect stuff.

Each structure is like declaring your own data type.
E.g fraction:
struc Fraction{
     int num; 
     int den;
}[1]// this is not initialisation

We can also make use of tags at the bottom in [1], tags allow us to declare without the use of struc in the main.

using:
struc Fraction frac1 = {0};
// we initializing the first variable num with 0
To access it we use
frac1.num //returns 0
we can do reading by
scanf("%d%d", &(frac.num), &(frac.den))

Behavior:
Structure variable contains multiple fields as defined in the structure
Each structure has an independent set of the fields
The fields are place in adjacent location in the memory



Structure variable is passed by value

-> It will not affect if change as a copy of the actual argument will be made
#we can use this cheat to make it copy the array for us

Structure: Passed by address

We can still pass by address by using &
e.g
print(&myfrac);

void print(struct Fraction *ptr){
//some code
printf("%d%d", (*ptr).num)
}

but we can use ptr-> num to replace (*ptr).num

Structure and arrays can be combined together
- Array of structures
Array of fraction, students
- Structure with array as field
- Structure with structure as field


Memory address in Stuct:
Memory allocation
Stuct Padding

<Prev                        Next>

No comments:

Post a Comment