SlideShare a Scribd company logo
1 of 15
Download to read offline
Chapter 3
Data Structures
Assoc. Prof. Dr. Oğuz FINDIK
2016-2017
KBUZEM
KARABUK UNIVERSITY
1
 Strings are one-dimesional arrays of char and
terminated by the end-of-string sentinel 0 or null
character .
 String constant are written between double quotes.
For example "abc" is a character array of size 4, with
the last element being the null character 0.
 char *p ="abc"
 Printf ("%s%sn",p,p+1);
2
Strings
 char *p ="abcde";
 char s[]={'a', 'b', 'c', 'd', 'e', '0'};
 #include <stdio.h>
 #include <stdlib.h>
 void f(int a[]);
 int main(void) {
 char s[] = "Pointer is heart of the C programming";
 char *p = " Pointer is heart of the C programming";
 int a[3];
 double d[5];
 printf("%s%dn%s%dn%s%dn%s%dn", "sizeof(s) = ", sizeof(s),
 "sizeof(p) = ", sizeof(p), "sizeof(a) = ", sizeof(a),
 "sizeof(d) = ", sizeof(d));
 f(a);
 return EXIT_SUCCESS;
 }
 void f(int a[]) {
 printf("In f(): sizeof(a) = %dn", sizeof(a));
 } 3
Strings
 #include <stdio.h>
 #include <stdlib.h>
 int find_size(char *);
 int main(void) {
 char *s = "Pointer is heart of the C programming";
 printf("Character of String : %d", find_size(s));
 return EXIT_SUCCESS;
 }
 int find_size(char *s) {
 int sum = 0;
 while (*s != '0') {
 while(isspace(*s)) s++;
 if (*s!='0'){
 sum++;
 s++;
 }
 }
 return sum;
 } 4
Characters of Strings
#include <stdio.h>
#include <stdlib.h>
int find_size(char *);
int main(void) {
char *s = "Pointer is heart of the C programming ";
printf("Words of String : %d", find_size(s));
return EXIT_SUCCESS;
}
int find_size(char *s) {
int sum = 0;
while (*s != '0') {
while(isspace(*s)) s++;
if (*s!='0'){
sum++;
while (!isspace(*s) && *s!='0') s++;
}
}
return sum;
}
5
Words of Strings
#include <stdio.h>
#include <stdlib.h>
void *str_cpy(char *, register const
char *);
int str_len(const char *);
int main(void) {
char c[] = "Pointers is important for C
programmer";
char *s1 = malloc((str_len(c) + 1) *
sizeof(char));
printf("%s", str_cpy(s1, c));
return EXIT_SUCCESS;
}
6
String copy Function
int str_len(const char *s1) {
int length = 0;
while (*s1++ != '0')
length++;
return length;
}
void *str_cpy(char *s1, register
const char *s2) {
register char *p = s1;
while (*p++ = *s2++)
;
return s1;
}
 int a[3][5];
 Starting at the base adress of the array, all the array
elements are stored contiguously in memory. The array
name a by itself is equivalent to &a[0]. It is pointer to an
array of 5 ints.
7
Multidimesional Array
 In C, array elements can be of any type, including
pointer type.
8
Array of pointers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 50
#define N 10
void wrt_words(char **);
void swap(char **, char **);
void sort_words(char **);
int main(void) {
char word[MAXWORD];
char *w[N];
int n, i;
for (i = 0; i < N; i++) {
scanf("%s", word);
fflush(stdout);
w[i] = calloc(strlen(word) + 1,
sizeof(char));
strcpy(w[i], word);
}
sort_words(w);
wrt_words(w);
return EXIT_SUCCESS;
}
9
Example
void sort_words(char **a) {
int i, j;
for (i = 0; i < N; i++) {
for (j = i + 1; j < N; j++) {
if
(strcmp(a[i], a[j]) > 0)
swap(&a[i], &a[j]);
}
}}
void swap(char **p, char **q) {
char *temp;
temp = *p;
*p = *q;
*q = temp;
}
void wrt_words(char **a) {
int i;
for (i = 0; i < N; i++) {
printf("%sn", a[i]);
}
}
 typedef is a keyword used in C language to assign alternative
names to existing types. Its mostly used with user defined
data types, when names of data types get slightly
complicated. Also typedef improves portability of your
product. Following is the general syntax for using typedef,
 typedef existing_name alias_name
 typedef unsigned long ulong;
 ulong i, j ;
10
typedef
 Enumerated Types are a special way of creating your own
