SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Pointers - DataStructures
Pointers - DataStructuresPointers - DataStructures
Pointers - DataStructures
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Pointers
PointersPointers
Pointers
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
Pointer
PointerPointer
Pointer
 
Ponters
PontersPonters
Ponters
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Pointers
PointersPointers
Pointers
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
Pointer
PointerPointer
Pointer
 
Pointers in C/C++ Programming
Pointers in C/C++ ProgrammingPointers in C/C++ Programming
Pointers in C/C++ Programming
 
Array, string and pointer
Array, string and pointerArray, string and pointer
Array, string and pointer
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
C Structure and Union in C
C Structure and Union in CC Structure and Union in C
C Structure and Union 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
 

Ähnlich wie Data structure week 2 (20)

C pointers and references
C pointers and referencesC pointers and references
C pointers and references
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
pointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptxpointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptx
 
Chap 11(pointers)
Chap 11(pointers)Chap 11(pointers)
Chap 11(pointers)
 
Pointers and arrays
Pointers and arraysPointers and arrays
Pointers and arrays
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Pointers
PointersPointers
Pointers
 
SPC Unit 3
SPC Unit 3SPC Unit 3
SPC Unit 3
 
Pointers
PointersPointers
Pointers
 
Pointers
PointersPointers
Pointers
 
Lect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer AbbasLect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer Abbas
 
c-arrays-pointers.ppt
c-arrays-pointers.pptc-arrays-pointers.ppt
c-arrays-pointers.ppt
 
c-arrays-pointer basics xxxx yyyy zzzzzzz
c-arrays-pointer basics xxxx yyyy zzzzzzzc-arrays-pointer basics xxxx yyyy zzzzzzz
c-arrays-pointer basics xxxx yyyy zzzzzzz
 
Chapter09-10.PPT
Chapter09-10.PPTChapter09-10.PPT
Chapter09-10.PPT
 
Chapter09-10 Pointers and operations .PPT
Chapter09-10  Pointers and operations .PPTChapter09-10  Pointers and operations .PPT
Chapter09-10 Pointers and operations .PPT
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Arrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptxArrays to arrays and pointers with arrays.pptx
Arrays to arrays and pointers with arrays.pptx
 
Python
PythonPython
Python
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 

Kürzlich hochgeladen (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 

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. 