Array
Array is a collection of data of same types stored in sequential memory location. It is a linear data structure, where data is stored sequentially one after the other. The elements in an array is accessed using an index. For example, In an array of n elements, the first element has index zeroand the last element has index (n-1). Elements with consecutive index (i.e. i and i+1) are stored in consecutive memory location in the system.
Array can be divided into following types:
1. One Dimensional Array
example:
• int var [10]; (declaration)
• var [2] = 12; (adding value)
so, the value on row -> 3 = 12
(why row 3? because array always start from 0)
2. Multi Dimensional Array
example 2 dimensional array:
• int var [10] [10]; (declares an integer array with 10 elements)
• var [2] [4] = 14; (accessing an integer array with 14 value)
so, the value on coloum -> 3 and row -> 5 = 14
array have more than 2 dimension, and all of them working like the examples on the top.
There are a number of operations
that can be performed on arrays.
They are:
• Traversal
• Insertion
• Searching
• Deletion
• Merging
• SortingPointer
C++ pointers are easy and fun to learn. Some C++ tasks are performed more easily with pointers, and other C++ tasks, such as dynamic memory allocation, cannot be performed without them.
As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory. Consider the following which will print the address of the variables defined −
The two most important operators
used with pointer type are:
• & (the address operator)
• * (the
dereferencing operator)
example:
If we have the declaration:
int test;
int
*ptest;
then test is an
integer and ptest is a
pointer to an integer. If we say:
ptest = &test;
then &test returns
the address of x and
assigns it as the value of ptest.
To assign a value of x we
can say
x =
10;
or
*ptest = 10;
Data structures are used to store data in a computer in an organized form. In C language Different types of data structures are; Array, Stack, Queue, Linked List, Binary Tree, Hash Tables.
- Array: Array is collection of similar data type, you can insert and deleted element form array without follow any order.
- Stack: Stack work on the basis of Last-In-First-Out (LIFO). Last entered element removed first.
- Queue: Queue work on the basis of First-In-First-Out (FIFO). First entered element removed first.
- Linked List: Linked list is the collection of node, Here you can insert and delete data in any order.
- Binary Tree: Stores data in a non linear form with one root node and sub nodes.
- Hash Tables: Stores data in tables form.
A data type defines what kind of value a column can hold: integer data, character data, monetary data, date and time data, binary strings, and so on.
example of predefined data types are:
• Int : (4), (6), (9)
• Char : (A), (D), (C)
• Float : (1,35), (3,567546)
•Abstract
Data Type
(ADT)
is a data type that is organized in such a way that the specification of
the objects and the
specification of the operations on the objects is separated
from the representation of the
objects and the implementation of the
operations.
Question
1. How many dimension in array (max).
2. What is the different between single pointer and double pointer.
3. How many pointer we can use (max).
Answer
1. maximum dimension we can use in array are 256 dimension.
2. Printed addresses become slightly different, when new line starts. If I use static array,
addresses are exactly the same, and array elements are going one after another.
Presumably, you're talking about a static array of arrays and compare it to the array of
pointers that you have.
Elements of an array are allocated continuously in memory. In the case of array of static
arrays, the elements of the outer array are static arrays and they are stored continuously in
memory. In the case of array of pointers, the elements of the outer array are the pointers
and they are also stored continuously in memory. The arrays of int that you allocate
dynamically on the other hand are not stored in the outer array. Where they're stored is
implementation defined and typically not related to the location of the array which holds the
pointers.
Extension.
Why we need to use Double Pointer?
If you want to have a list of characters (a word), you can use
char *word
If you want a list of words (a sentence), you can use
char **sentence
If you want a list of sentences (a monologue), you can use
char ***monologue
If you want a list of monologues (a biography), you can use
char ****biography
If you want a list of biographies (a bio-library), you can use
char *****biolibrary
etc.
3.Its depend by your computer, how far your computer can process it.
Thats all thank you :)
Tidak ada komentar:
Posting Komentar