Module4-Notes
Module4-Notes
University -Sem 1
Arrays, Strings and Structures
W4.3 STRUCTURES
concept of Structures
A:structure is a collection of
1.
data of different
2. The main difference between a structure and andatatypesis
datatype. array, that in an array, all the data has to be of the same
3. Many a times, we need to
combine
attributes may not be of the same dataseveral attributes of an obiect. together into a data
type. structure. These
A Eg: Let us look at the
attributes of a student
student
1. Name
character array
2. Age ... integer
3. Percentage ... float
As seen above, this is a collection of data of different data types. Here we use a
structure.
1. We can also combine a set of structures into an array.
For example, the above structure is for a student. We can make an array containing 100 such structures.
This is now representing data for a whole class of students.
a Structure
Declaration and Initializations of instances of this
have 3 members: ame, age, percentage. Make 3
structure. It must table.
Let us create a "student" values and display them in a
structure by the names s1, s2, and s3. Read inputs for all
ArrayS, Strings an
CProgramming| Mumbai University- Sem 1
structure in the manner below
1) First, we create a
struct
This is the keyword that creates a structure
struct student
student
This is the name of the structure
It is one ofthe mermbers of this structure
name
char name[100]; Data type: character array
int age, tis one of the menmbersof this structure
age
float percentage, Data type: int
It isone of the members of this structurs
percentage
Data type: float
biock:
Aternate method of declaringthis same structure and its 3 instances s1, s2, s3 in the same
struct student Notice, how s1, s2, s3 are defined just as the structure
"student" is being created.
char namef100]; Everything else is the same.
meth.
int age; Although not very popular, some programmers use this
fioat percentage;
}s1,82, 83;
Page:
4-28
tinclude<stdio.h>
struct student
C{ Bharat Acharya
int rollno,; Dec 22- Structures
char name [100]:
float marks; Roll No
Name Bharat
int main (void) Marks 94
Ro11 No
int i, count=i0; Name Tim
struct student list[101: Marks 97
printf("C | Bharat Acharya\n"}:
printf {" Roll No |3
-\n"): Nane
printf (" Dec 22 - Structures\n\n") Vinita
for (i = 0; i < count; it+) Marks 56
C| Bharat Acharya
Page: 4-31
Title COA
Author
Price BharatAcharya
499
Title CG
Author Bnarat
Price 528
Title DSA
Author
Price Acharya
555
Title
Author
COA Price
CG
DSA BharatAcharya
Bharat 499.00
520.09
8986 Acharya
Bharat 555.00
8086 560.00
Acharya 620.00
Bharat Acharya:
1/ book is a structure
char title[100]:
chat author[100]; | | title - char array
ioatprice; | a0thor - char aray
:ntmain(void)
count$:
int i, jr
struct book list[5]:
struct book tep; 1 array of 5 books
printf(C Bharat Acharya\n"); I| tor svapping
printf(nm -n"):
printf("Jun 22 - Structures\n\n"):
for i
(i0 ; i < count; i++}
1/ input the data
printf{Titie |");
scanf ("$s", list [i]. title) ;
printf ("Author | "}:
scanf ("s", list [ii. author):
printf {"Price "
scanf ("$f", &list{il.price);
printf {"\n"):
# ten
temp list[jJ
list ij list[j+1l
list(+1] temp
printf{"\n")
printf {"-m Price\n")
Author --\n")
printf{"Títle
printf (" #display the books
Lor (1 0; i < count; i++}
[i).title) i
Printf {"$-15s", 11st
list [i).author) i
Printf("%-15g",
list [il-price) i
PIintf ("%8.2£n", \n"):
printf (
printf ("\n\n")
return 0; I/cI Bharat Acharya
CProgramming | Mumbai University-Sem 1 Arrays,, Strings
G| Bharat Acharya
For the sake of showing
the output here, I have Jun 23.- Structures
run the program with a
count of 4. No
Name Bharat
In the exam the count Experience 7.
Salary. 8000
would be 10 as per the
question
No | 2
Name Acharya
Experience 8
Salary 8500
No 3
Name Tim
Experience 9
Salary 12000
No 4
Name John
Experience
Salary 6000
No Name Experiehce
Bharat 8800.00
2 Acharya 8500.00
Page:4-34
int main(void)
nt i, Count=4;
struct employee listI1001:
printfE("C
printf("
Bharat Acharya\n");
print f (("Jun 23-
for (i Structures\n\n")
0; i < count i )
printf "NO
Scanf("%d &list [il no)
printf ("Nane
Scanf S ist l name)
print(EXperience
scanf "a&iist lil experience)
printfSalary
scanf f &list I salary);
print £ ("\n")
printf\n)
printf l
Experience Salary\n")i
printf # NO Name
-\n"):
printf (
for (i 0 i count it +)
[il.salary<10000))
((1ist [il experiences=5) &&(1ist
if
print f( - -
printf ("\n\n")
return 0 Page: 4-35
Bharat Acharya
Tech-Neo Publications |
Module 5
5.1 POINTERS
int a = 25;
int *ptra &a; I/ Declare and Initialise the pointer in the same
statement
float b =2.5;
float "ptrb =&b;
char c='z';
char *ptrc =&c;
clude<Stdio,b
main
(void)
int a 25g
int *pptra
float b 25 CI Bharat Ácharya
s108t *ptrb &b;
char c z Pointers
char *ptrc &C
printf("C Bharat Acharya\n")i 25
Drintf("
printf("Pointers\n\n"):
-\n"): 25.000000
print£( ngd\n", *ptra)
orint£("E\nn,*ptrb)
printf ("%c\n" *ptrc): Bharat Acharya:
printf ("\n");
return 0;
Teturrn 0;
Page: 5-3
Acharya
University- Sem1
C Programming IMumbai
Arrays
using a Pointers and
a 5.1.3 Pointer
Arithmetic
array
method of accessing an
PointersS provide an efficient
powerful data structures like linkedlists, trees etc(We
vou cAn create Structures), pointers can
also be a
useful tool
Although using pointers Data
semester in thesubject called
Intensely next
llst"
arrays.
array of integers by the name
Suppose we create an
to this array by the name "ptrlist".
We create a pointer element of list.
have the address of the first
Now ptrlist will this list.
should point to the next element of
As we do ptrlist++, it
understand!
Now this is very important to
by 1 always.
ptrlist++ will not increment ptrlist
SIZE OF THE datatype
IT WILL INCREEMENT by the
That means 4 bytes.
An int is 32 bits (modern systems).
LOCATION HOLDS 1BYTE
Inthe memory 1 MEMORY
memory locations
That means an int is spread across 4
Assume our array begins at address2000
2000, 2001, 2003, 2003.
The first integer is stored in locations
The next integer is in 2004.
ptrlist originally is 2000
ptrlist++ will INCREMENT it by 4.
Nowptrlist will be 2004.
" That is how ptrlist willpoint to the next int inthe array.
Page:b
Tech-Neo Publications | Bharat Acharya
cProgramming| Mumbai
University -Sem 1
Pointers
Integerarray {incrementtby 4}
intlist/5):
"ptrlist: list; // note that it
int is an array so "&° is not
needed
ptrlist++
2000Int[0]
2001
int|0]
ptrlist++
2002int|int(0]0]
2003
2004
int[]
2005 int(1)
2006 int[1]
2007 int[1]
#include <stdio.h>
int main()
>Example :
void *ptr = malloc(1000);
address into ptr. All those memory locations will
This will allocate amemory of 1000 bytes and give its starting
Contain garbage values as of now.
Page: 5-7
Acharya
Tech-Ne0 Publications | Bharat