Type in C
 enum boolean {true,false};
 enum day
{Pazar,Pazartesi,Sali,carsamba,persembe,Cuma,Cumartesi}
 enum day =Pazar;
11
Enumerated Types
 The structure mechanism provides a means to aggregate
variables of different types. Let us define a structure that
describes student that has name, surname and number.
struct student{
int sequence;
char *name;
char *surname;
char *number;
};
struct student s1,s2; 12
Structure and Unions
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int sequence;
char *name;
char *surname;
char *number;
} student;
int main(void) {
int sira =0;
student s1;
s1.name = calloc(50,sizeof(char));
strcpy(s1.name,"Oguz");
s1.surname ="FINDIK";
s1.number="0102124525";
s1.sequence =++sira;
printf("Sequence :%dn",s1.sequence);
printf("Name :%sn",s1.name);
printf("Surname :%sn",s1.surname);
printf("Number :%sn",s1.number);
return EXIT_SUCCESS;
}
13
Example
A union, like a structure, is a derived type. Unions follow the same
syntax as structures but have members that share storage. A union
type defines a set of alternative values that may be stored in a
shared portion of memory. The programmer is responsible for
interpreting the stored values correctly. Consider the declaration
typedef union int_or_float {
int i;
float f;
} number;
14
Unions
#include <stdio.h>
#include <stdlib.h>
typedef union int_or_float {
int i;
float f;
} number;
int main(void) {
number n;
n.i = 10;
printf("d: %dn", n.i);
n.f = 20.0;
printf("f: %fn", n.f);
printf("d: %dn", n.i);
return EXIT_SUCCESS;
}
15
Example

More Related Content

What's hot (20)

Pointers in C
Pointers in CPointers in C
Pointers in C
 
This pointer
This pointerThis pointer
This pointer
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Pointers
PointersPointers
Pointers
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
Ponters
PontersPonters
Ponters
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
C Structure and Union in C
C Structure and Union in CC Structure and Union in C
C Structure and Union in C
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Used of Pointer in C++ Programming
Used of Pointer in C++ ProgrammingUsed of Pointer in C++ Programming
Used of Pointer in C++ Programming
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
 
Arrays
ArraysArrays
Arrays
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 

Similar to Data structure week 3

Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx8759000398
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationRabin BK
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programmingIcaii Infotech
 
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docx
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docxShad_Cryptography_PracticalFile_IT_4th_Year (1).docx
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docxSonu62614
 
Input output functions
Input output functionsInput output functions
Input output functionshyderali123
 
Presention programming
Presention programmingPresention programming
Presention programmingsaleha iqbal
 

Similar to Data structure week 3 (20)

COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
string , pointer
string , pointerstring , pointer
string , pointer
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
14 strings
14 strings14 strings
14 strings
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory AllocationPointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docx
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docxShad_Cryptography_PracticalFile_IT_4th_Year (1).docx
Shad_Cryptography_PracticalFile_IT_4th_Year (1).docx
 
Input output functions
Input output functionsInput output functions
Input output functions
 
Tut1
Tut1Tut1
Tut1
 
Presention programming
Presention programmingPresention programming
Presention programming
 
String notes
String notesString notes
String notes
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
structure,pointerandstring
structure,pointerandstringstructure,pointerandstring
structure,pointerandstring
 
Arrays
ArraysArrays
Arrays
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 

More from karmuhtam

Devre analizi deney malzeme listesi
Devre analizi deney malzeme listesiDevre analizi deney malzeme listesi
Devre analizi deney malzeme listesikarmuhtam
 
Deney 3 ve 4
Deney 3 ve 4Deney 3 ve 4
Deney 3 ve 4karmuhtam
 
Deney 1 ve 2
Deney 1 ve 2Deney 1 ve 2
Deney 1 ve 2karmuhtam
 
Data structure week y 5 1
Data structure week y 5 1Data structure week y 5 1
Data structure week y 5 1karmuhtam
 
Data structure week y 5
Data structure week y 5Data structure week y 5
Data structure week y 5karmuhtam
 
Data structure week y 4
Data structure week y 4Data structure week y 4
Data structure week y 4karmuhtam
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1karmuhtam
 
13. sınıfları başlık dosyaları
13.  sınıfları başlık dosyaları13.  sınıfları başlık dosyaları
13. sınıfları başlık dosyalarıkarmuhtam
 
12. stl örnekler
12.  stl örnekler12.  stl örnekler
12. stl örneklerkarmuhtam
 
11. stl kütüphanesi
11. stl kütüphanesi11. stl kütüphanesi
11. stl kütüphanesikarmuhtam
 
10. istisna isleme
10. istisna isleme10. istisna isleme
10. istisna islemekarmuhtam
 
9. şablonlar
9. şablonlar9. şablonlar
9. şablonlarkarmuhtam
 
8. çok biçimlilik
8. çok biçimlilik8. çok biçimlilik
8. çok biçimlilikkarmuhtam
 
7. kalıtım
7. kalıtım7. kalıtım
7. kalıtımkarmuhtam
 
6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlık6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlıkkarmuhtam
 
5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonları5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonlarıkarmuhtam
 
4. nesneler ve sınıflar
4. nesneler ve sınıflar4. nesneler ve sınıflar
4. nesneler ve sınıflarkarmuhtam
 

More from karmuhtam (20)

Devre analizi deney malzeme listesi
Devre analizi deney malzeme listesiDevre analizi deney malzeme listesi
Devre analizi deney malzeme listesi
 
Deney 6
Deney 6Deney 6
Deney 6
 
Deney 5
Deney 5Deney 5
Deney 5
 
Deney 3 ve 4
Deney 3 ve 4Deney 3 ve 4
Deney 3 ve 4
 
Deney 1 ve 2
Deney 1 ve 2Deney 1 ve 2
Deney 1 ve 2
 
Data structure week y 5 1
Data structure week y 5 1Data structure week y 5 1
Data structure week y 5 1
 
Data structure week y 5
Data structure week y 5Data structure week y 5
Data structure week y 5
 
Data structure week y 4
Data structure week y 4Data structure week y 4
Data structure week y 4
 
Data structure week 1
Data structure week 1Data structure week 1
Data structure week 1
 
13. sınıfları başlık dosyaları
13.  sınıfları başlık dosyaları13.  sınıfları başlık dosyaları
13. sınıfları başlık dosyaları
 
12. stl örnekler
12.  stl örnekler12.  stl örnekler
12. stl örnekler
 
11. stl kütüphanesi
11. stl kütüphanesi11. stl kütüphanesi
11. stl kütüphanesi
 
10. istisna isleme
10. istisna isleme10. istisna isleme
10. istisna isleme
 
9. şablonlar
9. şablonlar9. şablonlar
9. şablonlar
 
8. çok biçimlilik
8. çok biçimlilik8. çok biçimlilik
8. çok biçimlilik
 
7. kalıtım
7. kalıtım7. kalıtım
7. kalıtım
 
6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlık6. this işaretçisi ve arkadaşlık
6. this işaretçisi ve arkadaşlık
 
5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonları5. kurucu, yok edici ve kopyalama fonksiyonları
5. kurucu, yok edici ve kopyalama fonksiyonları
 
4. yapılar
4. yapılar4. yapılar
4. yapılar
 
4. nesneler ve sınıflar
4. nesneler ve sınıflar4. nesneler ve sınıflar
4. nesneler ve sınıflar
 

Recently uploaded

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

