Professional Documents
Culture Documents
Pointers and Pointer-Based Strings
Pointers and Pointer-Based Strings
Pointers and Pointer-Based Strings
Example:
int *ptr = &x;
int *ptr declares the pointer variable ptr to an integer value,
which we are initializing to the address of x (&x).
& and * operators (Example)
#include <iostream>
using namespace std;
int main() {
int a = 7; // initialize a with 7
int* aPtr = &a; // initialize aPtr with the address of int variable a
The value of a is 7
The value of *aPtr is 7
Output:
Pass-by-value:
The original value of number is 5
Value returned by cubeByValue: 125
The new value of number is 5
x = 5;
const int *const ptr = &x;
*ptr = 7; // error: *ptr is const; cannot assign new value
ptr = &y; // error: ptr is const; cannot assign new address
sizeof operators
• The unary operator sizeof determines the size of an array (or of
any other data type, variable or constant) in bytes during program
compilation.
• Size of fundamental data types:
Type Byte Type Byte Type Byte
char 1 long 4 double 8
int 4 long long 8 long double 12
short 2 float 4 bool 1
• Size of array:
• sizeof(arrayName) returns the total bytes of the array
• sizeof(arrayName[0]) returns the bytes of first element
cout << "The number of bytes in the array is " << sizeof(numbers);
Example 2:
cin >> setw(20) >> word;
Reads only up to 19 characters (space reserved for '\0')
Example 3:
cin.getline(array, size, delimiter);
The function stops reading characters when
1. the delimiter character is encountered (by default '\n' ),
2. when the end-of-file indicator is entered or
3. when the number of characters read so far is one less than the
length specified in the second argument.
Functions in <cstring> header
Function Explanations
size_t strlen (const char * cstr) Return the length of cstr, excluding
terminating null character '\0'.
// Copying
char * strcpy (char * dest, const Copy src into dest, return dest
char * src)
char * strncpy (char * dest, Copy at most n characters from src into
const char * src, size_t n) dest, return dest
// Comparison
int strcmp (const char * cstr1, Compare cstr1 and cstr2. Return 0 if
const char * cstr2)
cstr1 is equal to cstr2, less than zero
(usually -1) if cstr1 is less than cstr2,
more than zero (usually 1) if cstr1 is more
than cstr2.
int strncmp (const char * cstr1, Compare up to n characters.
const char * cstr2, size_t n)
// Concatenation Append src to dest, return src.
char * strcat (char * dest, const
char * src)
Functions in <cstring> header (Example –
1/2)
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char s1[20] = "Happy ";
char *s2 = "New Year ";
char s3[40] = "";
// Concatenating strings
strcat(s1, s2); // Append s2 to s1
cout << "\nAfter strcat(s1, s2): \ns1 = " << s1 << "\ns2 = " << s2;
strncat(s3, s1, 8); // Append first 8 characters of s1 to s3
cout << "\n\nAfter strncat(s3, s1, 8): \ns1 = " << s1 << "\ns3 = " <<
s3;
Functions in <cstring> header (Example –
2/2)
// Comparing strings
cout << "\n\nstrcmp(s1, s3) = " << strcmp(s1, s3);
cout << "\nstrncmp(s1, s3, 6) = " << strncmp(s1, s3, 6);
// Copying strings
Compare up to 6 characters of s1 and s3
cout << "\n\nBefore copying:\ns1 = " << s1 << "\ns3 = " << s3;
strcpy(s1, s3); // copy contents of s3 into s1
cout << "\nAfter copying: \ns1 = " << s1 << "\ns3 = " << s3;