3. Disclaimer: This presentation is prepared by trainees of
baabtra.com as a part of mentoring program. This is not
official document of baabtra.com – Mentoring Partner
5. STRUCTURES
Collection of data items of different types using a single name,
user defined datatype.
Grouping several pieces of related information together, array can
store only single type data.
E.g1. It can handle student_name,roll_number and marks under a
single name student.
E.g2. book: author ,price ,year.
customer: name, telephone, city, category.
7. DEFINING STRUCTURE TYPE
VARIABLE• Declare a variable of type structure to access the
members of structure.
• struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
};
struct book_bank book1,book2,book3;
• arrays of strucctures: struct book_bank book[3];
8. Another type of declaration:
• struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
} book1,book2,book3;
9. ACCESSING STRUCTURE MEMBERS
• We access and assign values to structure members.
• Access can be done by member operator ‘ . ’ also
known as dot operator period operator or by ->
(pointer to structure)
•E.g. book1.price=250;
book1.pages=300;
scanf(“%s”,book.title);
11. POINTERS
• Derived data type in C.
• Contain memory address as their values. i.e. It
stores memory address of another variable.
• Pointers reduce length of program.
• Declare as int *ptr
• Initialize pointer as null int *ptr=0 or int *ptr=NULL
12. & (address operator)
Returns address of operand
int y = 5;
int *yPtr;
yPtr = &y; //yPtr gets address of y
yPtr “points to” y
yPtr
y
5
yptr
500000 600000
y
600000 5
Address of y
is value of
yptr
13. • Declare an array int b[5] and a pointer int * bPtr
bPtr = b;
Array name actually a address of first element
OR
bPtr = &b[0].
C uses two pointer operators,
Indirection operator (*) – to get the value belongs to the address.
Address-of-operator (&) – ampersand symbol, means return the
address of a variable.
When & operator is placed before the name of a variable, it will
returns the memory address of the variable instead of stored
value.
15. STRING
• Sequence of characters treated as single data item
• There is no string type, we implement strings as arrays of
characters in C.
• E.g. “Welcome”;
DECLARE A STRING
• char str[10]; // str is an array of 10 chars or a string
• char str[10]=“Welcome” // Declare and initialize a string.
• char str[10]= { ‘w’, ’e’ , ’l’ , ‘c’ , ‘o’ , ‘m’ , ‘e’ }
16. READING A STRING
• scanf(“%s”,str); // read a string, no need of &
sign.
• gets(str); // read a string with white
spaces.
• scanf(“%5s”, str); // read only five characters even
str can hold 10.
• for(i=0;i<10;i++)
{
scanf(“%c”,&str[i]) // read by character-by character;
}
17. Writing string to screen
• printf(“%s”, str);
• puts(str);
STRING FUNCTION
• strcpy(s1, s2) – copies s2 into s1 (including ‘0’ as last char)
• strncpy(s1, s2, n) – same but only copies up to n chars of s2
• strcmp(s1, s2) – returns a negative int if s1 < s2, 0 if s1 = = s2 and a
positive int if s1 > s2
• strcat(s1, s2) – concatenates s2 onto s1 (this changes s1, but not s2)
• strlen(s1) – returns the integer length of s1
19. Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
20. Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
21. Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
Contact Us