C
PROGRAMMING AND
(DATA STRUCTURES
Variable length arrays in C
NESO ACADEMY
int main() {
int a[9] = {34, 56, 54, 32, 67, 89, 90, 32, 21};
int i:
1/0riginal order
for(i=0; i<9; i++) { Reversea series of numbers
printf("%d", a[i]) ; (Old Program)
printf("\n") ;
//Reverse order
for(i=8; i>=0; i--) {
printf("%d ", a[i]);
}
return 0;
NES ACADEMY
#include <stdio.h>
Reversea series of numbers
int main() { (New Program)
int n;
printf("Enter the number of elements you want to reverse: \n");
scanf ("%d", &n);
int a[n];
int i;
printf("Enter al1l the %d elements: \n", n);
for (i=0; i<n; i++)
scanf("%d ", &a[i]);
printf("Elements in reverse order are: \n");
for (i=n-1; i>=0; i--)
return
NEŠO ACADEMY
;
printf("%d ", a[i]);
ADVANTAGES
At the time of execution, we can decide the length of the array.
No need to choose the fix length while writing the code.
Even arbitrary expressionsare possible
Example :
int a[3*i+5];
int a[k/7+2];
int a[i+j];
NESO ACADEMY
PoINTS TOBE NOTED
. Variable length arrays cannot have static storage duration.
(2.) Variable Length Array does not have theinitializer.
NESO ACADEMY
Introduction to Pointers in C>
Neso Academy
C
PROGRAMMING AND
DATA STRUCTURES
0:02 / 2:51
Introduction to pointers in C
More vicdeos
Declaring & Initializing Pointers Call By Value & Definition of Array
General syntax for declaringpointervariables:
Call By Reference An Array a data structurecontaining a number of dato valves of which
is
(all
Address contents
1000 0010 0011
1001 0101 011
1002 0001 010
i
1003 0010 100
1019 0001 0001
20 Bytes
NESO ACADEMY
IMPORTANT POINTS
Pointer is aspecial variable that is capable of storing somne address.
It points toa memory location where the first byte is stored
1002
NESO ACADEMY
C
PROGRAMMING AND
DATA STRUCTURES
Declaring and initializing
pointets in C
NESO ACADEMY
SYNTAX FOR DECLARING POINTER VARIABLES
General syntax for declaring pointer variables:
data_type *pointer_name
HERE DATA TYPE REFERS TO THE TYPE OF THE VALUE THAT THE
POINTER WILL POINT TO.
NESO ACADEMY
For example:
int*ptr: Points to integer value
char*ptr, Points to character value
float *ptr: Points to float value
NESO ACADEMY
LETSTRY TO UNDERSTAND HOW TO INITIALIZE A POINTER
VARIABLE
NESO ACADEMY
NEED OF ADDRESS OF OPERATOR
Simply declaring a pointer is not enough.
It isimportant to initialize pointer before use.
One waytoinitialize a pointer is to assign address of some variable.
NESO ACADEMY
int x = 5;
ptr
int *ptr; 1000
ptr = &x; 2000
address of
operator
NESO ACADEMY
int x = 5;
5 ptr
int *ptr; 1000
1000
ptr = &x; 2000
address of
operator
NESO ACADEMY
int x = 5;
int *ptr; is equivalent to int x = 5, *ptr = &x;
ptr = &x;
NESO ACADEMY
C
PROGRAMMING AND
DATA STRUCTURES
Value of Operator
NESO ACADEMY
Value of operator/indirection operator/dereferenceoperator is an operator that
is used to access the valuestored atthe location pointed by the pointer.
int x = 5;
It says go to the
address of object
L
and takewhat is ptr
stored in the object 1000
int *ptr;
1000
ptr = &x; 2000
printf("%d", *ptr);
VALUE OF
OPERATOR
NESO ACADEMY
Value of operator/indirection operator/dereferenceoperator is an operator that
is used to access the valuestored atthe location pointed by the pointer.
int x = 5; 5 ptr
int *ptr; 1000
1000
ptr = &x; 2000
printf("%d", *ptr);
Output: 5
VALUE OF
OPERATOR
NESO ACADEMY
We can also changethe valueofthe objectpointed by the pointer.
For example:
X
int x = 10;
int *ptr = &x; 10
ptr
1000
*ptr = 4; 1000
2000
NESO ACADEMY
We can also changethe value ofthe object pointed by the pointer.
For examnple:
X
int x = 10;
int *ptr = &x; 4
ptr
1000
*ptr = 4; 1000
printf(%d", *ptr); 2000
Output: 4
NESO ACADEMY
A WORDOF CAUTION
/A Never apply the indirection operator to the uninitialized pointer
For example:
int *ptr;
printf(“%d", *ptr);
Output:
Undefined behaviour YOULL REGRE I MAN
NESO ACADEMY
ONE MORE...
/ Assigningvalue to an uninitialized pointer
dangerous.
is
New Notification
int *ptr;
Usuolly.
*ptr = 1; segmentation
fault is caused by
program trying to
Output: read or write an
illegal memory
Segmentation Fault (sIGSEGV) location.
NESO ACADEMY
C
PROGRAMMING AND
(DATA|STRUCTURES
Pointer assignment
NESO ACADEMY
P
i
int i = 10;
int *p, *q; 10
1000
NESO ACADEMY
1000 i
int i = 10;
int *p, *q; 10
p = &i; 1000
NESO ACADEMY
1000
int i 10; i
int *p, *q; 10
p = &i; 1000
q= P;
1000
printf("%d %d", *p, *q);
OUTPUT: 1010
NESO ACADEMY
NOTE: q = p is not same as *q = *p
int i = 10; int i = 10, j = 20;
int *p, *q; int *p, *q;
p = &i; P = &i;
q = &j:
i
10
1000
i 1000 1000
10
1000 20
1000
2000 2000
NESO ACADEMY
NOTE: q =p is not same as *q = *p
int i = 10; int i = 10, j 20;
int *p, *q; int *p, *q;
p = &ij P = &i;
q = &j;
q = P
-
*q= *p; i
10
1000
1000 1000
10
1000 10
1000
2000 2000
NESO ACADEMY