Strctures,strings,pointers

21

Transcript of Strctures,strings,pointers

Page 1: Strctures,strings,pointers
Page 2: Strctures,strings,pointers

STRUCTURE, POINTER, STRING

Nikhil Dev S.B

[email protected]

www.facebook.com/nikhildevsb

Twitter Profile

www.linkedin.com/nikhildevsb

Page 3: Strctures,strings,pointers

Disclaimer: This presentation is prepared by trainees ofbaabtra.com as a part of mentoring program. This is notofficial document of baabtra.com – Mentoring Partner

Page 4: Strctures,strings,pointers

OVER VIEW

1.STRUCTURES

2.POINTERS

3.STRING

Page 5: Strctures,strings,pointers

STRUCTURES

Collection of data items of different types using a single name,user defined datatype.

Grouping several pieces of related information together, array canstore only single type data.

E.g1. It can handle student_name,roll_number and marks under asingle name student.

E.g2. book: author ,price ,year. customer: name, telephone, city, category.

Page 6: Strctures,strings,pointers

DEFINING A STRUCTUREstruct tag-name{

datatype member1;datatype member2;. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

};

E.g. struct book_bank{

char title[20];char author[15];int pages;float price;

};

Page 7: Strctures,strings,pointers

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];

Page 8: Strctures,strings,pointers

Another type of declaration:

• struct book_bank{

char title[20];char author[15];int pages;float price;

} book1,book2,book3;

Page 9: Strctures,strings,pointers

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);

Page 10: Strctures,strings,pointers

STRUCTURE INITIALIZATION• struct

{int weight;float height;

} student ={60,180.75};

• struct st_record{

int weight;float height;

} ;struct st_record student1={60,180.75};

Page 11: Strctures,strings,pointers

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

Page 12: Strctures,strings,pointers

& (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 yis value of yptr

Page 13: Strctures,strings,pointers

• Declare an array int b[5] and a pointer int * bPtrbPtr = b; Array name actually a address of first element

ORbPtr = &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.

Page 14: Strctures,strings,pointers

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

int x,*p,y;

x=10;

p=&x;

y=*p; // accessing x through p OUTPUT

*p=20; // assigning 20 to x P: 2686734 (Address of x)

printf("\n\tp:%d",p); *p: 20

printf("\n\t*p:%d",*p); y: 10

printf("\n\ty:%d",y);

getch();

}

Page 15: Strctures,strings,pointers

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’ }

Page 16: Strctures,strings,pointers

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;}

Page 17: Strctures,strings,pointers

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

Page 18: Strctures,strings,pointers

Thank you...

Page 19: Strctures,strings,pointers

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 designedto make a quick, good software professional out of anybody who holds a passion for coding.

Page 20: Strctures,strings,pointers

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

Page 21: Strctures,strings,pointers

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.

Email: [email protected]

Contact Us