SlideShare a Scribd company logo
CProgramming Language
&
Data Structure
Prepared by: Moโ€™meN M. Ali
E-mail: momen.1993@live.com
References
โ€ข www.stackoverflow.com
โ€ข C Primer Plus 6th Edition
โ€ข Let Us C 5th Edition
โ€ข The C Programming Language 2nd Edition
โ€ข C Modern Approach 2nd Edition
โ€ข Data Structures and Algorithm Analysis in C 2nd Edition
Moโ€™meN M. Ali
C Programming Language
Review Programs are uploaded to this Git repo
https://github.com/Mo2meN-
Ali/x86/tree/master/Programming%20Course/1-
Arrays%20%26%20Pointers
C Programming Language
Moโ€™meN M. Ali
Topics
โ€ข Arrays & Pointers.
โ€ข Character String & String Functions.
โ€ข Storage Classes, Linkage & Memory Management.
โ€ข File Input/Output.
โ€ข Structures & Other Data Formats.
โ€ข Bit Fiddling.
โ€ข The C Preprocessor & The C Library.
โ€ข Algorithm Analysis & ADT.
โ€ข Stacks & Queues.
โ€ข Trees.
C Programming Language
Moโ€™meN M. Ali
Today You Will Learn About:
โ€ข Keyword: static.
โ€ข Operators: & * (unary).
โ€ข How to create and initialize arrays.
โ€ข Pointers.
โ€ข Writing functions that process arrays.
โ€ข Two-dimensional arrays.
C Programming Language
Moโ€™meN M. Ali
Arrays
โ€ข An array is composed of a series of elements of one data type.
โ€ข An array declaration tells the compiler how many elements the array
contains and what the type is for these elements.
โ€ข To access elements in an array, you identify an individual element by using
its subscript number, also called its index,The numbering starts with 0.
C Programming Language
Moโ€™meN M. Ali
/* some array declarations */
int main(void)
{
float candy[365]; /* array of 365 floats */
char code[12]; /* array of 12 chars */
int states[50]; /* array of 50 ints */
}
C Programming Language
Moโ€™meN M. Ali
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS]= {31,28,31,30,31,30,31,31,30,31,30,31};
int index;
for (index = 0; index < MONTHS; index++)
printf("Month %d has %2d days.n", index +1, days[index]);
return 0;
}
Output
Month 1 has 31 days.
Month 2 has 28 days.
Month 3 has 31 days.
Month 4 has 30 days.
Month 5 has 31 days.
Month 6 has 30 days.
Month 7 has 31 days.
Month 8 has 31 days.
Month 9 has 30 days.
Month 10 has 31 days.
Month 11 has 30 days.
Month 12 has 31 days.
C Programming Language
Moโ€™meN M. Ali
Entering Data into an Array
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "nEnter marks " ) ;
scanf ( "%d", &states[i] ) ;
}
C Programming Language
Moโ€™meN M. Ali
Reading data from an array
for ( i = 0 ; i <= 29 ; i++ )
sum= sum + marks[i] ;
C Programming Language
Moโ€™meN M. Ali
#include <stdio.h>
#define SIZE 4
int main(void)
{
int some_data[SIZE]= {1492, 1066};
int i;
printf("%2s%14sn", i, some_data[i]);
for (i = 0; i < SIZE; i++)
printf("%2d%14dn", i, some_data[i]);
return 0;
}
Output
i some_data[i]
0 1492
1 1066
2 0
3 0
C Programming Language
Moโ€™meN M. Ali
C Programming Language
Moโ€™meN M. Ali
#include <stdio.h>
int main(void)
{
const int days[]= {31,28,31,30,31,30,31,31,30,31};
int index;
for (index= 0; index < ( sizeof (days) / sizeof (days[0]) ); index++)
printf("Month %2d has %d days.n", index +1, days[index]);
return 0;
}
Output
Month 1 has 31 days.
Month 2 has 28 days.
Month 3 has 31 days.
Month 4 has 30 days.
Month 5 has 31 days.
Month 6 has 30 days.
Month 7 has 31 days.
Month 8 has 31 days.
Month 9 has 30 days.
Month 10 has 31 days.
C Programming Language
Moโ€™meN M. Ali
What if you fail to initialize an array?
#include <stdio.h>
#define SIZE 4
int main(void)
{
int no_data[SIZE]; /* uninitialized array */
int i;
printf("%2s%14sn", "i", "no_data[i]");
for (i = 0; i < SIZE; i++)
printf("%2d%14dn", i, no_data[i]);
return 0;
}
C Programming Language
Moโ€™meN M. Ali
Output (Your Results may vary)
i no_data[i]
0 0
1 4204937
2 4219854
3 2147348480
C Programming Language
Moโ€™meN M. Ali
Array Bounds
โ€ข You have to make sure you use array indices that are within bounds; that
is, you have to make sure they have values valid for the array. For
instance, suppose you make the following declaration:
int doofi[20];
โ€ข Then it's your responsibility to make sure the program uses indices only in
the range 0 through 19, because the compiler won't check for you.
C Programming Language
Moโ€™meN M. Ali
C Programming Language
Moโ€™meN M. Ali
#include <stdio.h>
#define SIZE 4
int main(void)
{
int value1 = 44;
int arr[SIZE];
int value2 = 88;
int i;
printf("value1 = %d, value2 = %dn", value1, value2);
for (i = -1; i <= SIZE; i++)
arr[i] = 2 * i + 1;
for (i = -1; i < 7; i++)
printf("%2d %dn", i , arr[i]);
printf("value1 = %d, value2 = %dn", value1, value2);
return 0;
}
Output
value1 = 44, value2 = 88
-1 -1
0 1
1 3
2 5
3 7
4 9
5 5
6 1245120
value1 = -1, value2 = 9
C Programming Language
Moโ€™meN M. Ali
Designated Initializers (C99)
โ€ข C99 has added a new capability designated initializers. This feature allows
you to pick and choose which elements are initialized. Suppose, for
example, that you just want to initialize the last element in an array. With
traditional C initialization syntax, you also have to initialize every element
preceding the last one:
int arr[6] = {0,0,0,0,0,212}; // traditional syntax
โ€ข With C99, you can use an index in brackets in the initialization list to specify
a particular element:
int arr[6] = {[5] = 212}; // initialize arr[5] to 212
C Programming Language
Moโ€™meN M. Ali
C Programming Language
Moโ€™meN M. Ali
// designate.c -- use designated initializers
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = {31,28, [4] = 31,30,31, [1] = 29};
int i;
for (i = 0; i < MONTHS; i++)
printf("%2d %dn", i + 1, days[i]);
return 0;
}
Output
1 31
2 29
3 0
4 0
5 31
6 30
7 31
8 0
9 0
10 0
11 0
12 0
C Programming Language
Moโ€™meN M. Ali
Suppose you donโ€™t specify the
array size?
int stuff[] = {1, [6] = 23}; // what happens?
int staff[] = {1, [6] = 4, 9, 10}; // what happens?
โ€ข The compiler will make the array big enough to accommodate the
initialization values. So stuff will have seven elements, numbered 0-6, and
staff will have two more elements, or 9.
C Programming Language
Moโ€™meN M. Ali
Specifying an Array Size
#define SIZE 4
int arr[SIZE]; // symbolic integer constant
double lots[144]; // literal integer constant
int n = 5;
int m = 8;
float a1[5]; // yes
float a2[5*2 + 1]; // yes
float a3[sizeof(int) + 1]; // yes
float a4[-4]; // no, size must be > 0
float a5[0]; // no, size must be > 0
float a6[2.5]; // no, size must be an integer
float a7[(int)2.5]; // yes, typecast float to int constant
float a8[n]; // not allowed before C99
float a9[m]; // not allowed before C99
C Programming Language
Moโ€™meN M. Ali
Exercise 1.0
30 Minutes
MOโ€™MEN M. ALI
C Programming Language
Multidimensional arrays
โ€ข A multidimensional array is an array which every element in it is an array.
float rain[5] [12]; // an array of 12 floats
โ€ข This tells us that each element is of type float[12]; that is, each of the five
elements of rain is, in itself, an array of 12 float values.
C Programming Language
Moโ€™meN M. Ali
Initializing a two-dimensional array
#define MONTHS 12 // number of months in a year
#define YEARS 5 // number of years of data
const float rain[YEARS][MONTHS] = {
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6}, // Row 0 (rain[0])
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3}, // Row 1 (rain[1])
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4}, // Row 2 (rain[2])
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2}, // Row 3 (rain[3])
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2} // Row 4 (rain[4])
};
Review Program: rain
C Programming Language
Moโ€™meN M. Ali
Pointers And Arrays
โ€ข Pointers offer an efficient way to deal with arrays. Indeed.
โ€ข Array notation is simply a disguised use of pointers.
1. date == &date[0]; // name of array is the address of the first element
2. dates +2 == &date[2] /* same address */
3. *(dates + 2) == dates[2] /* same value */
4. *(dates +2) /* value of the 3rd element of dates */
5. *dates +2 /* 2 added to the value of the 1st element */
C Programming Language
Moโ€™meN M. Ali
C Programming Language
Moโ€™meN M. Ali
#include <stdio.h>
#define SIZE 4
int main(void)
{
short dates [SIZE];
short * pti;
short index;
double bills[SIZE];
double * ptf;
pti = dates; // assign address of array to pointer
ptf = bills;
printf("%23s %15sn", "short", "double");
for (index = 0; index < SIZE; index ++)
printf("pointers + %d: %10p %10pn", index, pti + index, ptf + index);
return 0;
}
Output
short double
pointers + 0: 0x7fff5fbff8dc 0x7fff5fbff8a0
pointers + 1: 0x7fff5fbff8de 0x7fff5fbff8a8
pointers + 2: 0x7fff5fbff8e0 0x7fff5fbff8b0
pointers + 3: 0x7fff5fbff8e2 0x7fff5fbff8b8
C Programming Language
Moโ€™meN M. Ali
C Programming Language
Moโ€™meN M. Ali
/* day_mon3.c -- uses pointer notation */
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31};
int index;
for (index = 0; index < MONTHS; index++)
printf("Month %2d has %d days.n",
index +1, *(days + index)); // same as days[index]
return 0;
}
Functions, Arrays, and Pointers
โ€ข Suppose you want to write a function that operates on an array. For example,
suppose you want a function that returns the sum of the elements of an
array. Suppose marbles is the name of an array of int. What would the
function call look like? A reasonable guess would be this:
total = sum(marbles); // possible function call
โ€ข What would the prototype be? Remember, the name of an array is the
address of its first element,
int sum(int * ar); // corresponding prototype
C Programming Language
Moโ€™meN M. Ali
C Programming Language
Moโ€™meN M. Ali
int sum(int * ar) // corresponding definition
{
int i;
int total = 0;
for( i = 0; i < 10; i++) // assume 10 elements
total += ar[i]; // ar[i] the same as *(ar + i)
return total;
}
int sum(int * ar, int n) // more general approach
{
int i;
int total = 0;
for( i = 0; i < n; i++) // use n elements
total += ar[i]; // ar[i] the same as *(ar + i)
return total;
}
Using Pointer Parameters
โ€ข A function working on an array needs to know where to start and stop.
โ€ข Another way to describe the array is by passing two pointers, with the first
indicating where the array starts (as before) and the second where the array
ends.
โ€ข Now, the function can alter the value of the pointer itself, making it point to
each array element in turn.
C Programming Language
Moโ€™meN M. Ali
Arrays as Arguments
โ€ข Because the name of an array is the address of the first element, an actual
argument of an array name requires that the matching formal argument be a
pointer. Also, C interprets int ar[] to mean the same as int * ar.
โ€ข All four of the following prototypes are equivalent:
int sum(int *ar, int n);
int sum(int *, int);
int sum(int ar[], int n);
int sum(int [], int);
Review Program: Array as arguments
C Programming Language
Moโ€™meN M. Ali
Pointer Operations
Review Program: PointerOperations
โ€ข Assignmentโ€” You can assign an address to a pointer. The assigned value can be, for
example, an array name, a variable preceded by address operator ( & ), or another second
pointer.
โ€ข Value finding (dereferencing)โ€” The * operator gives the value stored in the pointed-to
location. Therefore, *ptr1 is initially 100 , the value stored at location 0x7fff5fbff8d0 .
โ€ข Taking a pointer addressโ€” Like all variables, a pointer variable has an address and a value.
The & operator tells you where the pointer itself is stored.
โ€ข Adding an integer to a pointerโ€” You can use the + operator to add an integer to a pointer
or a pointer to an integer. In either case, the integer is multiplied by the number of bytes in
the pointed-to type, and the result is added to the original address. This makes ptr1 + 4 the
same as &urn[4] .
C Programming Language
Moโ€™meN M. Ali
โ€ข Incrementing a pointerโ€” Incrementing a pointer to an array element makes it move to the
next element of the array. Therefore, ptr1++ increases the numerical value of ptr1 by 4 (4
bytes per int on our system) and makes ptr1 point to urn[1].
โ€ข Subtracting an integer from a pointerโ€” You can use the - operator to subtract an integer
from a pointer; the pointer has to be the first operand and the integer value the second
operand. The integer is multiplied by the number of bytes in the pointed-to type, and the
result is subtracted from the original address.
โ€ข Decrementing a pointerโ€” Of course, you can also decrement a pointer. In this example,
decrementing ptr2 makes it point to the second array element instead of the third. Note
that you can use both the prefix and postfix forms of the increment and decrement
operators.
โ€ข Differencingโ€” You can find the difference between two pointers. Normally, you do this for
two pointers to elements that are in the same array to find out how far apart the elements
are. The result is in the same units as the type size.
โ€ข Comparisonsโ€” You can use the relational operators to compare the values of two pointers,
provided the pointers are of the same type.
C Programming Language
Moโ€™meN M. Ali
Using const with formal
parameters
โ€ข If a function is intent is that it not change the contents of the array, use the keyword
const when declaring the formal parameter in the prototype and in the function
definition.
C Programming Language
Moโ€™meN M. Ali
Pointers to multidimensional
Arrays
โ€ข Since 2D-Arrays are Arrays of arrays, therefore we need a pointer-to-array instead of a
pointer-to-element.
int (* pz)[2]; // pz points to an array of 2 ints. (Can be used as 2D
// Array pointer).
int * pax[2]; // pax is an array of two pointers-to-int (Can no be used as 2D
// Array Pointer).
C Programming Language
Moโ€™meN M. Ali
Pointer Compatibility
โ€ข The rules for assigning one pointer to another are tighter than the rules for numeric
types. For example, you can assign an int value to a double variable without using a
type conversion, but you canโ€™t do the same for pointers to these two types:
int n= 5;
double x;
int * p1= &n;
double * pd= &x;
x= n; // implicit type conversion
pd= p1; // compile-time error
C Programming Language
Moโ€™meN M. Ali
int * pt;
int (*pa)[3];
int ar1[2][3];
int ar2[3][2];
int **p2; // a pointer to a pointer
pt = &ar1[0][0]; // both pointer-to-int
pt = ar1[0]; // both pointer-to-int
pt = ar1; // not valid
pa = ar1; // both pointer-to-int[3]
pa = ar2; // not valid
p2 = &pt; // both pointer-to-int *
*p2 = ar2[0]; // both pointer-to-int
p2 = ar2; // not valid
Review Program: Pointers and 2D-Arrays
C Programming Language
Moโ€™meN M. Ali
Functions and multidimensional
arrays
โ€ข If you want to write functions that process two-dimensional arrays, you need to
understand pointers well enough to make the proper declarations for function
arguments. In the function body itself, you can usually get by with array notation.
int junk[3][4] = { {2,4,5,8}, {3,5,6,9}, {12,10,8,6} };
int i, j;
int total = 0;
for (i = 0; i < 3 ; i++)
total += sum(junk[i], 4); // junk[i] -- one-dimensional array
C Programming Language
Moโ€™meN M. Ali
Exercise 1.1
30 Minutes
MOโ€™MEN M. ALI
C Programming Language
Variable-length arrays (VLAs)
โ€ข You may have noticed that you can not use a variable size multidimensional array as an
argument, that is because you always have to use a constant columns. Well VLAs is the
C99 way to solve this problem. You can use VLAs to pass a variable length
multidimensional array.
int sum2d(int rows, int cols, int ar[rows][cols]); // array a VLA
int sum2d(int, int, int ar[*][*]); // array a VLA, names omitted
โ€ข rows and cols are two variable arguments that can be passed at run-time.
โ€ข rows and cols must be define before the array.
int sum2d(int ar[rows][cols], int rows, int cols); // invalid order
Review Program: Functions usingVLAs
C Programming Language
Moโ€™meN M. Ali
More dimensions
โ€ข Everything we have said about two-dimensional arrays can be generalized
to three-dimensional arrays and further. You can declare a three-
dimensional array this way:
int box[10][20][30];
โ€ข You can visualize a one-dimensional array as a row of data, a two-
dimensional array as a table of data, and a three-dimensional array as a
stack of data tables. For example, you can visualize the box array as 10
two-dimensional arrays (each 20ร—30) stacked atop each other.
C Programming Language
Moโ€™meN M. Ali
Compound Literals
MOโ€™MEN M. ALI
C Programming Language
Search Yourself
THANK YOUSEE YOU SOON
C Programming Language
Moโ€™meN M. Ali

