Advanced Concepts of C and Introduction To Data Structures
Advanced Concepts of C and Introduction To Data Structures
Structures.
Arrays.
Pointers.
Advantages and disadvantages of a pointer.
Usage of pointers.
Difference between arrays and pointers.
Passing parameters – by value, by reference and by
address.
Dynamic allocation and de-allocation of memory.
Dynamic arrays.
Scope and lifetime of a variable.
Unions.
Enumerations.
1.1. Introduction
1
Data Structures with ‘c’
and later remembering them is a tedious process. It would be
easier for us if we could give a single name as a parent name
to refer to all the identifiers of the same type. A particular
value is referred using an index, which indicates whether the
value is first, second or tenth in that parents name.
1.3. Arrays
e.g.
int A[10];
| ----------------- Array----------------|
A[0]A[1] A[9]
2
Data Structures with ‘c’
where, SIZE is given by the user, the index value changes from
0 to SIZE-1.
#define SIZE 10
#define SIZE 10
main()
{
int arr[SIZE], i;
3
Data Structures with ‘c’
e.g.
e.g.
arr[get_value()] = somevalue;
e.g.
int arr[4][3];
e.g.
int arr[10][10];
for(int i = 1;i< = 10;i++)
for(int j = 1; j< = 10;j++)
{
arr[i][j] = i+j;
}
4
Data Structures with ‘c’
Now let us see how members of matrices are initialized
directly.
e.g.
int arr[4][3] = {{0,1,2},{3,4,5},{6,7,8},{9,10,11}};
1.8. Pointers
data_type *ptrname;
5
Data Structures with ‘c’
where type can be any of the basic data type such as
integer, float etc., or any of the user-defined data
type. Pointer name becomes the pointer of that data type.
e.g.
int *iptr;
char *cptr;
float *fptr;
e.g.
e.g.
iptr1=ival; // invalid, ival is not address.
1.11.Pointer Arithmetic
6
Data Structures with ‘c’
allowed on pointers than the ones discussed here. Consider a
program to demonstrate the pointer arithmetic.
e.g.
# include<stdio.h>
main()
{
int a[]={10,20,30,40,50}; ptr--> F000 10
int *ptr; F002 20
int i; F004 30
ptr=a; F006 40
for(i=0; i<5; i++) F008 50
{
printf(“%d”,*ptr++);
}
}
Output:
10 20 30 40 50
char 1byte
int 2bytes
float 4bytes
long int 4bytes
double 8bytes
short int 2bytes
1.12.Array of pointers
7
Data Structures with ‘c’
The example declares ‘A’ as an array of character
pointers. Each location in the array points to string of
characters of varying length. Here A[0] points to the first
character of the first string and A[1] points to the first
character of the second string, both of which contain only one
character. however, A[2] points to the first character of the
third string, which contains 9 characters.
t = x;
x = y;
y = t;
}
t = *x;
*x = *y;
*y = t;
}
void main()
{
8
Data Structures with ‘c’
int n1 = 25, n2 = 50;
Output:
arr[0] or *arr;
arr[1] or *(arr+1);
9
Data Structures with ‘c’
Now we have understood the relation between an array and
pointer. The traversal of an array can be made either through
subscripting or by direct pointer manipulation.
e.g.
void main()
{
int arr[] = {0,1,2,3,4,5,6,7,8,9}
print(arr,arr+9);
}
- automatic
- static
- external
- register
10
Data Structures with ‘c’
within the function are, by default, ‘automatic’. However, we
can explicitly declare them by using the keyword ‘automatic’.
e.g.
void print()
{
auto int i =0;
main()
{
print();
print();
print();
}
Output:
e.g.
void print()
{
static int i =0;
11
Data Structures with ‘c’
printf(“\n Value of i before incrementing is %d”,
i);
i = i + 10;
printf(“\n Value of i after incrementing is %d”, i);
main()
{
print();
print();
print();
}
Output:
e.g.
int g = 0;
void main()
{
12
Data Structures with ‘c’
:
:
}
void fn1()
{
:
:
}
extern int g = 0;
void fn2()
{
:
:
}
void fn3()
{
:
}
e.g.
void loopfn()
13
Data Structures with ‘c’
{
register int i;
Syntax:
14
Data Structures with ‘c’
where
- ptr is a pointer variable of type data_type.
- data_type can be any of the basic data type, user
defined or derived data type.
- size is the number of bytes required.
e.g.
ptr =(int*)malloc(sizeof(int)*n);
# include<stdio.h>
# include<string.h>
# include<alloc.h>
# include<process.h>
main()
{
char *str;
}
strcpy(str,”Hello”); /* copy hello into str
*/
printf(“\n str= %s “,str); /* display str */
free(str); /* free memory */
}
15
Data Structures with ‘c’
Syntax:
where
-
ptr is a pointer variable of type data_type.
-
data_type can be any of the basic data type, user
defined or derived data type.
- size is the number of bytes required.
- n is the number of blocks to be allocated of size
bytes.
and a pointer to the first byte of the allocated region is
returned.
e.g.
# include<stdio.h>
# include<string.h>
# include<alloc.h>
# include<process.h>
main()
{
char *str = NULL;
}
strcpy(str,”Hello”); /* copy hello into str
*/
printf(“\n str= %s “,str); /* display str */
free(str); /* free memory */
}
1.16.3. Function free(block)
# include<stdio.h>
# include<alloc.h>
main()
{
int *a;
a= (int*)malloc(sizeof(int));
*a = 10; ----> 10
a= (int*)malloc(sizeof(int)); ----> 20
*a = 20;
main()
{
int *a;
a= (int*)malloc(sizeof(int));
*a = 10; ----> 10
free(a);
----> ?
}
17
Data Structures with ‘c’
valid address we call it as ‘Dangling Pointer’. If we want to
reuse this pointer we can allocate memory for it again.
1.18. Structures
struct tag
{
type member1;
type member2;
type member3;
:
:
}variables;
e.g.
struct Account
{
int accnum;
char acctype;
char name[25];
float balance;
};
e.g.
18
Data Structures with ‘c’
newcust.balance = 100.0
printf(“%s”, oldcust.name);
e.g.
struct Date
{
int dd, mm, yy;
};
struct Account
{
int accnum;
char acctype;
char name[25];
float balance;
struct Date d1;
};
Account c1;
c1.d1.dd=21;
Account a[10];
19
Data Structures with ‘c’
We can also declare pointers to structures and to access
member variables we have to use the pointer operator ->
instead of a dot operator.
Account *aptr;
printf(“%s”,aptr->name);
e.g.
struct info
{
int i, j, k;
info *next;
};
1.19.Enumerated Constants
where
- enum is the keyword.
- identifier is the user defined enumerated data
type, which can be used to declare the variables in
the program.
- {c1,c2,...} are the names of constants and are
called enumeration constants.
- var_list is an optional list of variables.
e.g.
20
Data Structures with ‘c’
enum Colour{RED, BLUE, GREEN, WHITE, BLACK};
e.g.
e.g.
e.g.
21
Data Structures with ‘c’
However whenever necessary, an enumeration is
automatically promoted to arithmetic type.
e.g.
1.20.Unions
union tag
{
type memvar1;
type memvar2;
type memvar3;
:
:
};
e.g.
union utag
{
int num;
char ch;
};
22
Data Structures with ‘c’
The above union will have two bytes of storage allocated
to it. The variable num can be accessed as ff.sum and ch is
accessed as ff.ch. At any time, only one of these two
variables can be referred to. Any change made to one variable
affects another.
23
Data Structures with ‘c’