swf

Tuesday, April 8, 2008

Arrays & pointers:


Arrays
· An array is a group of related data items that share a common name
· C performs no bounds checking and, therefore, care should be exercised to ensure that the array indices are within the declared limits
· Remember that all elements of any given array must be of same type. I.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats.
· The first element in the array is numbered 0, so the last element is 1 less than the size of the array.
· An array is also known as a subscripted variable
· Before using an array , its type and dimension must be declared
· However big an array, its elements are always stored in contiguous memory locations.{like memory address 1000 ,1002 ,1004…..in same order}

Pointers:
-A pointer is a variable which represents the address of the variable rather than its value.
- In the given expression below the variable ‘pv’ is called a pointer to ‘v’, since the variable points the location where the variable ‘v’.
pv=&v;
- &--Represents the “address “of the variable.
- *---Represents “the data stored at the variable’s address“.
- *---It’s called the indirection operator and is a unary operator.
-Pointer variables can point to numeric or character variables, arrays, functions or other pointer variables.
- We can assign a pointer variable to another pointer, (e.g., pv=px), provided both the pointer variables are of same data type.

Pointer Declaration:
-A pointer declaration can be shown as follows:
(Data-type) *(variable)
-We cannot assign a constant i.e., an integer to a pointer, but we can assign ‘0’ to it by assigning the symbolic constant NULL at the time of pointer declaration.
- Since, the arrays are internally created pointers, there is no need of “ampersands(&)” before the ‘variable of array’ in the ‘scanf’ statements, provided when the array name is mentioned in the scanf statement, for this the example is given below

Scanf(“%d”,&a); is wrong

Scanf(“%d”,a); is correct
Declaration:
Data type *pt_name;
This tells the compiler three things about the variable pt_name
1. the asterisk(*) tells that the variable pt_name is a pointer variable
2. pt_name needs a memory location
3. pt_name points to a variable of type data type
E.g. int *p;

Initializing:
P = &quantity
(We must ensure that the pointer variables always points the corresponding type of data)
E.g. float a, b;
Int x,*p;

p=&a
b=*p
Output = errors
Compiler will not detect such errors; care should be taken to avoid wrong pointer assignments

A pointer variable can be initialized in its declaration itself
E.g. int x,*p = &x; ----valid----
Int *p = &x, x; (not valid)

Valid statements
X = *(&x) = *p = y
&x = &*p
X = 10;
P = &x;
*p = 25 (this will change the value of x = 10 to x = 25)

Pointer expressions:
For example: if p1 and p2 are properly declared and initialized pointers, then the following statements are valid

Y = *p1**p2; same as (*p1)*(*p2)
Sum = sum +*p1;
Z = 5*-*p2/ *p1; same as (5*(-(*p2)))/(*p2)
Note that there is a blank space between / and * in the item 3 above

Z = 5*-*p2/*p1; -----wrong
If the is no blank space between / and * then it will be considered as beginning
of a comment

These are valid:
Arithmetic:
P1+4
P2-2
P1-p2
If p1 & p2 are both pointers to the same array then p2- p1 gives the number of elements between p1 and p2
Short hand –operators:
P1++
--P2
Sum+=*p2
Comparisons:
P1 > p2
P1 ! = P2
P1=-P2
Not valid:
We may not use pointers in division or multiplication
P1/p2 or p1*p2 or p1/3
We cannot add two pointers
P1+p2
Pointers increment and scale factor:
Increment
P1=p2+2
P1=p1+1
P1++ = will cause the pointer p1 to point to the next value of its type
(1.e. for int *p1 p1 = 2000 p1++=2002)



Length of various data types:
Characters 1 byte
Integers 2 bytes
Floats 4 bytes
Long integers 4 bytes
Double 8 bytes

One dimensional array:
Declaration:
Type variable-name [size]
Int group [10]
E.g. :
If the numbers are 1, 2,3,4,5,6,7,8,9,10
They are stored as

0 1 2 3 4 5 6 7 8 9
x[0]=1 x[1]=2………..x[9]=10

Float height [25]

Char name [10] (** when declaring character arrays, we must always allow one extra element space for the null terminator)
w e l c o m e \0


Initialization of arrays:
Static type array-name [size] = {list of values};
Static int number [3] = {1, 2, 3}; 1 2 3
Static int total [5] = {1, 2, 4}; 1 2 4 0 0

If the number of values in the list is less than the number of elements ,then only that many elements will be initialized .the remaining elements will be set to zero automatically only if storage class is static otherwise garbage value gets stored.

Static int counter [] = {1, 1, 1, 1};
Static char name [] = {‘j’,’o’,’h’,’n’};

Drawbacks:
There is no convenient way to initialize only selected elements.
There is no shortcut method for initializing a large number of array elements like the one available in FORTRAN.
Scanning an array:

For ( i =0; i <>

The base address of a single dimensional array can be expressed as: Simply ‘X’ or X[0];


When we need the address of the second array element we can simply write in the given form as shown below: (x+i) or &x[i]; Where I represents the element present in the ith place.


When the value of I increased the program skips the number of bytes depending on the data type present.


Ø We also can write the above format in pointers also as shown below: X[i] or *(x+i);


Malloc Function: Ø Malloc is the function which accepts a number (which is the size of the array) and reserves that many ‘bytes’ of memory (base on the ‘data type’) during runtime process and that reserved memory is assigned to the given array.


Ø The mechanism of reserving the required amount of memory during the runtime process is known as ‘Dynamic Memory Allocation’.


E.g. variable=(data type *)malloc(sizeof(data type)*variable1);


Call by Value: Ø When a parameter is sent to a function by using call by value technique a duplicate copy of these values (i.e., actual parameters) are assigned to the corresponding formal parameters. Ø Since the formal parameters has the duplicate copy of values, their will be no effect on the parameters used in the main function when the values of formal parameters are modified.


Call by Reference: Ø When a parameter is sent to a function by using call by reference technique, the address of the given variable is stored in the corresponding pointer variable.


Ø So, any modifications made to the pointer variable present in the user defined function, modifies the values stored at the address given to the pointer variable.


Ø So, even after the termination of the user defined function, the modified values can be accessed in the main function (unlike in call by value).


Passing an Array to a Function: Ø Arrays are multidimensional elements, since we cannot return multiple values by using call by value technique to the main function;


we always use the call by reference technique in case of arrays.


Ø In case of call by reference technique we write an ‘ampersand (&)’ before the actual parameters in the calling functions. But, in case of arrays it is different. Since the arrays are the internally created pointers, we directly pass the address of the array by giving the name of the array in the calling function.


Ø We can declare two types of data type declarations in the formal parameters, they are: · Declaring an array in the formal parameters without specifying the size of the array (since, it automatically receives the size of the array from main). ·


Since the actual parameters sent the address to the formal parameters, we can declare pointers in the formal parameters.

No comments: