SlideShare a Scribd company logo
Chapter 2
Data Structures
Assoc. Prof. Dr. Oğuz FINDIK
2016-2017
KBUZEM
KARABUK UNIVERSITY
1
 An Array is a data type that uses subscripted variables and
makes it possible to represantations of a large number of
homegeneus values.
 Arrays and pointers are closely related concepts. An Array
name by itself is treated as a constant pointer, and
pointers, like arrays, can be subscripted.
 Strings are one-dimensional arrays of characters.
2
Arrays, Pointers and Strings
 Programs often use homogeneous data. For example we want
to store some grades and we should define as follows
 int grade0,grade1,grade2,grade3;
 We can define one array to hold these values.
 int grade[3];
 The integer 3 in the declerations represents the number of
elements in the array. The indexing of array elements always
starts at 0.
3
One-dimesional Arrays
 int a[size];
 Lower bound = 0
 Upper bound = size – 1
 Size upper bound + 1
 İt is a good way to define size of and Array as symbolic constant
 #define N 100
 int a[N];
 for(i=0;i<N;i++)
 sum +=a[i];
4
One-dimesional Arrays
 float f[5]={0.0,1.0,2.0,3.0,4.0};
 int a[100] = {0};
 int [] ={2,3,5,-7};
 char s[] = " abc"
 char s[] ={‘a’, ‘b’, ‘c’};
5
İnitialization
 A simple variable in a program is stored in
a certain number of bytes at a particular
memory locaiton, or adress. Pointers are
used in programs to access memory and
manipulate them.
 İf v is a variable, then &v is the locaiton or
address in memory of tis stored value.
 int *p;
 p=0;
 p=NULL;
 p=&i;
 p=(int *)1776; 6
Pointers
 İf p is a pointer,
then *p is the
value of the
variable of which
p is the address.
The direct value of
p is a memory
location, whereas
*p is the indirect
value of p
 int a=1,b=2,*p;
 p=&a;
7
Pointers example
a b p
a b p
8
Example
9
Call by Value/Call by reference
 An array name by itself is an adress, and pointers can be subscripted.
Pointer variable can take different addresses as values. In contrast, an
array name is an address, or pointer, that is fixed.
 Suppose that a is an array, iis an int and p is a pointer.
 a[i] is equivalent to *(a+i)
 p[i] is equivalent to *(p+i)
 #define N 100
 int a[N], i,*p,sum=0;
 p=a; is equivalent to p=&a[0]
p=a;
for(p=a;p<&a[N];++p) for(i=0;i<N;++i) for(i=0;i<N;++i)
sum +=*p; sum +=*(a+i) sum +=p[i];
10
The relationship between arrays and
Pointers
 İf the variable p is a pointer to a particular type, then the
expression p+1 yields the correct machine address for storing or
accessing the next variable of that type. And we can use these
expression as same meaning.
11
Pointer Arithmetic and Element Size
 #include <stdio.h>
 #include <stdlib.h>
 #define N 10
 int sum(int *);
 int main(void) {
 int a[10] = { 2, 3, 4, 5, 7, 8, 1, 2, 1, 6 };
 printf("Sum of array is:%d", sum(a));
 return 0;
 }
 int sum(int a[]) {
 int toplam = 0, *p;
 for (p = a; p < &a[N]; p++) {
 toplam += *p;
 }
 return toplam;
 }
12
Arrays as Function Arguments
 void swap(int *, int *);
 int main(void) {
 int a[10] = { 2, 3, 4, 5, 7, 8, 1, 2, 1, 6 };
 int i, j;
 for (i = 0; i < N; i++)
 for (j = N - 1; j > i; --j) {
 if (a[j - 1] > a[j])
 swap(&a[j - 1], &a[j]);
 }
 printf("{");
 for (i = 0; i < N; i++)
 printf("%d %s", a[i], (i < (N - 1)) ? "," : "}");
 return 0;
 } 13
Example Buble Sort
14
Dynamic Memory Allocation With
calloc() and malloc()
 Function prototypes are in the stdlib.h. The calloc stand for
contiguous allocation and malloc stands for memory allocation.
 We can uses calloc() and malloc() to dynamically create space
for arrays, structures and unions.

15
Example

More Related Content

PDF
Data structure week 3
PPT
detailed information about Pointers in c language
PDF
Module 02 Pointers in C
PPTX
Pointer in C
PPT
Pointers in C
PPT
Pointers+(2)
PPT
Pointer in C
PPTX
C programming - Pointers
Data structure week 3
detailed information about Pointers in c language
Module 02 Pointers in C
Pointer in C
Pointers in C
Pointers+(2)
Pointer in C
C programming - Pointers

What's hot (20)

PPT
Pointers - DataStructures
PPTX
Pointers in C
PPTX
Pointers
PPTX
Pointer in c program
PPT
Lecture 17 - Strings
PPT
PDF
Lk module5 pointers
PDF
PPTX
COM1407: Working with Pointers
PPTX
Pointers in c++
PPTX
Pointers in c++
PDF
Lecturer23 pointersin c.ppt
PDF
PPTX
Pointers in C/C++ Programming
PPTX
Array, string and pointer
PPT
Lecture 18 - Pointers
PPTX
Used of Pointer in C++ Programming
Pointers - DataStructures
Pointers in C
Pointers
Pointer in c program
Lecture 17 - Strings
Lk module5 pointers
COM1407: Working with Pointers
Pointers in c++
Pointers in c++
Lecturer23 pointersin c.ppt
Pointers in C/C++ Programming
Array, string and pointer
Lecture 18 - Pointers
Used of Pointer in C++ Programming
Ad

Similar to Data structure week 2 (20)

PPT
ch08.ppt
PPTX
3.ArraysandPointers.pptx
PPSX
Pointers
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PPTX
Presentation of c 1
PPTX
POLITEKNIK MALAYSIA
PPTX
Address, Pointers, Arrays, and Structures.pptx
PPT
c-arrays-pointers.ppt
PPT
c-arrays-pointer basics xxxx yyyy zzzzzzz
PPTX
week14Pointers_II. pointers pemrograman dasar C++.pptx
PPTX
C Programming : Pointers and Arrays, Pointers and Strings
PDF
C Recursion, Pointers, Dynamic memory management
PPTX
Chapter 13.pptx
PPTX
C++ Pointer | Introduction to programming
PPT
Pointers C programming
PPTX
Pointers and Dynamic Memory Allocation
PPTX
C (PPS)Programming for problem solving.pptx
PPTX
Co&amp;al lecture-08
PPT
show the arrays ppt for first year students
PPT
C array and Initialisation and declaration
ch08.ppt
3.ArraysandPointers.pptx
Pointers
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Presentation of c 1
POLITEKNIK MALAYSIA
Address, Pointers, Arrays, and Structures.pptx
c-arrays-pointers.ppt
c-arrays-pointer basics xxxx yyyy zzzzzzz
week14Pointers_II. pointers pemrograman dasar C++.pptx
C Programming : Pointers and Arrays, Pointers and Strings
C Recursion, Pointers, Dynamic memory management
Chapter 13.pptx
C++ Pointer | Introduction to programming
Pointers C programming
Pointers and Dynamic Memory Allocation
C (PPS)Programming for problem solving.pptx
Co&amp;al lecture-08
show the arrays ppt for first year students
C array and Initialisation and declaration
Ad

More from karmuhtam (20)

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

Recently uploaded (20)

PPTX
Pharma ospi slides which help in ospi learning
PDF
Sports Quiz easy sports quiz sports quiz
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Classroom Observation Tools for Teachers
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Lesson notes of climatology university.
PPTX
Cell Structure & Organelles in detailed.
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pharma ospi slides which help in ospi learning
Sports Quiz easy sports quiz sports quiz
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Classroom Observation Tools for Teachers
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Lesson notes of climatology university.
Cell Structure & Organelles in detailed.
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
O7-L3 Supply Chain Operations - ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Computing-Curriculum for Schools in Ghana
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

Data structure week 2

  • 1. Chapter 2 Data Structures Assoc. Prof. Dr. Oğuz FINDIK 2016-2017 KBUZEM KARABUK UNIVERSITY 1
  • 2.  An Array is a data type that uses subscripted variables and makes it possible to represantations of a large number of homegeneus values.  Arrays and pointers are closely related concepts. An Array name by itself is treated as a constant pointer, and pointers, like arrays, can be subscripted.  Strings are one-dimensional arrays of characters. 2 Arrays, Pointers and Strings
  • 3.  Programs often use homogeneous data. For example we want to store some grades and we should define as follows  int grade0,grade1,grade2,grade3;  We can define one array to hold these values.  int grade[3];  The integer 3 in the declerations represents the number of elements in the array. The indexing of array elements always starts at 0. 3 One-dimesional Arrays
  • 4.  int a[size];  Lower bound = 0  Upper bound = size – 1  Size upper bound + 1  İt is a good way to define size of and Array as symbolic constant  #define N 100  int a[N];  for(i=0;i<N;i++)  sum +=a[i]; 4 One-dimesional Arrays
  • 5.  float f[5]={0.0,1.0,2.0,3.0,4.0};  int a[100] = {0};  int [] ={2,3,5,-7};  char s[] = " abc"  char s[] ={‘a’, ‘b’, ‘c’}; 5 İnitialization
  • 6.  A simple variable in a program is stored in a certain number of bytes at a particular memory locaiton, or adress. Pointers are used in programs to access memory and manipulate them.  İf v is a variable, then &v is the locaiton or address in memory of tis stored value.  int *p;  p=0;  p=NULL;  p=&i;  p=(int *)1776; 6 Pointers  İf p is a pointer, then *p is the value of the variable of which p is the address. The direct value of p is a memory location, whereas *p is the indirect value of p
  • 7.  int a=1,b=2,*p;  p=&a; 7 Pointers example a b p a b p
  • 9. 9 Call by Value/Call by reference
  • 10.  An array name by itself is an adress, and pointers can be subscripted. Pointer variable can take different addresses as values. In contrast, an array name is an address, or pointer, that is fixed.  Suppose that a is an array, iis an int and p is a pointer.  a[i] is equivalent to *(a+i)  p[i] is equivalent to *(p+i)  #define N 100  int a[N], i,*p,sum=0;  p=a; is equivalent to p=&a[0] p=a; for(p=a;p<&a[N];++p) for(i=0;i<N;++i) for(i=0;i<N;++i) sum +=*p; sum +=*(a+i) sum +=p[i]; 10 The relationship between arrays and Pointers
  • 11.  İf the variable p is a pointer to a particular type, then the expression p+1 yields the correct machine address for storing or accessing the next variable of that type. And we can use these expression as same meaning. 11 Pointer Arithmetic and Element Size
  • 12.  #include <stdio.h>  #include <stdlib.h>  #define N 10  int sum(int *);  int main(void) {  int a[10] = { 2, 3, 4, 5, 7, 8, 1, 2, 1, 6 };  printf("Sum of array is:%d", sum(a));  return 0;  }  int sum(int a[]) {  int toplam = 0, *p;  for (p = a; p < &a[N]; p++) {  toplam += *p;  }  return toplam;  } 12 Arrays as Function Arguments
  • 13.  void swap(int *, int *);  int main(void) {  int a[10] = { 2, 3, 4, 5, 7, 8, 1, 2, 1, 6 };  int i, j;  for (i = 0; i < N; i++)  for (j = N - 1; j > i; --j) {  if (a[j - 1] > a[j])  swap(&a[j - 1], &a[j]);  }  printf("{");  for (i = 0; i < N; i++)  printf("%d %s", a[i], (i < (N - 1)) ? "," : "}");  return 0;  } 13 Example Buble Sort
  • 14. 14 Dynamic Memory Allocation With calloc() and malloc()  Function prototypes are in the stdlib.h. The calloc stand for contiguous allocation and malloc stands for memory allocation.  We can uses calloc() and malloc() to dynamically create space for arrays, structures and unions. 