Data structure week 3

  • 1. Chapter 3 Data Structures Assoc. Prof. Dr. Oğuz FINDIK 2016-2017 KBUZEM KARABUK UNIVERSITY 1
  • 2.  Strings are one-dimesional arrays of char and terminated by the end-of-string sentinel 0 or null character .  String constant are written between double quotes. For example "abc" is a character array of size 4, with the last element being the null character 0.  char *p ="abc"  Printf ("%s%sn",p,p+1); 2 Strings
  • 3.  char *p ="abcde";  char s[]={'a', 'b', 'c', 'd', 'e', '0'};  #include <stdio.h>  #include <stdlib.h>  void f(int a[]);  int main(void) {  char s[] = "Pointer is heart of the C programming";  char *p = " Pointer is heart of the C programming";  int a[3];  double d[5];  printf("%s%dn%s%dn%s%dn%s%dn", "sizeof(s) = ", sizeof(s),  "sizeof(p) = ", sizeof(p), "sizeof(a) = ", sizeof(a),  "sizeof(d) = ", sizeof(d));  f(a);  return EXIT_SUCCESS;  }  void f(int a[]) {  printf("In f(): sizeof(a) = %dn", sizeof(a));  } 3 Strings
  • 4.  #include <stdio.h>  #include <stdlib.h>  int find_size(char *);  int main(void) {  char *s = "Pointer is heart of the C programming";  printf("Character of String : %d", find_size(s));  return EXIT_SUCCESS;  }  int find_size(char *s) {  int sum = 0;  while (*s != '0') {  while(isspace(*s)) s++;  if (*s!='0'){  sum++;  s++;  }  }  return sum;  } 4 Characters of Strings
  • 5. #include <stdio.h> #include <stdlib.h> int find_size(char *); int main(void) { char *s = "Pointer is heart of the C programming "; printf("Words of String : %d", find_size(s)); return EXIT_SUCCESS; } int find_size(char *s) { int sum = 0; while (*s != '0') { while(isspace(*s)) s++; if (*s!='0'){ sum++; while (!isspace(*s) && *s!='0') s++; } } return sum; } 5 Words of Strings
  • 6. #include <stdio.h> #include <stdlib.h> void *str_cpy(char *, register const char *); int str_len(const char *); int main(void) { char c[] = "Pointers is important for C programmer"; char *s1 = malloc((str_len(c) + 1) * sizeof(char)); printf("%s", str_cpy(s1, c)); return EXIT_SUCCESS; } 6 String copy Function int str_len(const char *s1) { int length = 0; while (*s1++ != '0') length++; return length; } void *str_cpy(char *s1, register const char *s2) { register char *p = s1; while (*p++ = *s2++) ; return s1; }
  • 7.  int a[3][5];  Starting at the base adress of the array, all the array elements are stored contiguously in memory. The array name a by itself is equivalent to &a[0]. It is pointer to an array of 5 ints. 7 Multidimesional Array
  • 8.  In C, array elements can be of any type, including pointer type. 8 Array of pointers
  • 9. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXWORD 50 #define N 10 void wrt_words(char **); void swap(char **, char **); void sort_words(char **); int main(void) { char word[MAXWORD]; char *w[N]; int n, i; for (i = 0; i < N; i++) { scanf("%s", word); fflush(stdout); w[i] = calloc(strlen(word) + 1, sizeof(char)); strcpy(w[i], word); } sort_words(w); wrt_words(w); return EXIT_SUCCESS; } 9 Example void sort_words(char **a) { int i, j; for (i = 0; i < N; i++) { for (j = i + 1; j < N; j++) { if (strcmp(a[i], a[j]) > 0) swap(&a[i], &a[j]); } }} void swap(char **p, char **q) { char *temp; temp = *p; *p = *q; *q = temp; } void wrt_words(char **a) { int i; for (i = 0; i < N; i++) { printf("%sn", a[i]); } }
  • 10.  typedef is a keyword used in C language to assign alternative names to existing types. Its mostly used with user defined data types, when names of data types get slightly complicated. Also typedef improves portability of your product. Following is the general syntax for using typedef,  typedef existing_name alias_name  typedef unsigned long ulong;  ulong i, j ; 10 typedef
  • 11.  Enumerated Types are a special way of creating your own Type in C  enum boolean {true,false};  enum day {Pazar,Pazartesi,Sali,carsamba,persembe,Cuma,Cumartesi}  enum day =Pazar; 11 Enumerated Types
  • 12.  The structure mechanism provides a means to aggregate variables of different types. Let us define a structure that describes student that has name, surname and number. struct student{ int sequence; char *name; char *surname; char *number; }; struct student s1,s2; 12 Structure and Unions
  • 13. #include <stdio.h> #include <stdlib.h> typedef struct { int sequence; char *name; char *surname; char *number; } student; int main(void) { int sira =0; student s1; s1.name = calloc(50,sizeof(char)); strcpy(s1.name,"Oguz"); s1.surname ="FINDIK"; s1.number="0102124525"; s1.sequence =++sira; printf("Sequence :%dn",s1.sequence); printf("Name :%sn",s1.name); printf("Surname :%sn",s1.surname); printf("Number :%sn",s1.number); return EXIT_SUCCESS; } 13 Example
  • 14. A union, like a structure, is a derived type. Unions follow the same syntax as structures but have members that share storage. A union type defines a set of alternative values that may be stored in a shared portion of memory. The programmer is responsible for interpreting the stored values correctly. Consider the declaration typedef union int_or_float { int i; float f; } number; 14 Unions
  • 15. #include <stdio.h> #include <stdlib.h> typedef union int_or_float { int i; float f; } number; int main(void) { number n; n.i = 10; printf("d: %dn", n.i); n.f = 20.0; printf("f: %fn", n.f); printf("d: %dn", n.i); return EXIT_SUCCESS; } 15 Example