More Related Content

What's hot (20)

PDF
Functions, Strings ,Storage classes in C
arshpreetkaur07
ย 
PPS
C programming session 09
Dushmanta Nath
ย 
PDF
Structures-2
arshpreetkaur07
ย 
PPT
Chapter Eight(3)
bolovv
ย 
PPT
Chapter Eight(1)
bolovv
ย 
PPTX
C Programming Unit-2
Vikram Nandini
ย 
PDF
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
ย 
PDF
Tutorial on c language programming
Sudheer Kiran
ย 
PDF
Introduction to c programming
Infinity Tech Solutions
ย 
PDF
C++
Shyam Khant
ย 
PPT
02a fundamental c++ types, arithmetic
Manzoor ALam
ย 
PPTX
Learn c++ Programming Language
Steve Johnson
ย 
PPT
C++ Language
Syed Zaid Irshad
ย 
PPT
Basics of c++ Programming Language
Ahmad Idrees
ย 
DOCX
Theory1&amp;2
Dr.M.Karthika parthasarathy
ย 
PPTX
Unit 3
GOWSIKRAJAP
ย 
PDF
C interview-questions-techpreparation
Kushaal Singla
ย 
PDF
C++ questions And Answer
lavparmar007
ย 
PDF
C programming session8
Keroles karam khalil
ย 
PDF
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
chinthala Vijaya Kumar
ย 
Functions, Strings ,Storage classes in C
arshpreetkaur07
ย 
C programming session 09
Dushmanta Nath
ย 
Structures-2
arshpreetkaur07
ย 
Chapter Eight(3)
bolovv
ย 
Chapter Eight(1)
bolovv
ย 
C Programming Unit-2
Vikram Nandini
ย 
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
ย 
Tutorial on c language programming
Sudheer Kiran
ย 
Introduction to c programming
Infinity Tech Solutions
ย 
C++
Shyam Khant
ย 
02a fundamental c++ types, arithmetic
Manzoor ALam
ย 
Learn c++ Programming Language
Steve Johnson
ย 
C++ Language
Syed Zaid Irshad
ย 
Basics of c++ Programming Language
Ahmad Idrees
ย 
Theory1&amp;2
Dr.M.Karthika parthasarathy
ย 
Unit 3
GOWSIKRAJAP
ย 
C interview-questions-techpreparation
Kushaal Singla
ย 
C++ questions And Answer
lavparmar007
ย 
C programming session8
Keroles karam khalil
ย 
CBSE Class 12 Computer Science(083) Sample Question Paper 2020-21
chinthala Vijaya Kumar
ย 

