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

PDF
Arrays-Computer programming
PDF
Control statements-Computer programming
PPTX
function, storage class and array and strings
PPT
Functions and pointers_unit_4
PPT
PPTX
C Programming Unit-3
PPTX
One dimensional arrays
DOC
Arrays and Strings
Arrays-Computer programming
Control statements-Computer programming
function, storage class and array and strings
Functions and pointers_unit_4
C Programming Unit-3
One dimensional arrays
Arrays and Strings

What's hot (20)

PPTX
Array and string
DOCX
Type header file in c++ and its function
PPT
Array
PPTX
Unit 6. Arrays
PPT
strings
PPT
Lecture 17 - Strings
PPTX
C++ programming (Array)
PPT
Lecture 18 - Pointers
PPTX
Programming in c Arrays
PPT
Pointers+(2)
PPTX
Arrays 1D and 2D , and multi dimensional
PDF
C Pointers
PPT
Strings
PPTX
Python programming workshop
PPTX
C++ lecture 04
PDF
PPTX
Strings
PPTX
Arrays in c language
PPT
PPT
Array and string
Type header file in c++ and its function
Array
Unit 6. Arrays
strings
Lecture 17 - Strings
C++ programming (Array)
Lecture 18 - Pointers
Programming in c Arrays
Pointers+(2)
Arrays 1D and 2D , and multi dimensional
C Pointers
Strings
Python programming workshop
C++ lecture 04
Strings
Arrays in c language
Ad

Similar to Array, string and pointer (20)

PPTX
Algoritmos e Estruturas de Dados - Pointers
PPT
Unit3 C
PPTX
Pointers
PPTX
Arrays
PDF
C pointers and references
PPT
Array THE DATA STRUCTURE. ITS THE STRUCT
DOCX
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
PPTX
3.ArraysandPointers.pptx
DOC
PPTX
Ponters
PPT
string function with example...................
PPTX
Module 4- Arrays and Strings
DOCX
Array assignment
PDF
VIT351 Software Development VI Unit2
PPT
Pointers and arrays
PPT
Lesson in Strings for C Programming Lessons
PDF
Array&amp;string
PPTX
Pointers and Dynamic Memory Allocation
PPTX
Pointer in C
PPTX
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Algoritmos e Estruturas de Dados - Pointers
Unit3 C
Pointers
Arrays
C pointers and references
Array THE DATA STRUCTURE. ITS THE STRUCT
PPS 4.4ARRAYS ARRAY DECLARATION & INITIALIZATION, BOUND CHECKING ARRAYS (1-D...
3.ArraysandPointers.pptx
Ponters
string function with example...................
Module 4- Arrays and Strings
Array assignment
VIT351 Software Development VI Unit2
Pointers and arrays
Lesson in Strings for C Programming Lessons
Array&amp;string
Pointers and Dynamic Memory Allocation
Pointer in C
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Ad

More from Nishant Munjal (20)

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

Recently uploaded (20)

PPTX
meets orient on the new industry intereacting skills .pptx
PPTX
Ship’s Structural Components.pptx 7.7 Mb
PDF
ETO & MEO Certificate of Competency Questions and Answers
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
International Journal of Information Technology Convergence and Services (IJI...
PDF
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
PPTX
Glazing at Facade, functions, types of glazing
PDF
Chad Ayach - A Versatile Aerospace Professional
PDF
6th International Conference on Artificial Intelligence and Machine Learning ...
PPT
High Data Link Control Protocol in Data Link Layer
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
Queuing formulas to evaluate throughputs and servers
PPTX
AgentX UiPath Community Webinar series - Delhi
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PPTX
Internship_Presentation_Final engineering.pptx
meets orient on the new industry intereacting skills .pptx
Ship’s Structural Components.pptx 7.7 Mb
ETO & MEO Certificate of Competency Questions and Answers
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
Strings in CPP - Strings in C++ are sequences of characters used to store and...
International Journal of Information Technology Convergence and Services (IJI...
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
Glazing at Facade, functions, types of glazing
Chad Ayach - A Versatile Aerospace Professional
6th International Conference on Artificial Intelligence and Machine Learning ...
High Data Link Control Protocol in Data Link Layer
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Simulation of electric circuit laws using tinkercad.pptx
Lesson 3_Tessellation.pptx finite Mathematics
Queuing formulas to evaluate throughputs and servers
AgentX UiPath Community Webinar series - Delhi
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
Internship_Presentation_Final engineering.pptx

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); } }