CONTENTS Variables Placeholders Arrays Pointers Types of Pointers Arrays using Pointers
Variables are simply names used to refer to some location in memory – a location that holds a value with which we are working. Variable names may consists of letters, digits, and the underscore character subject to the following conditions. Eg :- avg , count , emp_salary , num1, etc. Variables
Rules of declaring V ariables Commas or blanks are not allowed. No special characters allowed. First letter of the variable should be a letter. Uppercase and lowercase letters are significant. Variable name should not be a C keyword. It should not have same name as the function thst is written by the user or already exist in the C library. Each variable used must be declared in the program.
Placeholders Placeholders are used to determine when what type of values are going to be input or displayed , depending on what type of functions that we use. Data type Placeholder int %d Char %c float %f string %s
Input Placeholders: scanf () function requires input placeholders to allow data to be transferred to the specific variable depending on which placeholder we will use. Output Placeholders: Output placeholders are used to display data onto the screen. printf () function uses the output placeholder when necessary. The only difference with the input placeholders is the double data type placeholder for input function is %lf, while the double data type placeholder for output function is %f.
Arrays An array is fixed size sequential collection of elements of homogenous nature. Array Declaration:- data_type arrayname [size]; eg - int arr [20]; /*defines a block of 20 consecutive objects of type int */ Array Initialization:- int a[5] = { 1,2,3,4,5}; int a[ ] = {1,2,3,4,5}; Types:- One dimensional Multi dimensional
One Dimensional Arrays A collection of variables are given one variable name using only one subscript and such a variable is called a single-subscripted variable or one dimensional array. Syntax:- data_type ArrayName [size]; data_type : is a valid data type like int , char, or float. ArrayName : is a valid identifier. Size: maximum no. of elements that can be stored in an array. Initialization:- int arr [5]; Stored in contiguous memory locations.
Multi Dimensional Arrays Two dimensional Array: Syntax: data_type ArrayName [ rowsize ][ columnsize ]; data_type : is a valid data type like int , char, or float. ArrayName : is a valid identifier. rowsize and columnsize : maximum no. of elements that can be stored in the row and the column. Initialization:- int arr [2][2]; int arr [2][2] = {1,2,0,1}; int arr [3][3] = {{1,2,0},{1,4,6},{3,8,3}};
Pointers A pointer variable is another variable that holds the address of the given variable to be accessed. Pointers provide a way of accessing a variable without referring to variable directly. Declaration:- type ∗ pt_name ; Initialization:- int x, ∗p = & x; A pointer should be initialized before use. The unary operator ∗ is termed as dereference operator or indirection operator, as it allows to access the value of a variable indirectly.
Types Of Pointers Null Pointer :- Null pointer is a constant with a value of zero defined in several standard libraries. Eg:- int *ptr = NULL Dangling Pointers :- If any pointer is pointing to any address at given point in a program and later on the variable has been deleted from that specific memory location, although the pointer is still referring to the memory location. These pointers are called dangling pointers. Eg :- int *x,* y; x = (int *)malloc(sizeof (int)); *y=10; x = y; free (y);
GenericPointers :- Pointers which doesn’t have any specific data type is known as generic pointers. Eg :- void *the_data; Wild Pointers :- Uninitialized pointers are known as wild pointers because they point to some arbitrary memory location and may cause a program to crash or behave badly. Eg:- int main ( ) { int *ptr; printf(“%d”,*ptr); }
#include <stdio.h> int main() { int data[5], i; printf("Enter elements: "); for(i = 0; i < 5; ++i) scanf("%d", data + i); printf("You entered: \n"); for(i = 0; i < 5; ++i) printf("%d\n", *(data + i)); return 0; } Arrays Using Pointers Output : Enter elements: 1 2 3 5 4 You entered: 1 2 3 5 4
Difference between Arrays and Pointers POINTERS ARRAY Pointer is a variable which stores the address of another variable. Array is a collectihon of homogeneous data elements. Pointer can’t be initialized at definition. Arrays can be initialized at definition. They are static in nature. They are static in nature. The assembly code of pointer is different that array . The assembly code of an array is different than pointer.