View
93
Download
0
Tags:
Embed Size (px)
Citation preview
Reshma Raju
twitter.com/username
in.linkedin.com/in/profilename
8547829221
STRUCTURE, POINTER AND STRING
Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
STRUCTURE
• Structure is a collection of different data type which are
grouped and each element in c structure is called members.
• Structure help to construct a complex data type in more
meaningful.
• It is similar to an array. The only difference is that array is
used to store collection of similar data type, but structure can
store collection of any type of data.
Syntax of structure
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
Example for structure#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
struct student s;
printf("Enter information of students:\n\n");
printf("Enter name: ");
scanf("%s",s.name);
printf("Enter roll number: ");
scanf("%d",&s.roll);
printf("Enter marks: ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
return 0;
}
OUTPUT
Enter information of students:
Enter name: Anu
Enter roll number: 1
Enter marks: 852
Displaying Information
Name: Anu
Roll: 1
Marks: 852
POINTER• Pointer is a user defined data type which creates special types
of variables which can hold the address of primitive data type
like char, int, float, double or user defined data type like
function, pointer etc. or derived data type like array, structure
etc.
• The general form of a pointer variable declaration is:
type *var-name;
NULL POINTER
• It is always a good practice to assign a NULL value to a
pointer variable in case you do not have exact address to be
assigned. This is done at the time of variable declaration.
• A pointer that is assigned NULL is called a null pointer.
• The NULL pointer is a constant with a value of zero defined in
several standard libraries
Example for pointer#include <stdio.h>
int main ()
{
int var = 20; // actual variable declaration
int *ip; // pointer variable declaration
ip = &var; // store address of var in pointer variable
printf("Address of var variable: %x\n", &var );
printf("Address stored in ip variable: %x\n", ip ); // address stored in pointer variable
printf("Value of *ip variable: %d\n", *ip ); // access the value using the pointer
return 0;
}
OUTPUT
Address of var variable: 28ff18
Address stored in ip variable: 28ff18
Value of *ip variable: 20
STRING• The string in C programming language is actually a one-
dimensional array of characters which is terminated by
a null character '\0'.
• Thus a null-terminated string contains the characters that
comprise the string followed by a null.
• The length of the string is the number of character + one
( null character )
Functions & purpose of string
S.N
.
Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
Example for string#include <stdio.h> OUTPUT
#include <string.h> strcpy( str3, str1) : Hello
int main () strcat( str1, str2): HelloWorld
{ strlen(str1) : 10
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
strcpy(str3, str1); // copy str1 into str3
printf("strcpy( str3, str1) : %s\n", str3 );
strcat( str1, str2); // concatenates str1 and str2
printf("strcat( str1, str2): %s\n", str1 );
len = strlen(str1); // total lenghth of str1 after concatenation
printf("strlen(str1) : %d\n", len );
return 0;
}
THANK YOU
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.
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
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