Chapter 4-Computer Programming- Array ,String & Pointer
Chapter 4-Computer Programming- Array ,String & Pointer
Unit-4
Arrays, Strings and
Pointers
Admas Abtew
Faculty of Computing and
Informatics
Jimma Technology Institute, Jimma
[email protected]
+251912499102
Looping
Outline
Introduction to Arrays
Types of arrays
Array initialization
Strings
Library functions for strings
Pointers
Pointer Declaration
1. Introduction to Arrays
The null string, "", only contains the null terminator and represents
the empty string.
return(0);
}
#include <iostream.h>
#include <cstdio.h>
strlen(str) returns the length of
#include <cstring.h>
the string pointed to by str, i.e.,
int main()
the number of characters
{
excluding the null
char str[80];
terminator.
cout << “Enter a string: “;
gets(str); // let the input is: hello
// Length is: 5
cout << “Length is: “ << strlen(str);
return(0);
}
#include <iostream.h>
#include <cstdio.h> The strcat(s1,s2)
#include <cstring.h> function appends s2 to the
int main(){ end of s1. String s2 is
char s1[21], s2[11]; unchanged.
strcpy(s1, “Hello”);
strcpy(s2, “ there”); Displays:
int main()
{ Note: You may not get the same result on
int var1 = 3; your system.
int var2 = 24;
int var3 = 17; The 0x in the beginning represents the
address is in hexadecimal form.
cout << &var1 << endl;
Notice that first address differs from second
cout << &var2 << endl;
by 4-bytes and second address differs from
cout << &var3 << endl;
third by 4-bytes.
return 0;
} This is because the size of integer (variable of
type int) is 4 bytes in 64-bit system.
CP Unit 4-Arrays, Strings and Pointers 42
Pointer Definition
Pointers are used in C++ program to access the
memory and manipulate the address.
For type T, T* is the type “pointer to T”
For example:
int* pc2 = &c; // Correct! pc2 is address pointer, and &c is also address.
CP Unit 4-Arrays, Strings and Pointers 48
49