Viewers also liked (18)

PPTX
Array in Java
Ali shah
ย 
PPTX
Software Engineering for Web Applications
Moh'd Shakeb Baig
ย 
PDF
Fundamentals of data structure in c s. sahni , s. anderson freed and e. horowitz
JAKEwook
ย 
PPT
Pointers C programming
Appili Vamsi Krishna
ย 
PPT
Pointers - DataStructures
Omair Imtiaz Ansari
ย 
PPSX
C programming pointer
argusacademy
ย 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
ย 
PPTX
2- Dimensional Arrays
Education Front
ย 
PPT
Two dimensional array
Rajendran
ย 
PPTX
2CPP06 - Arrays and Pointers
Michael Heron
ย 
PPTX
C pointer
University of Potsdam
ย 
PPTX
Procedural programming
Ankit92Chitnavis
ย 
PDF
4 the relational data model and relational database constraints
Kumar
ย 
PPTX
Procedural vs. object oriented programming
Haris Bin Zahid
ย 
PPTX
Web engineering lecture 1
University of Swat
ย 
PPTX
Data structure and its types
Navtar Sidhu Brar
ย 
PPT
Lect 1. introduction to programming languages
Varun Garg
ย 
PPT
DATA STRUCTURES
bca2010
ย 
Array in Java
Ali shah
ย 
Software Engineering for Web Applications
Moh'd Shakeb Baig
ย 
Fundamentals of data structure in c s. sahni , s. anderson freed and e. horowitz
JAKEwook
ย 
Pointers C programming
Appili Vamsi Krishna
ย 
Pointers - DataStructures
Omair Imtiaz Ansari
ย 
C programming pointer
argusacademy
ย 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
ย 
2- Dimensional Arrays
Education Front
ย 
Two dimensional array
Rajendran
ย 
2CPP06 - Arrays and Pointers
Michael Heron
ย 
C pointer
University of Potsdam
ย 
Procedural programming
Ankit92Chitnavis
ย 
4 the relational data model and relational database constraints
Kumar
ย 
Procedural vs. object oriented programming
Haris Bin Zahid
ย 
Web engineering lecture 1
University of Swat
ย 
Data structure and its types
Navtar Sidhu Brar
ย 
Lect 1. introduction to programming languages
Varun Garg
ย 
DATA STRUCTURES
bca2010
ย 
Ad

