Data structure is the way you organize data in the main memory during execution of a program.
An array is a collection of similar data elements.
Declaration : int A[5];
Initialization : int B[5]={2,4,6,8,10};
If you have an array of some size and don't define the elements they will become zero by default.
A structure is a collection of data members that are related under one name.
- Defining a structure
struct Rectangle { int length; int width; };
- Size of a structure
The total size of a structure is the sum of its element in memory. An int is 4 bytes.
- Declaring a structure
struct Rectangle r;
struct Rectangle r={10,15}
with initialization
- Accessing members
Members are accessed with dot operators.
r.length = 15;
r.width = 10;
A pointer is an address variable that is meant for storing the address of data not the data itself.
A pointer allows a program to access Heap memory and resources outside of the program.
Pointers are also used for parameter passing.
Data variable == int a = 10;
Address variable == int *p;
Pointer initialization == p = &a;
Use an asterisk *
for declaration and dereferencing. The ampersand &
is is used when initializing.
Every pointer takes 8 bytes no matter the data type.
A reference is an alias given to a variable. Use an ampersand &
to declared a reference. Referencing is useful for parameter passing in small functions. A reference must be initialized when declared.
A pointer uses ->
to access a structure instead of a dot operator.
A function is a group of related instructions that performs a specific task. They are also known as modules or procedures. Breaking your program into smaller functions used inside of a main function is called Modular or Procedural programming. One function cannot access the variables of another function.