Professional Documents
Culture Documents
C Lec 101-105
C Lec 101-105
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;
int a[n];
int i;
printf("Enter al1l the %d elements: \n", n);
for (i=0; i<n; i++)
return
NEŠO ACADEMY
;
printf("%d ", a[i]);
ADVANTAGES
Example :
int a[3*i+5];
int a[k/7+2];
int a[i+j];
NESO ACADEMY
PoINTS TOBE NOTED
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
is
(all
Address contents
20 Bytes
NESO ACADEMY
IMPORTANT POINTS
1002
NESO ACADEMY
C
PROGRAMMING AND
DATA STRUCTURES
Declaring and initializing
pointets in C
NESO ACADEMY
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:
NESO ACADEMY
LETSTRY TO UNDERSTAND HOW TO INITIALIZE A POINTER
VARIABLE
NESO ACADEMY
NEED OF ADDRESS OF OPERATOR
NESO ACADEMY
int x = 5;
ptr
int *ptr; 1000
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
For example:
int *ptr;
printf(“%d", *ptr);
Output:
NESO ACADEMY
ONE MORE...
New Notification
int *ptr;
Usuolly.
*ptr = 1; segmentation
fault is caused by
program trying to
Output: read or write an
illegal memory
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
q= P;
1000
printf("%d %d", *p, *q);
OUTPUT: 1010
NESO ACADEMY
NOTE: q = p is not same as *q = *p
10
1000
i 1000 1000
10
1000 20
1000
2000 2000
NESO ACADEMY
NOTE: q =p is not same as *q = *p
-
*q= *p; i
10
1000
1000 1000
10
1000 10
1000
2000 2000
NESO ACADEMY