Similar to C programming & data structure [arrays & pointers] (20)

PDF
arraysfor engineering students sdf ppt on arrays
ruvirgandhi123
ย 
PPT
Ch7
Ramesh Ankathi
ย 
PPT
Ch7
sanya6900
ย 
PPTX
C language
Priya698357
ย 
PPTX
Arrays basics
sudhirvegad
ย 
PPTX
Chapter1.pptx
WondimuBantihun1
ย 
PDF
Programming Fundamentals Arrays and Strings
imtiazalijoono
ย 
PDF
8 arrays and pointers
MomenMostafa
ย 
PPTX
Abir ppt3
abir96
ย 
PPT
Project presentation(View calender)
Ikhtiar Khan Sohan
ย 
PPT
Unit i intro-operators
HINAPARVEENAlXC
ย 
PPTX
COM1407: Arrays
Hemantha Kulathilake
ย 
PDF
Session 5-exersice
Keroles karam khalil
ย 
PDF
c++ referesher 1.pdf
AnkurSingh656748
ย 
ODP
C prog ppt
xinoe
ย 
PPTX
Introduction to c part 2
baabtra.com - No. 1 supplier of quality freshers
ย 
PPT
Arrays in c programing. practicals and .ppt
Carlos701746
ย 
PPT
R Programming Intro
062MayankSinghal
ย 
PDF
L5 array
Santoshkumar Balkunde
ย 
arraysfor engineering students sdf ppt on arrays
ruvirgandhi123
ย 
Ch7
sanya6900
ย 
C language
Priya698357
ย 
Arrays basics
sudhirvegad
ย 
Chapter1.pptx
WondimuBantihun1
ย 
Programming Fundamentals Arrays and Strings
imtiazalijoono
ย 
8 arrays and pointers
MomenMostafa
ย 
Abir ppt3
abir96
ย 
Project presentation(View calender)
Ikhtiar Khan Sohan
ย 
Unit i intro-operators
HINAPARVEENAlXC
ย 
COM1407: Arrays
Hemantha Kulathilake
ย 
Session 5-exersice
Keroles karam khalil
ย 
c++ referesher 1.pdf
AnkurSingh656748
ย 
C prog ppt
xinoe
ย 
Arrays in c programing. practicals and .ppt
Carlos701746
ย 
R Programming Intro
062MayankSinghal
ย 
Ad

