0% found this document useful (0 votes)
10 views7 pages

Known As "Call by Values." Known As "Call by References.: Advantages of Arrays

Uploaded by

dhritipal515
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Known As "Call by Values." Known As "Call by References.: Advantages of Arrays

Uploaded by

dhritipal515
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Call by value Call by reference

While calling a function, when you While calling a function, in


pass values by copying variables, it isprogramming language instead of
known as “Call By Values.” copying the values of variables, the
address of the variables is used it is
known as “Call By References.
In this method, a copy of the variable In this method, a variable itself is
is passed. passed.
Values of variables are passed using a Pointer variables are required to store
straightforward method. the address of variables.
Original value not modified. The original value is modified.
Actual and formal arguments Actual and formal arguments
will be created in different will be created in the same
memory location memory location
Advantages of Arrays
Below are some advantages of the array:
• In an array, accessing an element is very easy by using the index
number.
• The search process can be applied to an array easily.
• 2D Array is used to represent matrices.
• For any reason a user wishes to store multiple values of similar
type then the Array can be used and utilized efficiently.
• Disadvantages of Arrays
• Array size is fixed: The array is static, which means its size is
always fixed. The memory which is allocated to it cannot be
increased or decreased.
• Array is homogeneous: The array is homogeneous, i.e., only
one type of value can be store in the array. For example, if an
array type “int“, can only store integer elements and cannot
allow the elements of other types such as double, float, char so
on.
• Array is Contiguous blocks of memory: The array stores data
in contiguous(one by one) memory location.
• Insertion and deletion are not easy in Array: The operation
insertion and deletion over an array are problematic as to insert
or delete anywhere in the array, it is necessary to traverse the
array and then shift the remaining elements as per the
operation. This operation cost is more.

Definition of array:
An array is defined as the collection of similar type of data items stored at contiguous
memory locations. Arrays are the derived data type in C programming language which can
store the primitive type of data such as int, char, double, float, etc. It also has the capability
to store the collection of derived data types, such as pointers, structure, etc. The array is the
simplest data structure where each data element can be randomly accessed by using its
index number.
Declaration of C Array

We can declare an array in the c language in the following way.

data_type array_name[array_size];

Now, let us see the example to declare the array.

int marks[5];

Here, int is the data_type, marks are the array_name, and 5 is the array_size.

How is an array name interpreted? How it is passed to a function?

All array names will implicitly convert to a pointer to the first element in the array. So,
when passing an array to a function, we must also pass the array length as a
separate argument because the pointer alone cannot tell you how many elements
were actually allocated to the array, let alone how many are currently in use.
However, there are some exceptions. For example, a null-terminated string
argument does not require a length argument as the null-terminator denotes the end
of the character array. User-defined arrays can use a similar technique, using any
"unused" or "invalid" value or token to denote the end of the array.

Structure Union

The struct keyword is used to define a structure. The union keyword is used to define union.

When the variables are declared in a structure, When the variable is declared in the union, the
the compiler allocates memory to each variables compiler allocates memory to the largest size
member. The size of a structure is equal or variable member. The size of a union is equal
greater to the sum of the sizes of each data to the size of its largest data member size.
member.

Each variable member occupied a unique Variables members share the memory space of
memory space. the largest size variable.

Changing the value of a member will not affect Changing the value of one member will also
other variables members. affect other variables members.

Each variable member will be assessed at a time. Only one variable member will be assessed at
a time.
What is Data Structure?
The data structure name indicates itself that organizing the data in memory. There are
many ways of organizing the data in the memory as we have already seen one of the
data structures, i.e., array in C language. Array is a collection of memory elements in
which data is stored sequentially, i.e., one after another. In other words, we can say
that array stores the elements in a continuous manner. This organization of data is
done with the help of an array of data structures.

various ways to organize the data in memory like

• Array
• Stack
• Queue
• Linked List
• Trees
• Hashing

Purpose of Scanf in C

In C programming language, scanf is a function that stands for Scan


Formatted String. It reads data from stdin (standard input stream i.e. usually
keyboard) and then writes the result into the given arguments.
• It accepts character, string, and numeric data from the user using
standard input.
• Scanf also uses format specifiers like printf.
Syntax:
int scanf( const char *format, … );
Here,
•int is the return type.
•format is a string that contains the type specifiers(s).
• “…” indicates that the function accepts a variable number of
arguments.
Example type specifiers recognized by scanf:
%d to accept input of integers.
%ld to accept input of long integers
%lld to accept input of long long integers
%f to accept input of real number.
%c to accept input of character types.
%s to accept input of a string.
Example:
int var;
scanf(“%d”, &var);
Comparison between scanf() with getchar0 function

