You are on page 1of 9

BTech 1St year [JNTU syllabus] study material(CDS)

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.

· 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

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;

Ø *---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

E.g. int *p;

Initializing:

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

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

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

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

P1++

--P2

Sum+=*p2

Comparisons:

P1 > p2

P1 ! = P2

P1=-P2

Not valid:

P1/p2 or p1*p2 or p1/3

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

Length of various data types:

Characters 1 byte

Integers 2 bytes

Floats 4 bytes

Double 8 bytes

Declaration:

E.g. :

They are stored as

10
7

0123456789

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)

Initialization of arrays:

Static int total [5] = {1, 2, 4};

* 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.

Drawbacks:

1. There is no convenient way to initialize only selected elements.

2. There is no shortcut method for initializing a large number of array elements like the one
available in FORTRAN.

Scanning an array:

Scanf (“%d”, &x);

}

There will be no error message to warn us that we are going beyond the array size. In some
cases, the computer may just hang. thus; the following program may turn out to be suicidal.

C performs no bounds checking and, therefore, care should be exercised to ensure that the array
indices are within the declared limits

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;

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 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.