More from MomenMostafa (9)

PDF
9 character string &amp; string library
MomenMostafa
ย 
PDF
7 functions
MomenMostafa
ย 
PDF
6 c control statements branching &amp; jumping
MomenMostafa
ย 
PDF
5 c control statements looping
MomenMostafa
ย 
PDF
4 operators, expressions &amp; statements
MomenMostafa
ย 
PDF
2 data and c
MomenMostafa
ย 
PDF
1 introducing c language
MomenMostafa
ย 
PDF
3 character strings and formatted input output
MomenMostafa
ย 
PPTX
Embedded System Practical Workshop using the ARM Processor
MomenMostafa
ย 
9 character string &amp; string library
MomenMostafa
ย 
7 functions
MomenMostafa
ย 
6 c control statements branching &amp; jumping
MomenMostafa
ย 
5 c control statements looping
MomenMostafa
ย 
4 operators, expressions &amp; statements
MomenMostafa
ย 
2 data and c
MomenMostafa
ย 
1 introducing c language
MomenMostafa
ย 
3 character strings and formatted input output
MomenMostafa
ย 
Embedded System Practical Workshop using the ARM Processor
MomenMostafa
ย 

Recently uploaded (20)

PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
ย 
PPTX
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
ย 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
PDF
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
ย 
PPTX
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
ย 
PDF
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
ย 
PPTX
computer forensics encase emager app exp6 1.pptx
ssuser343e92
ย 
PPTX
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
ย 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
ย 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
PPTX
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
ย 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
ย 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
ย 
Avast Premium Security crack 25.5.6162 + License Key 2025
HyperPc soft
ย 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
ย 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
ย 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
ย 
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
ย 
Seamless-Image-Conversion-From-Raster-to-wrt-rtx-rtx.pptx
Quick Conversion Services
ย 
capitulando la keynote de GrafanaCON 2025 - Madrid
Imma Valls Bernaus
ย 
computer forensics encase emager app exp6 1.pptx
ssuser343e92
ย 
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
ย 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
ย 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
ย 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
ย 
EO4EU Ocean Monitoring: Maritime Weather Routing Optimsation Use Case
EO4EU
ย 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
ย 

