0% found this document useful (0 votes)
28 views97 pages

C Programming: Structures and Pointers

The document provides an overview of key programming concepts including structures, pointers, and dynamic memory allocation in C and C++. It explains memory allocation for structures, the use of pointers and double pointers, and the differences between malloc and calloc for dynamic memory allocation. Additionally, it covers how to handle arrays and strings using pointers, as well as the syntax for dynamic memory allocation using the new operator.

Uploaded by

arpanmahanty01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Topics covered

  • Memory Reallocation,
  • Scale Factor,
  • String Handling,
  • Memory Management Techniques,
  • Character Arrays,
  • Pointer Constants,
  • Double Pointer,
  • Data Type Size,
  • Pointer Efficiency,
  • Structures
0% found this document useful (0 votes)
28 views97 pages

C Programming: Structures and Pointers

The document provides an overview of key programming concepts including structures, pointers, and dynamic memory allocation in C and C++. It explains memory allocation for structures, the use of pointers and double pointers, and the differences between malloc and calloc for dynamic memory allocation. Additionally, it covers how to handle arrays and strings using pointers, as well as the syntax for dynamic memory allocation using the new operator.

Uploaded by

arpanmahanty01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Topics covered

  • Memory Reallocation,
  • Scale Factor,
  • String Handling,
  • Memory Management Techniques,
  • Character Arrays,
  • Pointer Constants,
  • Double Pointer,
  • Data Type Size,
  • Pointer Efficiency,
  • Structures

Lecture-1

Overview
Overview
Of Some
Selected Topics
 Structures
 Union
 Pointers
 Bit Specified variable
 DMA
(Dynamic Memory Allocation)
Structures
Memory Allocation to Structures

Unless we associate a variable


with defined structure
no memory space is allocated
Name of ith student

Enrollment no. of ith student

Marks of ith student

Class S.N. of ith student


Further
expanding
Student Record
One Department Elective

One Institute Elective

Three Core Courses


Compulsory for all students
The Data Type is either
int
unsigned int
Or signed int

Bit length is number of bits used


For signed int at least two bits
to be used , one for sign
Illustration for BFS

Struct Example
{
int a;
unsigned sex_code :1 ;
int b;
int d:3;
signed int c:2 ;
float marks;
int e:2 ;
float value;
};
0 15
a
0 15 a

0 15
Sex_code
0 15 a

0 15
Sex_code

0 15 b
0 15 a

0 15
Sex_code

0 15 b

0 1 2 15 d
0 15 a

0 15
Sex_code

0 15 b

0 1 2 3 4 15 d,c
0 15 a

0 15
Sex_code

0 15 b

0 1 2 3 4 15 d,c

0 3
1 marks
0 15 a

0 15
Sex_code

0 15 b

0 1 2 3 4 15 d,c

0 3
1 marks

0 1
e
0 15 a

0 15
Sex_code

0 15 b

0 1 2 3 4 15 d,c

0 3
1
marks
0 1 15 e

0 3
1value

18 bytes
Efficient way of
defining structure for this case
Struct Example
{
unsigned sex_code :
1;
int d: 3;
int c: 2 ;
int e: 2 ;
int a;
int b;
float marks;
float value;
};
Sex_code, d ,c , e ( 1—3—2—2)
0 1 2 3 4 5 6 7

marks

value

14 bytes
Pointers
Point to Ponder
int value1, *p1;
float value2, *p2 ;
p1 = &value1;
p2 = &value2;

Why data type is


