A9 - derived data types
arrays
array
- a collection of values with the same time, represented by a single name, and an index that allows the selection of individual members
type array_name[array_length] = {elememnt1, element2};
int arr[5] = {1, 2, 3, 4, 5};
int arr_2D[3][2] = {1, 2},
{3, 4},
{5, 6};
pointer
pointer
- a type, whose value is the address of another variable
#include <stdio.h>
int main ()
{
float var1 = 5;
float *p = &var1; /* points to var1 */
printf("%f\n", p); /* address of pointer p */
printf("%f\n", *p); /* value of pointer p */
}
function
function
- a group of instructions to perform a given task
#include <stdio.h>
#include <stdint.h>
/* function declaration */
int calculateSum(int x, int y);
int main ()
{
int calc = calculateSum(1, 2);
print("%d\n", calc);
}
/* function implementation */
int calcualteSum(int x, int y)
{
return x + y;
}
structure
structure
- a collection of variables of possibly different types, into a single user defined type
struct address {
char name[100];
char street[100];
char city[50];
int pin;
}
struct address add1 = {"abode", "this street", "that town", 12345}; /* assign values */
add1.name = "residence" /* edit a value */
int num = add1.pin /* access a value */
// access using a pointer
struct address add2; /* define a new address */
add2_ptr = &add2; /* assign a pointer for the structure */
add2_ptr->name = 'work' /* assign values */
union
union
- a collection of variables of possibly different types into a single user defined type, stored in the same memory location
- only one property can be initialised at a time
union address {
char name[100];
char street[100];
char city[50];
int pin;
}
union address add1 = {"abode"};
strcpy(add1.street, "this street");