C programming & data structure [arrays & pointers]

  • 2. References โ€ข www.stackoverflow.com โ€ข C Primer Plus 6th Edition โ€ข Let Us C 5th Edition โ€ข The C Programming Language 2nd Edition โ€ข C Modern Approach 2nd Edition โ€ข Data Structures and Algorithm Analysis in C 2nd Edition Moโ€™meN M. Ali C Programming Language
  • 3. Review Programs are uploaded to this Git repo https://github.com/Mo2meN- Ali/x86/tree/master/Programming%20Course/1- Arrays%20%26%20Pointers C Programming Language Moโ€™meN M. Ali
  • 4. Topics โ€ข Arrays & Pointers. โ€ข Character String & String Functions. โ€ข Storage Classes, Linkage & Memory Management. โ€ข File Input/Output. โ€ข Structures & Other Data Formats. โ€ข Bit Fiddling. โ€ข The C Preprocessor & The C Library. โ€ข Algorithm Analysis & ADT. โ€ข Stacks & Queues. โ€ข Trees. C Programming Language Moโ€™meN M. Ali
  • 5. Today You Will Learn About: โ€ข Keyword: static. โ€ข Operators: & * (unary). โ€ข How to create and initialize arrays. โ€ข Pointers. โ€ข Writing functions that process arrays. โ€ข Two-dimensional arrays. C Programming Language Moโ€™meN M. Ali
  • 6. Arrays โ€ข An array is composed of a series of elements of one data type. โ€ข An array declaration tells the compiler how many elements the array contains and what the type is for these elements. โ€ข To access elements in an array, you identify an individual element by using its subscript number, also called its index,The numbering starts with 0. C Programming Language Moโ€™meN M. Ali /* some array declarations */ int main(void) { float candy[365]; /* array of 365 floats */ char code[12]; /* array of 12 chars */ int states[50]; /* array of 50 ints */ }
  • 7. C Programming Language Moโ€™meN M. Ali #include <stdio.h> #define MONTHS 12 int main(void) { int days[MONTHS]= {31,28,31,30,31,30,31,31,30,31,30,31}; int index; for (index = 0; index < MONTHS; index++) printf("Month %d has %2d days.n", index +1, days[index]); return 0; }
  • 8. Output Month 1 has 31 days. Month 2 has 28 days. Month 3 has 31 days. Month 4 has 30 days. Month 5 has 31 days. Month 6 has 30 days. Month 7 has 31 days. Month 8 has 31 days. Month 9 has 30 days. Month 10 has 31 days. Month 11 has 30 days. Month 12 has 31 days. C Programming Language Moโ€™meN M. Ali
  • 9. Entering Data into an Array for ( i = 0 ; i <= 29 ; i++ ) { printf ( "nEnter marks " ) ; scanf ( "%d", &states[i] ) ; } C Programming Language Moโ€™meN M. Ali Reading data from an array for ( i = 0 ; i <= 29 ; i++ ) sum= sum + marks[i] ;
  • 10. C Programming Language Moโ€™meN M. Ali #include <stdio.h> #define SIZE 4 int main(void) { int some_data[SIZE]= {1492, 1066}; int i; printf("%2s%14sn", i, some_data[i]); for (i = 0; i < SIZE; i++) printf("%2d%14dn", i, some_data[i]); return 0; }
  • 11. Output i some_data[i] 0 1492 1 1066 2 0 3 0 C Programming Language Moโ€™meN M. Ali
  • 12. C Programming Language Moโ€™meN M. Ali #include <stdio.h> int main(void) { const int days[]= {31,28,31,30,31,30,31,31,30,31}; int index; for (index= 0; index < ( sizeof (days) / sizeof (days[0]) ); index++) printf("Month %2d has %d days.n", index +1, days[index]); return 0; }
  • 13. Output Month 1 has 31 days. Month 2 has 28 days. Month 3 has 31 days. Month 4 has 30 days. Month 5 has 31 days. Month 6 has 30 days. Month 7 has 31 days. Month 8 has 31 days. Month 9 has 30 days. Month 10 has 31 days. C Programming Language Moโ€™meN M. Ali
  • 14. What if you fail to initialize an array? #include <stdio.h> #define SIZE 4 int main(void) { int no_data[SIZE]; /* uninitialized array */ int i; printf("%2s%14sn", "i", "no_data[i]"); for (i = 0; i < SIZE; i++) printf("%2d%14dn", i, no_data[i]); return 0; } C Programming Language Moโ€™meN M. Ali
  • 15. Output (Your Results may vary) i no_data[i] 0 0 1 4204937 2 4219854 3 2147348480 C Programming Language Moโ€™meN M. Ali
  • 16. Array Bounds โ€ข You have to make sure you use array indices that are within bounds; that is, you have to make sure they have values valid for the array. For instance, suppose you make the following declaration: int doofi[20]; โ€ข Then it's your responsibility to make sure the program uses indices only in the range 0 through 19, because the compiler won't check for you. C Programming Language Moโ€™meN M. Ali
  • 17. C Programming Language Moโ€™meN M. Ali #include <stdio.h> #define SIZE 4 int main(void) { int value1 = 44; int arr[SIZE]; int value2 = 88; int i; printf("value1 = %d, value2 = %dn", value1, value2); for (i = -1; i <= SIZE; i++) arr[i] = 2 * i + 1; for (i = -1; i < 7; i++) printf("%2d %dn", i , arr[i]); printf("value1 = %d, value2 = %dn", value1, value2); return 0; }
  • 18. Output value1 = 44, value2 = 88 -1 -1 0 1 1 3 2 5 3 7 4 9 5 5 6 1245120 value1 = -1, value2 = 9 C Programming Language Moโ€™meN M. Ali
  • 19. Designated Initializers (C99) โ€ข C99 has added a new capability designated initializers. This feature allows you to pick and choose which elements are initialized. Suppose, for example, that you just want to initialize the last element in an array. With traditional C initialization syntax, you also have to initialize every element preceding the last one: int arr[6] = {0,0,0,0,0,212}; // traditional syntax โ€ข With C99, you can use an index in brackets in the initialization list to specify a particular element: int arr[6] = {[5] = 212}; // initialize arr[5] to 212 C Programming Language Moโ€™meN M. Ali
  • 20. C Programming Language Moโ€™meN M. Ali // designate.c -- use designated initializers #include <stdio.h> #define MONTHS 12 int main(void) { int days[MONTHS] = {31,28, [4] = 31,30,31, [1] = 29}; int i; for (i = 0; i < MONTHS; i++) printf("%2d %dn", i + 1, days[i]); return 0; }
  • 21. Output 1 31 2 29 3 0 4 0 5 31 6 30 7 31 8 0 9 0 10 0 11 0 12 0 C Programming Language Moโ€™meN M. Ali
  • 22. Suppose you donโ€™t specify the array size? int stuff[] = {1, [6] = 23}; // what happens? int staff[] = {1, [6] = 4, 9, 10}; // what happens? โ€ข The compiler will make the array big enough to accommodate the initialization values. So stuff will have seven elements, numbered 0-6, and staff will have two more elements, or 9. C Programming Language Moโ€™meN M. Ali
  • 23. Specifying an Array Size #define SIZE 4 int arr[SIZE]; // symbolic integer constant double lots[144]; // literal integer constant int n = 5; int m = 8; float a1[5]; // yes float a2[5*2 + 1]; // yes float a3[sizeof(int) + 1]; // yes float a4[-4]; // no, size must be > 0 float a5[0]; // no, size must be > 0 float a6[2.5]; // no, size must be an integer float a7[(int)2.5]; // yes, typecast float to int constant float a8[n]; // not allowed before C99 float a9[m]; // not allowed before C99 C Programming Language Moโ€™meN M. Ali
  • 24. Exercise 1.0 30 Minutes MOโ€™MEN M. ALI C Programming Language
  • 25. Multidimensional arrays โ€ข A multidimensional array is an array which every element in it is an array. float rain[5] [12]; // an array of 12 floats โ€ข This tells us that each element is of type float[12]; that is, each of the five elements of rain is, in itself, an array of 12 float values. C Programming Language Moโ€™meN M. Ali
  • 26. Initializing a two-dimensional array #define MONTHS 12 // number of months in a year #define YEARS 5 // number of years of data const float rain[YEARS][MONTHS] = { {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6}, // Row 0 (rain[0]) {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3}, // Row 1 (rain[1]) {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4}, // Row 2 (rain[2]) {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2}, // Row 3 (rain[3]) {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2} // Row 4 (rain[4]) }; Review Program: rain C Programming Language Moโ€™meN M. Ali
  • 27. Pointers And Arrays โ€ข Pointers offer an efficient way to deal with arrays. Indeed. โ€ข Array notation is simply a disguised use of pointers. 1. date == &date[0]; // name of array is the address of the first element 2. dates +2 == &date[2] /* same address */ 3. *(dates + 2) == dates[2] /* same value */ 4. *(dates +2) /* value of the 3rd element of dates */ 5. *dates +2 /* 2 added to the value of the 1st element */ C Programming Language Moโ€™meN M. Ali
  • 28. C Programming Language Moโ€™meN M. Ali #include <stdio.h> #define SIZE 4 int main(void) { short dates [SIZE]; short * pti; short index; double bills[SIZE]; double * ptf; pti = dates; // assign address of array to pointer ptf = bills; printf("%23s %15sn", "short", "double"); for (index = 0; index < SIZE; index ++) printf("pointers + %d: %10p %10pn", index, pti + index, ptf + index); return 0; }
  • 29. Output short double pointers + 0: 0x7fff5fbff8dc 0x7fff5fbff8a0 pointers + 1: 0x7fff5fbff8de 0x7fff5fbff8a8 pointers + 2: 0x7fff5fbff8e0 0x7fff5fbff8b0 pointers + 3: 0x7fff5fbff8e2 0x7fff5fbff8b8 C Programming Language Moโ€™meN M. Ali
  • 30. C Programming Language Moโ€™meN M. Ali /* day_mon3.c -- uses pointer notation */ #include <stdio.h> #define MONTHS 12 int main(void) { int days[MONTHS] = {31,28,31,30,31,30,31,31,30,31,30,31}; int index; for (index = 0; index < MONTHS; index++) printf("Month %2d has %d days.n", index +1, *(days + index)); // same as days[index] return 0; }
  • 31. Functions, Arrays, and Pointers โ€ข Suppose you want to write a function that operates on an array. For example, suppose you want a function that returns the sum of the elements of an array. Suppose marbles is the name of an array of int. What would the function call look like? A reasonable guess would be this: total = sum(marbles); // possible function call โ€ข What would the prototype be? Remember, the name of an array is the address of its first element, int sum(int * ar); // corresponding prototype C Programming Language Moโ€™meN M. Ali
  • 32. C Programming Language Moโ€™meN M. Ali int sum(int * ar) // corresponding definition { int i; int total = 0; for( i = 0; i < 10; i++) // assume 10 elements total += ar[i]; // ar[i] the same as *(ar + i) return total; } int sum(int * ar, int n) // more general approach { int i; int total = 0; for( i = 0; i < n; i++) // use n elements total += ar[i]; // ar[i] the same as *(ar + i) return total; }
  • 33. Using Pointer Parameters โ€ข A function working on an array needs to know where to start and stop. โ€ข Another way to describe the array is by passing two pointers, with the first indicating where the array starts (as before) and the second where the array ends. โ€ข Now, the function can alter the value of the pointer itself, making it point to each array element in turn. C Programming Language Moโ€™meN M. Ali
  • 34. Arrays as Arguments โ€ข Because the name of an array is the address of the first element, an actual argument of an array name requires that the matching formal argument be a pointer. Also, C interprets int ar[] to mean the same as int * ar. โ€ข All four of the following prototypes are equivalent: int sum(int *ar, int n); int sum(int *, int); int sum(int ar[], int n); int sum(int [], int); Review Program: Array as arguments C Programming Language Moโ€™meN M. Ali
  • 35. Pointer Operations Review Program: PointerOperations โ€ข Assignmentโ€” You can assign an address to a pointer. The assigned value can be, for example, an array name, a variable preceded by address operator ( & ), or another second pointer. โ€ข Value finding (dereferencing)โ€” The * operator gives the value stored in the pointed-to location. Therefore, *ptr1 is initially 100 , the value stored at location 0x7fff5fbff8d0 . โ€ข Taking a pointer addressโ€” Like all variables, a pointer variable has an address and a value. The & operator tells you where the pointer itself is stored. โ€ข Adding an integer to a pointerโ€” You can use the + operator to add an integer to a pointer or a pointer to an integer. In either case, the integer is multiplied by the number of bytes in the pointed-to type, and the result is added to the original address. This makes ptr1 + 4 the same as &urn[4] . C Programming Language Moโ€™meN M. Ali
  • 36. โ€ข Incrementing a pointerโ€” Incrementing a pointer to an array element makes it move to the next element of the array. Therefore, ptr1++ increases the numerical value of ptr1 by 4 (4 bytes per int on our system) and makes ptr1 point to urn[1]. โ€ข Subtracting an integer from a pointerโ€” You can use the - operator to subtract an integer from a pointer; the pointer has to be the first operand and the integer value the second operand. The integer is multiplied by the number of bytes in the pointed-to type, and the result is subtracted from the original address. โ€ข Decrementing a pointerโ€” Of course, you can also decrement a pointer. In this example, decrementing ptr2 makes it point to the second array element instead of the third. Note that you can use both the prefix and postfix forms of the increment and decrement operators. โ€ข Differencingโ€” You can find the difference between two pointers. Normally, you do this for two pointers to elements that are in the same array to find out how far apart the elements are. The result is in the same units as the type size. โ€ข Comparisonsโ€” You can use the relational operators to compare the values of two pointers, provided the pointers are of the same type. C Programming Language Moโ€™meN M. Ali
  • 37. Using const with formal parameters โ€ข If a function is intent is that it not change the contents of the array, use the keyword const when declaring the formal parameter in the prototype and in the function definition. C Programming Language Moโ€™meN M. Ali
  • 38. Pointers to multidimensional Arrays โ€ข Since 2D-Arrays are Arrays of arrays, therefore we need a pointer-to-array instead of a pointer-to-element. int (* pz)[2]; // pz points to an array of 2 ints. (Can be used as 2D // Array pointer). int * pax[2]; // pax is an array of two pointers-to-int (Can no be used as 2D // Array Pointer). C Programming Language Moโ€™meN M. Ali
  • 39. Pointer Compatibility โ€ข The rules for assigning one pointer to another are tighter than the rules for numeric types. For example, you can assign an int value to a double variable without using a type conversion, but you canโ€™t do the same for pointers to these two types: int n= 5; double x; int * p1= &n; double * pd= &x; x= n; // implicit type conversion pd= p1; // compile-time error C Programming Language Moโ€™meN M. Ali
  • 40. int * pt; int (*pa)[3]; int ar1[2][3]; int ar2[3][2]; int **p2; // a pointer to a pointer pt = &ar1[0][0]; // both pointer-to-int pt = ar1[0]; // both pointer-to-int pt = ar1; // not valid pa = ar1; // both pointer-to-int[3] pa = ar2; // not valid p2 = &pt; // both pointer-to-int * *p2 = ar2[0]; // both pointer-to-int p2 = ar2; // not valid Review Program: Pointers and 2D-Arrays C Programming Language Moโ€™meN M. Ali
  • 41. Functions and multidimensional arrays โ€ข If you want to write functions that process two-dimensional arrays, you need to understand pointers well enough to make the proper declarations for function arguments. In the function body itself, you can usually get by with array notation. int junk[3][4] = { {2,4,5,8}, {3,5,6,9}, {12,10,8,6} }; int i, j; int total = 0; for (i = 0; i < 3 ; i++) total += sum(junk[i], 4); // junk[i] -- one-dimensional array C Programming Language Moโ€™meN M. Ali
  • 42. Exercise 1.1 30 Minutes MOโ€™MEN M. ALI C Programming Language
  • 43. Variable-length arrays (VLAs) โ€ข You may have noticed that you can not use a variable size multidimensional array as an argument, that is because you always have to use a constant columns. Well VLAs is the C99 way to solve this problem. You can use VLAs to pass a variable length multidimensional array. int sum2d(int rows, int cols, int ar[rows][cols]); // array a VLA int sum2d(int, int, int ar[*][*]); // array a VLA, names omitted โ€ข rows and cols are two variable arguments that can be passed at run-time. โ€ข rows and cols must be define before the array. int sum2d(int ar[rows][cols], int rows, int cols); // invalid order Review Program: Functions usingVLAs C Programming Language Moโ€™meN M. Ali
  • 44. More dimensions โ€ข Everything we have said about two-dimensional arrays can be generalized to three-dimensional arrays and further. You can declare a three- dimensional array this way: int box[10][20][30]; โ€ข You can visualize a one-dimensional array as a row of data, a two- dimensional array as a table of data, and a three-dimensional array as a stack of data tables. For example, you can visualize the box array as 10 two-dimensional arrays (each 20ร—30) stacked atop each other. C Programming Language Moโ€™meN M. Ali
  • 45. Compound Literals MOโ€™MEN M. ALI C Programming Language Search Yourself
  • 46. THANK YOUSEE YOU SOON C Programming Language Moโ€™meN M. Ali