to be associated with pointer ?
When it holds integer address!
#include <stdio.h>
int main()
{
int *ptrq, q;
q = 50;

/* address of q is assigned to ptr */

ptrq = &q;

/* display q's value using ptr variable */


printf("%d", *ptrq);
return 0;
Pointer to Pointer/ double pointer
int num=123;
int *pr2;
int **pr1;
pr1 = &pr2;
pr2 = &num
Use of double pointer
/* Possible ways to find value of variable
num*/

printf("\n Value of num is: %d", num);

printf("\n Value of num using pr2 is: %d", *pr2);

printf("\n Value of num using pr1 is: %d", **pr1);

int b = *pr2;
c = b+(*pr2)
int b = *pr2;
c = b+(*pr2)
0r c = b+ **pr1

Or C = 2*(**pr1)
All will lead c= 246 but num does not change

num = 2*(**pr1) or **pr1 = 2*(*pr2)


Both will lead value of num As 246
/*Possible ways to find address of num*/

printf("\n Address of num is: %p", &num);

printf("\n Address of num using pr2 is: %p", pr2);

printf("\n Address of num using pr1 is: %p", *pr1);


Double Pointer in C
int main()
{
int var = 789;

// pointer for var


int* ptr2;

// double pointer for


ptr2
int** ptr1;
// storing address of var in ptr2
ptr2 = &var;
// Storing address of ptr2 in ptr1
ptr1 = &ptr2;
// Displaying value of var using
// both single and double pointers
printf("Value of var = %d\n", var);
printf("Value of var using single
pointer
= %d\n", *ptr2);
Double Pointer in C++
int val = 169;

// storing address of val to pointer ptr.


int *ptr = &val;

int **double_ptr = &ptr;

// pointer to a pointer declared which is


pointing to an integer.
What will be the size of a
pointer to a pointer in C
double pointer behave similarly
to a normal pointer both in C &
C++

So, the size of the variable of the


double-pointer and the size of the
normal pointer variable is always
equal.
Pointer Increment
& Scale factor
Pointers & Arrays
One Dimensional Array
Static int x(5) = { 1, 2, 3, 4, 6}

Int *p

variable X[0] X[1] X[2] X[3] X[4]

value 1 2 3 4 6
address 1000 1002 1004 1006 1008
x is base address
x and &x[0] is same and is 1000
p = x is same as p = &x[0]
x is a pointer constant
p is a pointer variable

p &x[0] *p …….. x[0]


p+1 &x[1] *(p+1) … x[1]
p+2 &x[2] *(p+2) … x[2]
p+3 &x[3] .
p+4 &x[4] .
variable X[0] X[1] X[2] X[3] X[4]
value 1 2 3 4 6
address 1000 1002 1004 1006 1008
Address of x[i] =
Base address of array X +
i* scale factor for int.
=X+2*i
Scale factor is 2 for integer, 4 for float & 1 for char

ith element = *(X + 2 * i)


Two Dimensional Array
static int a[2][3];
int *p ;
p=a ; //*or p = &a[0][0]

p ….. Pointer to first Row

(a+i) or a[i] …. Pointer to ith Row

*(a+i) …. pointer to the first element in the ith Row


*(a+i) + j …… Pointer to jth element in ith Row
*(*(a+i) + j) ……. Value of (i,j)th element
( jth element in ith Row)
….. a[0][0] ROW-0
….. a[0][1]
….. a[0][2]
ROW- a[1] …..
1 [0]
a[1] …..
[1]
a[1] …..
[2]

(p+3)+j ………. Address of jth element in


second row
*((p+Max_COL*i)+j) … (i,j)th element
Char NAME [4][16]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

R O O R K E E \0
U N A \0
M E E R U T \0
D H A R A M S H A L A \0

SPACE ALLOCATED ….. 64 bytes


SPACE USED …… 31 bytes
Static char *name[4] = { “ROORKEE’
“UNA”
“MEERUT”
“DHARAMSHALA”
};
R O O R K E E \0
U N A \0
M E E R U T \0
D H A R A M S H A L A \0

SPACE ALLOCATED ….. 31 bytes


SPACE USED …… 31 bytes
SPACE IS ALLOCATED EXACTLY
WHAT IS REQUIRED

NAME[0] …. Pointer to first String

Name[i] …. Pointer to ith String


POINTERS & CHARACTER STRING
main()
{
char *name;
Int length ;
Char *cptr = name;
Name = “ROORKEE” ;
When(*cptr != ‘\0’)
{
Cptr++ ;
}
Length = cptr – name;
Printf( “\n length of string = %d”, length);
} /*main ends here

USING POINTERS WE CAN HANDLE TABLE OF STRINGS

Char name[20]
Name = “Roorekee”
Static char *name[4] = { “ROORKEE’
“UNA”
“MEERUT”
“DHARAMSHALA”
};
USING ARROW OPERATOR
Index/ Record of
0 Student[0]
1 .
2 .
3 .
. .
. .
48 .
49 Student[49]
DYNAMIC
MEMORY
ALLOCATION
(DMA)
Difference Between

malloc( ) & calloc( )


Important Points !
Important Points !
Ptr May Change or may not Change
Case-1 newsize is Smaller
Case-2 newsize is Larger
Case-1 New size is smaller
Case-2 newsize is Larger

(a)
Case-2 newsize is Larger
(b)
In C++, memory is divided into two parts –

Stack - All the variables that are


declared inside any function take memory
from the stack.

Heap - It is unused memory in the


program that is generally used for
dynamic memory allocation
Dynamic memory allocation
using the new operator

To allocate the space dynamically, the operator


new is used.
It means creating a request for memory
allocation on the free store. If memory is
available, memory is initialized, and the address
of that space is returned to a pointer variable.
Syntax
Pointer_variable = new data_type;

The pointer_varible is of pointer


data_type.

The data type can be int, float,


string, char, etc.
int *m = NULL // Initially we have a NULL
pointer
m = new int // memory is requested to the
Initialize memory

We can also initialize memory using new


operator.

int *m = new int(20);


Float *d = new float(21.01);
Allocate a block of memory

We can also use a new operator to allocate


a block(array) of a particular data type.

For example
int *arr = new int[10]

Here we have dynamically allocated memory for ten


integers which also returns a pointer to the first element
of the array. Hence, arr[0] is the first element and so on.

You might also like