SlideShare a Scribd company logo
ARRAY, STRING AND
POINTER
Unit 3
ARRAY
An array is a sequential collection of variables
of same data type which can be accessed using
an integer as index, that generally starts from
0. It stores data elements in a continuous
memory location. Each element can be
individually referenced with its respective
index.
…
1-Dimensional array: It is a linear array that stores
elements in a sequential order. Let us try to
demonstrate this with an example: Let us say we have
to store integers 2, 3, 5, 4, 6, 7. We can store it in an
array of integer data type. The way to do it is:
Declaration:
dataType nameOfTheArray [sizeOfTheArray];
int Arr[6];
Here Arr is the name of array and 6 is the size. It is necessary to define
the size array at compile time.
…
To store the above elements in the array:
dataType nameOfTheArray [ ] = {elements of array };
int Arr [ ] = { 2, 3, 5, 4, 6, 7 };
Since, the indexing of
an array starts from 0,
if the 4th element
needs to be accessed,
Arr [ 3 ] will give you
the value of the 4th
element. Similarly, nth
element can be
accessed by Arr[n-1].
…
#include<stdio.h>
int main(void)
{
int a[5];
a[0]=9;
a[1]=10;
a[2]=14;
a[3]=76;
a[4]=34;
for(int i=0; i<5;
i++)
{
printf("%d",a[i]);
}
#include<stdio.h>
int main(void)
{
int
a[5]={9,4,22,18,17};
for(int i=0; i<5; i++)
{
printf("%d",a[i]);
}
}
…
#include<stdio.h>
int main(void)
{
int a[5];
printf("Enter the 5 values:
");
for(int i=0; i<5; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<5; i++)
{
printf("nThe Values are:
%d",a[i]);
}
WHY ARRAY STARTS FROM ‘0’
5000 + 0 (Index) * 2(Size of Datatype
INT)
= 5000
5000 + 1 (Index) * 2(Size of Datatype
INT)
= 5002
5000 + 2 (Index) * 2(Size of Datatype
INT)
= 5004
5000 + 0 (Index) * 4(Size of Datatype
Float)
= 5000
5000 + 1 (Index) * 4(Size of Datatype
Float)
= 5004
5000 + 2 (Index) * 4(Size of Datatype = 5008
For INT Data Type
For FLOAT Data Type
STRING
STRING
In C, string is stored in an array of characters.
Each character in a string occupies one location in an
array. The null character '0' is put after the last character.
This is done so that program can tell when the end of
the string has been reached.
char c[] = “any name";
When the compiler encounters a sequence of characters
enclosed in the double quotation marks, it appends a null
character 0 at the end by default.
DECLARATION
char s[5];
Here, we have declared a string of 5 characters.
Initialization
char c[] = "abcd";
char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '0'};
char c[5] = {'a', 'b', 'c', 'd', '0'};
EXAMPLE
#include <stdio.h>
void main ()
{
char a[7] = { ‘e’, ‘x’, ‘a’, ‘m’, ‘p’, ‘l’, ’e’ };
char b[10];
printf (“Enter the value for B”);
scanf("%s",b);
printf ("A Output: %sn", a);
printf ("B Output: %sn", b);
}
STRING FUNCTIONS
1. strlen("name of string")
2. strcpy( dest, source)
3. strcmp( string1, string2 )
4. strstr( str1, str2 )
STRING COPY AND LENGTH
#include <stdio.h>
#include <string.h>
Int main ()
{
char a[7] = { 'n', 'i', 's', 'h', 'a', 'n' };
char b[10]; int c; //scanf("%s",a);
printf ("A Output: %sn", a);
strcpy(b, a); c=strlen(b);
printf ("B Output: %sn", b);
printf ("B Output: %dn", c);
return 0;
}
STRCMP( STR1 , STR2 );
This function is used to compare two strings "str1"
and "str2". this function returns zero("0") if the two
compared strings are equal, else some none zero
integer.
Return value
- if Return value if < 0 then it indicates str1 is less
than str2
- if Return value if > 0 then it indicates str2 is less
than str1
- if Return value if = 0 then it indicates str1 is equal to
str2
#include <stdio.h>
#include <string.h>
void main ()
{
char a[10];
char b[10];
int c;
printf ("nEnter the value for a: ");
scanf("%s",a);
printf ("nEnter the value for b: ");
scanf("%s",b);
if(strcmp(b, a)==0)
{
printf ("Strings are equal");
}
else
{
printf ("Strings are not equal");
}
}
Output:
Enter the value of a:
example
Enter the value of b:
example
Strings are equal.
STRSTR()
#include<stdio.h>
#include<string.h>
int main ()
{
char str1[55] ="This is a test string for testing";
char str2[20]="test";
char *p;
p = strstr (str1, str2);
if(p)
{
printf("string foundn" );
}
else printf("string not foundn" );
return 0;
}
Output:
String found
POINTER
POINTERS
Pointers store address of variables or a memory
location.
datatype *var_name;
int *ptr;
// An example pointer "ptr" that holds address of an integer variable
or holds address of a memory whose value(s) can be accessed as
integer values through "ptr"
…
#include <stdio.h>
void main()
{
int x = 10;
int *ptr;
ptr = &x;
printf(“Value is : %d”,*ptr);
}
1) Since there is * in declaration, ptr
becomes a pointer varaible (a variable
that stores address of another variable)
2) Since there is int before *, ptr is
pointer to an integer type variable int
*ptr;& operator before x is used to get
address of x. The address of x is
assigned to ptr.
…
void main()
{
int *p;
int var = 10;
p= &var;
printf("Value of variable var is: %d", var);
printf("nValue of variable var is: %d",
*p);
printf("nAddress of variable var is: %d",
&var);
printf("nAddress of variable var is: %d",
p);
/* Pointer of integer type, this can hold
the * address of a integer type variable.
*/
/* Assigning the address of variable var to
the pointer * p. The p can hold the
address of var because var is * an integer
type variable. */
Output:
Value of variable var is: 10
Value of variable var is: 10
Address of variable var is: 0x7fff5ed98c4c
Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50
…
ASCII VALUES
1. The ASCII table has 128
characters, with values from
0 through 127.
2. Uppercase A has ASCII
value 65 in decimal .So
for Z ,the value is 90 in
decimal.
3. Lowercase a has ASCII
value 97 in decimal .So
for z ,the value is 122 in
decimal.
The following program gives the ASCII values :
#include <stdio.h>
int main()
{
int i;
for( i=0 ; i<=255 ; i++ ) /*ASCII values ranges
from 0-255*/
{
printf("ASCII value of character %d = %cn", i, i);
}
}

More Related Content

What's hot (20)

PPTX
Array and string
prashant chelani
 
DOCX
Type header file in c++ and its function
Frankie Jones
 
PPT
Array
Hajar
 
PPTX
Unit 6. Arrays
Ashim Lamichhane
 
PPT
strings
teach4uin
 
PPT
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
PPTX
C++ programming (Array)
طارق بالحارث
 
PPT
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
PPTX
Programming in c Arrays
janani thirupathi
 
PPT
Pointers+(2)
Rubal Bansal
 
PPTX
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
PDF
C Pointers
omukhtar
 
PPT
Strings
Mitali Chugh
 
PPTX
Python programming workshop
BAINIDA
 
PPTX
C++ lecture 04
HNDE Labuduwa Galle
 
PDF
Arrays
Shakila Mahjabin
 
PPTX
Strings
Saranya saran
 
PPTX
Arrays in c language
tanmaymodi4
 
PPT
Ponters
Mukund Trivedi
 
PPT
Arrays
Rahul Mahamuni
 
Array and string
prashant chelani
 
Type header file in c++ and its function
Frankie Jones
 
Array
Hajar
 
Unit 6. Arrays
Ashim Lamichhane
 
strings
teach4uin
 
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
C++ programming (Array)
طارق بالحارث
 
Lecture 18 - Pointers
Md. Imran Hossain Showrov
 
Programming in c Arrays
janani thirupathi
 
Pointers+(2)
Rubal Bansal
 
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
C Pointers
omukhtar
 
Strings
Mitali Chugh
 
Python programming workshop
BAINIDA
 
C++ lecture 04
HNDE Labuduwa Galle
 
Strings
Saranya saran
 
Arrays in c language
tanmaymodi4
 

Similar to Array, string and pointer (20)

PPTX
4. chapter iii
Chhom Karath
 
PDF
VIT351 Software Development VI Unit2
YOGESH SINGH
 
PDF
Unit 2
TPLatchoumi
 
DOCX
Unitii classnotes
Sowri Rajan
 
PPT
Lesson in Strings for C Programming Lessons
JamesChristianGadian
 
PPTX
Keypoints c strings
Akila Krishnamoorthy
 
PPT
string function with example...................
NishantsrivastavaV
 
DOCX
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
Sitamarhi Institute of Technology
 
PPS
C programming session 05
Dushmanta Nath
 
PPTX
data types in C programming
Harshita Yadav
 
PDF
cassignmentii-170424105623.pdf
YRABHI
 
PPTX
Pointers in c - Mohammad Salman
MohammadSalman129
 
PPTX
C-Arrays & Strings (computer programming).pptx
yusuph2410
 
PDF
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
PPTX
ARRAY's in C Programming Language PPTX.
MSridhar18
 
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
PDF
Slide set 6 Strings and pointers.pdf
HimanshuKansal22
 
PPTX
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
AntareepMajumder
 
PPTX
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
PPTX
Ponters
Anil Dutt
 
4. chapter iii
Chhom Karath
 
VIT351 Software Development VI Unit2
YOGESH SINGH
 
Unit 2
TPLatchoumi
 
Unitii classnotes
Sowri Rajan
 
Lesson in Strings for C Programming Lessons
JamesChristianGadian
 
Keypoints c strings
Akila Krishnamoorthy
 
string function with example...................
NishantsrivastavaV
 
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
Sitamarhi Institute of Technology
 
C programming session 05
Dushmanta Nath
 
data types in C programming
Harshita Yadav
 
cassignmentii-170424105623.pdf
YRABHI
 
Pointers in c - Mohammad Salman
MohammadSalman129
 
C-Arrays & Strings (computer programming).pptx
yusuph2410
 
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
 
ARRAY's in C Programming Language PPTX.
MSridhar18
 
UNIT 4 POINTERS.pptx pointers pptx for basic c language
wwwskrilikeyou
 
Slide set 6 Strings and pointers.pdf
HimanshuKansal22
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
AntareepMajumder
 
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
Ponters
Anil Dutt
 
Ad

More from Nishant Munjal (20)

PPTX
Database Management System
Nishant Munjal
 
PPTX
Functions & Recursion
Nishant Munjal
 
PPTX
Programming in C
Nishant Munjal
 
PPTX
Introduction to computers
Nishant Munjal
 
PPTX
Unix Administration
Nishant Munjal
 
PPTX
Shell Programming Concept
Nishant Munjal
 
PPTX
VI Editor
Nishant Munjal
 
PPTX
Introduction to Unix
Nishant Munjal
 
PPTX
Routing Techniques
Nishant Munjal
 
PPTX
Asynchronous Transfer Mode
Nishant Munjal
 
PPTX
Overview of Cloud Computing
Nishant Munjal
 
PPTX
SQL Queries Information
Nishant Munjal
 
PPTX
Database Design and Normalization Techniques
Nishant Munjal
 
PPTX
Concurrency Control
Nishant Munjal
 
PPTX
Transaction Processing Concept
Nishant Munjal
 
PPTX
Database Management System
Nishant Munjal
 
PPTX
Relational Data Model Introduction
Nishant Munjal
 
PPTX
Virtualization, A Concept Implementation of Cloud
Nishant Munjal
 
PPTX
Technical education benchmarks
Nishant Munjal
 
PPSX
Bluemix Introduction
Nishant Munjal
 
Database Management System
Nishant Munjal
 
Functions & Recursion
Nishant Munjal
 
Programming in C
Nishant Munjal
 
Introduction to computers
Nishant Munjal
 
Unix Administration
Nishant Munjal
 
Shell Programming Concept
Nishant Munjal
 
VI Editor
Nishant Munjal
 
Introduction to Unix
Nishant Munjal
 
Routing Techniques
Nishant Munjal
 
Asynchronous Transfer Mode
Nishant Munjal
 
Overview of Cloud Computing
Nishant Munjal
 
SQL Queries Information
Nishant Munjal
 
Database Design and Normalization Techniques
Nishant Munjal
 
Concurrency Control
Nishant Munjal
 
Transaction Processing Concept
Nishant Munjal
 
Database Management System
Nishant Munjal
 
Relational Data Model Introduction
Nishant Munjal
 
Virtualization, A Concept Implementation of Cloud
Nishant Munjal
 
Technical education benchmarks
Nishant Munjal
 
Bluemix Introduction
Nishant Munjal
 
Ad

Recently uploaded (20)

PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
Design Thinking basics for Engineers.pdf
CMR University
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
20ES1152 Programming for Problem Solving Lab Manual VRSEC.pdf
Ashutosh Satapathy
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 

Array, string and pointer

  • 2. ARRAY An array is a sequential collection of variables of same data type which can be accessed using an integer as index, that generally starts from 0. It stores data elements in a continuous memory location. Each element can be individually referenced with its respective index.
  • 3. … 1-Dimensional array: It is a linear array that stores elements in a sequential order. Let us try to demonstrate this with an example: Let us say we have to store integers 2, 3, 5, 4, 6, 7. We can store it in an array of integer data type. The way to do it is: Declaration: dataType nameOfTheArray [sizeOfTheArray]; int Arr[6]; Here Arr is the name of array and 6 is the size. It is necessary to define the size array at compile time.
  • 4. … To store the above elements in the array: dataType nameOfTheArray [ ] = {elements of array }; int Arr [ ] = { 2, 3, 5, 4, 6, 7 }; Since, the indexing of an array starts from 0, if the 4th element needs to be accessed, Arr [ 3 ] will give you the value of the 4th element. Similarly, nth element can be accessed by Arr[n-1].
  • 5. … #include<stdio.h> int main(void) { int a[5]; a[0]=9; a[1]=10; a[2]=14; a[3]=76; a[4]=34; for(int i=0; i<5; i++) { printf("%d",a[i]); } #include<stdio.h> int main(void) { int a[5]={9,4,22,18,17}; for(int i=0; i<5; i++) { printf("%d",a[i]); } }
  • 6. … #include<stdio.h> int main(void) { int a[5]; printf("Enter the 5 values: "); for(int i=0; i<5; i++) { scanf("%d",&a[i]); } for(i=0; i<5; i++) { printf("nThe Values are: %d",a[i]); }
  • 7. WHY ARRAY STARTS FROM ‘0’ 5000 + 0 (Index) * 2(Size of Datatype INT) = 5000 5000 + 1 (Index) * 2(Size of Datatype INT) = 5002 5000 + 2 (Index) * 2(Size of Datatype INT) = 5004 5000 + 0 (Index) * 4(Size of Datatype Float) = 5000 5000 + 1 (Index) * 4(Size of Datatype Float) = 5004 5000 + 2 (Index) * 4(Size of Datatype = 5008 For INT Data Type For FLOAT Data Type
  • 9. STRING In C, string is stored in an array of characters. Each character in a string occupies one location in an array. The null character '0' is put after the last character. This is done so that program can tell when the end of the string has been reached. char c[] = “any name"; When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character 0 at the end by default.
  • 10. DECLARATION char s[5]; Here, we have declared a string of 5 characters. Initialization char c[] = "abcd"; char c[50] = "abcd"; char c[] = {'a', 'b', 'c', 'd', '0'}; char c[5] = {'a', 'b', 'c', 'd', '0'};
  • 11. EXAMPLE #include <stdio.h> void main () { char a[7] = { ‘e’, ‘x’, ‘a’, ‘m’, ‘p’, ‘l’, ’e’ }; char b[10]; printf (“Enter the value for B”); scanf("%s",b); printf ("A Output: %sn", a); printf ("B Output: %sn", b); }
  • 12. STRING FUNCTIONS 1. strlen("name of string") 2. strcpy( dest, source) 3. strcmp( string1, string2 ) 4. strstr( str1, str2 )
  • 13. STRING COPY AND LENGTH #include <stdio.h> #include <string.h> Int main () { char a[7] = { 'n', 'i', 's', 'h', 'a', 'n' }; char b[10]; int c; //scanf("%s",a); printf ("A Output: %sn", a); strcpy(b, a); c=strlen(b); printf ("B Output: %sn", b); printf ("B Output: %dn", c); return 0; }
  • 14. STRCMP( STR1 , STR2 ); This function is used to compare two strings "str1" and "str2". this function returns zero("0") if the two compared strings are equal, else some none zero integer. Return value - if Return value if < 0 then it indicates str1 is less than str2 - if Return value if > 0 then it indicates str2 is less than str1 - if Return value if = 0 then it indicates str1 is equal to str2
  • 15. #include <stdio.h> #include <string.h> void main () { char a[10]; char b[10]; int c; printf ("nEnter the value for a: "); scanf("%s",a); printf ("nEnter the value for b: "); scanf("%s",b); if(strcmp(b, a)==0) { printf ("Strings are equal"); } else { printf ("Strings are not equal"); } } Output: Enter the value of a: example Enter the value of b: example Strings are equal.
  • 16. STRSTR() #include<stdio.h> #include<string.h> int main () { char str1[55] ="This is a test string for testing"; char str2[20]="test"; char *p; p = strstr (str1, str2); if(p) { printf("string foundn" ); } else printf("string not foundn" ); return 0; } Output: String found
  • 18. POINTERS Pointers store address of variables or a memory location. datatype *var_name; int *ptr; // An example pointer "ptr" that holds address of an integer variable or holds address of a memory whose value(s) can be accessed as integer values through "ptr"
  • 19. … #include <stdio.h> void main() { int x = 10; int *ptr; ptr = &x; printf(“Value is : %d”,*ptr); } 1) Since there is * in declaration, ptr becomes a pointer varaible (a variable that stores address of another variable) 2) Since there is int before *, ptr is pointer to an integer type variable int *ptr;& operator before x is used to get address of x. The address of x is assigned to ptr.
  • 20. … void main() { int *p; int var = 10; p= &var; printf("Value of variable var is: %d", var); printf("nValue of variable var is: %d", *p); printf("nAddress of variable var is: %d", &var); printf("nAddress of variable var is: %d", p); /* Pointer of integer type, this can hold the * address of a integer type variable. */ /* Assigning the address of variable var to the pointer * p. The p can hold the address of var because var is * an integer type variable. */ Output: Value of variable var is: 10 Value of variable var is: 10 Address of variable var is: 0x7fff5ed98c4c Address of variable var is: 0x7fff5ed98c4c Address of pointer p is: 0x7fff5ed98c50
  • 21.
  • 22. ASCII VALUES 1. The ASCII table has 128 characters, with values from 0 through 127. 2. Uppercase A has ASCII value 65 in decimal .So for Z ,the value is 90 in decimal. 3. Lowercase a has ASCII value 97 in decimal .So for z ,the value is 122 in decimal. The following program gives the ASCII values : #include <stdio.h> int main() { int i; for( i=0 ; i<=255 ; i++ ) /*ASCII values ranges from 0-255*/ { printf("ASCII value of character %d = %cn", i, i); } }