0% found this document useful (0 votes)
8 views

classwork_pointernStruct

The document provides a comprehensive overview of basic string operations, pointers, pointer arithmetic, and structures in C programming. It includes examples of string manipulation functions, pointer types, and how to use pointers with arrays and functions. Additionally, it covers user-defined data types such as structures and unions, illustrating their usage with code snippets.

Uploaded by

tanishatayal26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

classwork_pointernStruct

The document provides a comprehensive overview of basic string operations, pointers, pointer arithmetic, and structures in C programming. It includes examples of string manipulation functions, pointer types, and how to use pointers with arrays and functions. Additionally, it covers user-defined data types such as structures and unions, illustrating their usage with code snippets.

Uploaded by

tanishatayal26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Dr.

Vani Nijhawan

Q WA menu driven program to perform 4 (length, copy, compare and concat)


basic string operations on strings user defined.
Printf(“1………. String length”);
Printf(“2………. String copy”);
Printf(“3………. String compare”);
Printf(“4………. String concat”);
scanf(“%s %s”, s1,s2);
scanf(“%d”,&choice);

switch (choice)
{
Case 1:
N= len_string(s1);
Break;
Case 2:
strcpy(s3,s2);
Break;
Case 3:
N= strcmp(s1,s2);
Break;
Case 4:
strcat(s4,s1);
Break;
Void transpose{
Dr. Vani Nijhawan

Pointers
A special variable that contains addresses.

int a;
int *a1; //pointer => it will hold some address
Data types:
int , char, float…….
Pointer data types: 19 34521

int, char, float, ….., void a (34521) p1 (11231)


The type of variable will decide the type of pointer, if we want it to point to
that particular variable. // a , &a, p1, &p1, *(p1)
int a=19;
//sizeof(int);
int *p1;
p1 = &a; // & => address operator

#include<stdio.h>
Void main() // * => value at address operator
{
int a; // %d-intergers, %u=> unsigned nos, %p=> pointers
int *p1;
scanf(“%d”, &a); //25 25 32114

p1=&a; a 32114 p1 (11232)


printf(“%d”,a); //25
Dr. Vani Nijhawan

printf(“\n%p”,&a); //32114
printf(“\n%u”,p1); // 32114
printf(“\n%d”,*p1); // 25 turns the value at address contained by p1
printf(“\n%u”,&p1); //11232
}

Pointer Arithmetic
#include<stdio.h>
void main()
{

17543
int a, b,*p1,*p2, x,y,z; 4+3=7 11643
2
a=12; b (17543) p1 p2
b=4; a (11643)
p1=&a;
p2=&b;
x= *p1 * *p2 - 6; // 12 * 4 -6 = 48 – 6= 42
y= 4 * - *p2/ *p1 +10; // 4 * - 4/12 +10 = -16/12 +10 = -1 +10=9
printf(“%u”, &a); // 11643
printf(“%u”, &b); // 17543
printf(“a=%d and b=%d”, a,b); //12 4
printf(“x=%d and b=%d”,x ,y); // 42 9
*p2= *p2 +3; // 4 +3=7
*p1= *p2 – 5; // 7 -5=2
z= *p1 * *p2 – 6; // 2 * 7 -6 = 8
printf(“\na=%d and b=%d”, a,b); //2 7
Dr. Vani Nijhawan

printf(“z=%d ”, z); // 8
}

Pointer types Example:


Void main()
{
int a, *aa;
void *p; A’s add

float b, *bb; aa bb cc
char c, *cc; p
a=10, b=34.5, c=’s’;
aa= &a; bb=&b;
cc = &c;
p= &a;
p= &b;
p= &c;
}
Example Double Pointer:
A variable that holds the address of a (single) pointer.
Void main()
{ a p k
585 6541,42
int a=585;//int= 2bytes
3241
int *p; //pointer 6541to42 6545,6548 3241
int **k; // double pointer 6543 1123
Dr. Vani Nijhawan

int ***d; //triple pointer


1123
p= &a;
k= &p;
//d = &k;
//k=&a; // Not valid
printf(“a=%d”,a); // 585
printf(“%u”,&a); // 6541
printf(“%d”,*p); // 585
printf(“%u”,p); //6541
printf(“%u”,&p); // 3241
printf(“%d”,**k); // *(*(k) ) 585
printf(“a=%d”,&k); // 1123
printf(“a=%d”,k); // 3241
//Printf(“a=%d”,***d); // *(*(*(d) ) ) //585

&a, &p, &k // address


*p // *(p)

Function returning int pointer :terminology


int * add();

int *large(int *, int*);


void main()
{
Dr. Vani Nijhawan

int a=10;
int b=20;
int *p;
p=large(&a,&b);
printf(“%d”, *p); // 20
}
Int *large (int *x, int *y )
{ If(*x>*y) // *x=10 > *y=20
Return x; //int’s add
Else
Return y; // int’s add
}
Example: Pointer and Array
Void main()
{
Int i=3, *x; // i++ =4
float j=1.5, *y;
char k=’c’, *z;
print(“%d”,i);
print(“%f”,j);
print(“%c”,k);
x=&i; //
y=&j; // int * x
z=&k;
print(“%u”,x); // 1202,03
print(“%u”,y); // 1310
Dr. Vani Nijhawan

print(“%u”,z); // 1230
x++; // 1204
y++; // 1314
z++; // 1231
print(“%u”,x);
print(“%u”,y);
print(“%u”,z);
}
Example: Pointer and Array2
void main()
{ num
int num[]= {24,34,12,44,58,41}; 24 34 12 44 58 41

int i=0, *j, **k; 1128

j= &num[0]; //j=num //1128 sizeof(int)=2


while(i<=5)
{
printf(“address =%u”, &num[i]); //&num[0]
printf(“element=%d”, *j); //24
i++; //i=1
j++; // j= 1130
}
}
Dr. Vani Nijhawan

Example: Passing an entire Array to a function (pointer, array, functions)


void display(int *j, int n)
void main()
{ int num[]= {24,34,12,44,58,41};
Display(&num[0],6); }
void display(int *j, int n)
{ int i=0;
while(i<n)
{ printf(“element=%d”, *j); //24 34 12……
i++;
j++; //j+2 ➔ 1128 1132 j=num
}
}

24 34 12 44 58 61

Some Notations: 1128


num[i] //num[2] => 3rd element of array //12
*(num+i) // * (num+2) 1128 1130 1132 //12
*(i+num) // 12
i[num] // 12

Example: Array of pointers


Dr. Vani Nijhawan

void main()
{ int *arr[4]; //int *a
i(&a) j(&b) k(&c) l(&d)
int a=10, b=20,c=67,d=87,m;
arr[0]= &a;
arr[1]= &b;
arr[2]= &c;
arr[3]= &d;
for(m=0;m<=3;m++)
{ printf(“\n%d”, *(arr[m])); //*(arr[0]) //10
}
}

Pointer to an Array, pointer to a float a p 1050


int a[]={58,53,11}; j++ 58 53 11

p = &a[0]; //p=a; 1050


or 5 63 12

p=a;
p++;
Assignment:
Stdio.h: printf scanf fprint fscanf fopen fclose gets puts getchar putchar
string.h: strlen strcpy strcmp strcat strrev strncmp, etc
stdlib.h: calloc malloc free exit realloc abs atoi atof atoll rand
math.h : ceil floor pow sqrt sin cos exp log tan
ctype: isalnum, isalpha isascii isdigit isupper toascii toupper tolower isxdigit
a[1]
Dr. Vani Nijhawan
1 23 45 65
1034

conio.h: clrscr getch getche s 23 33 54 56 67 54 43 87

*(s[2]+1)
s[4][2] 1034,35=>23 1036,37 =>33
Some notations: s[0]
s[2][1] //54 *(s[2]+1) s[1] =>1038 s[2] +1=> add of 54
*(s[2]+1) //54
*(*(s+2)+1) //54

Example: Pointer to an array


Void main()
{ 1034,35 =>4, 1036,37 => 3 ,
1038,39=>7 1040,41 =>6
Int a[][4]= { 4,3,7,6, 1034
3,7,9,2, 4 3 7 6 3 7 9 2 2 5 7 6

2,5,7,6 };
Int *p; 1042 1036
Int (*q) [4]; // 1. Columns 2. Dimension q p
p= a;
q=a;
printf(“\n %u %u”,p,q); // 1034 1034
p++; //1036
q++; //1042
printf(“%u %u”,p,q);
}
Dr. Vani Nijhawan

void pointer / Generic pointer


void *p;
int a=44; float b=39;
p=&a;
p=&b;
NULL Pointer
A pointer which does not point to any valid memory address. It points towards
NULL.
Declaraing a NULL pointer:
int *ptr= NULL;
Where, NULL is a macro defined in many header files.

Function Returning float Pointer/ function returning char


float * add(int, int);

float *check();
void main()
{ float *c;
c= check();
printf("%f",*c); //16
}
float * check()
{ float a=10, b=16;
float *r;
if(a>b)
r=&a;
Dr. Vani Nijhawan
&b

else r
r=&b;
return r; } //

Pointer to a function / pointer to an array(array) p


p = a; //base address
p = add;
var vs functions
x=a()
x=a

Declaration Syntax:
Return_type (* function_pointer) (arguments);
int (*func) (int a, float b);
Calling a function using function pointer:
(*func) (1,2.0);
Or func(1,2.0);
Example:
void print(int);
void (*p) (int);
void main()
{ int a=22;
p=print;
(*p) (10); // call to the function
p (20); // function call
Dr. Vani Nijhawan

}
void print(int x)
{
printf(“\n %d”,x); //10 20
}

Structures
It is a user defined data type
Data Types :
1. Primitive: int char float //int a, float x;
2. Derived DT: arrays strings
3. User defined DT: structure, union, enumeration (enum), typedef
struct st_name
{
Data_type member1_name;
Data_type member2_name;
Data_type member3_name;

} ;
struct book_bank
{
char title[20]; b1 (=46 bytes) b (2 bytes)
char author[20]; Let us C Yashwant Kanitkar 560 355.0
int pages;
Dr. Vani Nijhawan

float price; 20 20 2 4
} b3,b4 ;
struct book_bank b1,b2; // b1,b2 var of structure type // v.m
(variable.member) // b1.title //float f = b1.price

b1 => let us c Yashwant Kanitkar 560 355.0


int n =b3.pages;

Structure Variable initialisation

struct class
{
int number;
char name[10];
float marks;
};
void main()
{ int x;
struct class s1= {60,”abc”, 60.75}; // at compile time
struct class s2= {64,”xyz”, 70.5};
struct class s3;
s3=s2; // copying all member values at one go
x= ((s3.number == s2.number) && (s3.marks == s2.marks)) ? 1: 0 ;
if(x==1)
{
printf(“s3 and s2 are same”);
Dr. Vani Nijhawan

printf(“%d %s %f\n”, s3.number, s3.name,s3.marks);


}
else
printf(“s3 and s2 are not same”);
}

Example:
void main()
{ struct employee “abc” 30 500.5

{
char name[10];
int age;
float salary;
};
struct employee e1= {“abc”, 30, 500.5};
struct employee e2, e3;
strcpy(e2.name, e1.name); //e2.name => “abc”
e2.age=e1.age;
e2.salary =e1.salary;
printf(“enter data for an employee”);
scanf(“%s %d %f”,e3.name,&e3.age, &e3.salary);// run time initialisati
Dr. Vani Nijhawan

printf(“\n%s %d %f”, e1.name,e1.age,e1.salary);


printf(“\n%s %d %f”, e2.name,e2.age,e2.salary);
printf(“\n%s %d %f”, e3.name,e3.age,e3.salary);
}

Union
It is also a user defined data type.
Very similar to structure that is it is also a heterogeneous collection of data
members.
Its memory occupancy is not the sum of individual memory occupancies of its
all members.
e.g.
syntax:
union union_name
{
Data_type member1_name;
Data_type member2_name;
Data_type member3_name;
} ;
e.g
union employee u1
19
{
char name[10]; //10
int age; //2 10 bytes
float salary; //4
} u1,u2;
Dr. Vani Nijhawan

void main()
{
union employee e_u1, e_u2;
strcpy(e_u1.name, “Isha”);
printf(“%s”, e_u1.name); //Isha
scanf(“%d”, &e_u1.age); //19
printf(“%d”, e_u1.age); //19
printf(“%s”, e_u1.name);
}

Array of Structures , array within structure


Q1. Calculate total marks subject wise and student wise.
struct marks // struct marks m[3] ;
{ student[0] student[1] student[2]
int sub[3];
23 56 77 …x.... …… …. …. …
int total; 4 4 4
};
void main()
{ int i;
struct marks student[3] = {{23,56,77,…x…},
{43,67,89,..y..},{66,33,77,…z…}};
struct marks total; //
23+43+66 56+67+33 77+89+77 x+y+z
for(i=0;i<=2;i++) //v.m
{
student[i].total= student[i].sub[1]+ student[i].sub2+ student[i].sub3;
total.sub1= total.sub1 + student[i].sub1;
Dr. Vani Nijhawan

total.sub2= total.sub2 + student[i].sub2;


total.sub3= total.sub3 + student[i].sub3;
total.total= total.total + student[i].total;
}
Printf(“student total\n”);
for(i=0;i<=2;i++)
Printf(“student %d\n,i+1,student[i].total);
printf(“subject total\n”);
printf(%s %d\n%s %d\n, %s %d\n
“Subject 1 “, total.sub1, “Subject 2 “,
total.sub2, “Subject 3 “, total.sub3);
printf(“\n Grand total = %d \n”, total.total);
}

Structures within Structures / Nested Structures


struct salary
{ char name[5];
char dept[5];
Int basic;
struct allowance
{ int da; //a1.da
int hra;
int ca;
} a1;
} employee1; // employee1.a1.hra //employee1.basic
//employee1.name
Dr. Vani Nijhawan

Method 2: Nested Structures


struct allowance
{ Int da; //a1.da
Int hra;
Int ca;
};
struct salary
{ char name[5];
char dept[5];
struct allowance pay;
struct allowance arrears;
} employee1;
void main()
{ scanf("%s",employee1.name);
scanf("%s",employee1.dept);
scanf("%d",&employee1.pay.da);
scanf("%d",&employee1.arrears.hra);
printf("%s",employee1.name);
printf("%s",employee1.dept);
printf("%d",employee1.pay.da);
printf("%d",employee1.arrears.hra);
}
Structures and functions
struct stores
{ char name[20]; // 20
float price; //4
Dr. Vani Nijhawan

Int quantity; //2


} y[2];
struct stores update(struct stores product, float p, int q);
float mul(struct stores stock); //p *q
Void main()
{
float p_incre, value;
int q_incre;
struct stores item= {“parkerpen”, 200.0, 5};
printf(“input price and quantity increment values”);
scanf(“%f %d”,p_incre,q_incre); //25 100
item = update(item,p_incre,q_incre); //overwrite
printf(“updated values of items\n”);
printf(“name %s\n”,item.name); //parkerpen
printf(“Price %f\n”,item.price); //225
printf(“Quantity %d\n”,item.quantity); //105
value=mul(item);
printf(“\n value of item= %f”,value); //225*105
}
struct stores update (struct stores product, float p, int q)
{
product.price +=p; //25+200=225
product.quantity +=q; //5+100 =105
return product;
}
float mul(struct stores stock)
Dr. Vani Nijhawan

{
return (stock.price * stock.quantity);
}
Size of Structures
Sizeof(struct stores) //26
Sizeof(y) // y is struct array //2*26=52
Sizeof(y)/sizeof(x) // gives ?

Q1. There is an array ‘a’ of type struct x (occupying 20 bytes), which occupies
100 bytes in memory. Calculate the size if array ‘a’.
Ans 100/20 = 5 is the size of the array.

You might also like