scanf is a C function to read input from the standard input until encountering
whitespace, newline or EOF.

getchar is a C function to read a character only from the standard input


stream(stdin), which is the keyboard.

C programming function arguments


C programming function arguments also known as parameters are the variables that will
receive the data sent by the calling program. These arguments serve as input data to the
function to carry out the specified task.
Description of C programming function arguments

Here, as shown in the figure above arguments_value is used to send values to the
called program.

Function arguments in c programming


Basically, there are two types of arguments:

• Actual arguments
• Formal arguments

The variables declared in the function prototype or definition are known as Formal
arguments and the values that are passed to the called function from the main
function are known as Actual arguments.

The actual arguments and formal arguments must match in number, type, and
order.
Following are the two ways to pass arguments to the function:

• Pass by value
• Pass by reference

What is need of return statement?


The return statement returns the flow of the execution to the function from
where it is called. This statement does not mandatorily need any
conditional statements. As soon as the statement is executed, the flow of
the program stops immediately and returns the control from where it was
called. The return statement may or may not return anything for a void
function, but for a non-void function, a return value must be returned.
Syntax:
return[expression];
Recursion Iteration
Function calls itself. A set of instructions repeatedly
executed.
For functions. For loops.
Through base case, where there When the termination condition for
will be no function call. the iterator ceases to be satisfied.
Used when code size needs to be Used when time complexity needs to
small, and time complexity is not an be balanced against an expanded
issue. code size.
Smaller code size Larger Code Size.
Very high(generally exponential) Relatively lower time
time complexity. complexity(generally polynomial-
logarithmic).
What is Recursion?
The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called a recursive function.
Using a recursive algorithm, certain problems can be solved quite easily.
Examples of such problems are Towers of Hanoi
(TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
A recursive function solves a particular problem by calling a copy of itself
and solving smaller subproblems of the original problems.
Distinguish between malloc() and calloc() function?
malloc() calloc()
It is a function that creates one block It is a function that assigns more than
of memory of a fixed size. one block of memory to a single
variable.
It only takes one argumemt It takes two arguments.
It is faster than calloc. It is slower than malloc()
It has high time efficiency It has low time efficiency
It is used to indicate memory allocation It is used to indicate contiguous
memory allcoation
The Dynamic memory allocation enables the C programmers to allocate memory at
runtime.
The different functions that we used to allocate memory dynamically at run time are

• malloc () − allocates a block of memory in bytes at runtime.
• calloc () − allocating continuous blocks of memory at run time.
• realloc () − used for reduce (or) extend the allocated memory.
• free () − deallocates previously allocated memory space.
C Dynamic Memory Allocation can be defined as a procedure in which the
size of a data structure (like Array) is changed during the runtime.

C malloc() method

The “malloc” or “memory allocation” method in C is used to dynamically


allocate a single large block of memory with the specified size. It returns a
pointer of type void which can be cast into a pointer of any form. It doesn’t
Initialize memory at execution time so that it has initialized each block with the
default garbage value initially.

Syntax:
ptr = (cast-type*) malloc(byte-size)
For Example:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400 bytes of
memory. And, the pointer ptr holds the address of the first byte in the
allocated memory.
If space is insufficient, allocation fails and returns a NULL pointer.

C calloc() method

1. “calloc” or “contiguous allocation” method in C is used to


dynamically allocate the specified number of blocks of memory of
the specified type. it is very much similar to malloc() but has two
different points and these are:
2. It initializes each block with a default value ‘0’.
3. It has two parameters or arguments as compare to malloc().
Syntax:
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-size is the size of each
element.
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements each
with the size of the float.
If space is insufficient, allocation fails and returns a NULL pointer.

C free() method

“free” method in C is used to dynamically de-allocate the memory. The


memory allocated using functions malloc() and calloc() is not de-allocated on
their own. Hence the free() method is used, whenever the dynamic memory
allocation takes place. It helps to reduce wastage of memory by freeing it.
Syntax:
free(ptr);

C realloc() method

“realloc” or “re-allocation” method in C is used to dynamically change the


memory allocation of a previously allocated memory. In other words, if the
memory previously allocated with the help of malloc or calloc is insufficient,
realloc can be used to dynamically re-allocate memory. re-allocation of
memory maintains the already present value and new blocks will be
initialized with the default garbage value.
Syntax:
ptr = realloc(ptr, newSize);

where ptr is reallocated with new size 'newSize'.


If space is insufficient, allocation fails and returns a NULL pointer.

You might also like