Array:
Array is a type of data structure that we could find in programming which are used to store a fixed-size sequential data that are similar to one and another. When you think of array, think of it as a box filled with the same materials as in like boxes of books, boxes of glass, etc.
There are multiple types of array. For example a single dimension array, double dimension array, triple, and so-on. The way to use it are similar to one another and we should see it down below.
There are multiple types of array. For example a single dimension array, double dimension array, triple, and so-on. The way to use it are similar to one another and we should see it down below.
Delaration:
To declare array in C just simply:
type(int/char/double/float/etc) nameofArray[arraySize];
ex. int Numbers[5];
To make a different dimension array just add more sizes to the variable.
ex. int Numbers[5][5]; [2D]
ex. int Numbers[5][5][5]; [3D]
and so-on.
The way you should see these array are like tables and graphs. For single dimension array are the most simplest of them all. For 2D array, you must imagine a table which have a column and row where the first index declaration means for the row and the second one is for the column. For 3D array, its like an x, y, z graph which gets more complicated as the number of dimension increases. As it is too complicated I will further explain just with the single dimension array.
Initializing Array:
Sometimes we may make a program in which we will need the person's input and later on is stored within the array. But in this lesson I will teach the simplest way of initializing array.
To initialize just simply:
int Numbers[5] = {0, 30, 40, 10, 20};
Access:
Acessing an array is just like calling a variable but instead you also need to call out the arraySize or index number.
ex. printf("%d", Number[1]);
and the result will be 30.
ex. printf("%d", Number[1]);
and the result will be 30.
========================================================================
Pointer:
Pointers are one of the main things that you need to be able to use if you're aiming to become a professional programmer. Pointers are used to point towards an address of a certain variable. Many programmers use pointers for memory allocation, without pointer we may not be able to use memory allocation. The reason why will be clearly shown later on in the second part of this lesson when I will explain about Linked List.
Declaration:
Pointers are easy to remember. It uses the symbol (*).
ex. int *a;
How to use Pointers?
Things you need to remember when you're using a pointer:
- Define a pointer variable,
- Assign the address of the variable to the pointer,
- Access the value in the address assign towards the pointer.
Source Code:
#include <stdio.h> int main () { int var = 20; /* actual variable declaration */ int *ip; /* pointer variable declaration */ ip = &var; /* store address of var in pointer variable*/ printf("Address of var variable: %x\n", &var ); /* address stored in pointer variable */ printf("Address stored in ip variable: %x\n", ip ); /* access the value using the pointer */ printf("Value of *ip variable: %d\n", *ip ); return 0; }
From the above code, you should be getting this result:
Address of var variable: bffd8b3c Address stored in ip variable: bffd8b3c Value of *ip variable: 20
========================================================================
So that is the end of part 1. I will be posting the second part by the end of today so don't forget to check it out later. Hopefully this lessons will help you in someway. I will see you guys next time. Take care~
No comments:
Post a Comment