Known As "Call by Values." Known As "Call by References.: Advantages of Arrays
Known As "Call by Values." Known As "Call by References.: Advantages of Arrays
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
data_type array_name[array_size];
int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size.
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.
• Array
• Stack
• Queue
• Linked List
• Trees
• Hashing
Purpose of Scanf in C
scanf is a C function to read input from the standard input until encountering
whitespace, newline or EOF.
Here, as shown in the figure above arguments_value is used to send values to the
called program.
• 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
C malloc() method
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
C free() method
C realloc() method