CCS0007 - Laboratory Exercise 5
CCS0007 - Laboratory Exercise 5
CCS007L
(COMPUTER PROGRAMMING 2)
EXERCISE
5
POINTERS
Section: Tx05
Type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name
of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that you
use for multiplication. However, in this statement the asterisk is being used to designate a
variable as a pointer. Following are the valid pointer declaration:
The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory address. The
only difference between pointers of different data types is the data type of the variable or
constant that the pointer points to.
Simply, a pointer is a variable that stores the memory address as its value.
A pointer variable points to a data type of the same type, and is created with
the * operator. The address of the variable you're working with is assigned to the pointer:
Create a pointer variable with the name ptr, that points to a string variable, by using the asterisk
sign * (string* ptr). Note that the type of the pointer has to match the type of the variable you're
working with. Use the & operator to store the memory address of the variable called food, and assign it
to the pointer. Now, ptr holds the value of food's memory address.
Complete the codes for the stringCat function. It will use the same function which is
strcat function.
#include <iostream>
using namespace std;
int main(){
char str1[20]= " Happy ";
char str2[20]= " Man";
stringCat(str1,str2);
cout << str1;
system("pause > 0");
return 0;
ddddddsadsa
sdasdasda
int main(){
char str[]="Happy Day";
cout << stringRev(str);
system("pause > 0");
return 0;
}
char* stringRev(char *s){
char* tmp;
tmp = new char;
int i, cnt(0);
for (i=0; s[i]!='/';i++)
{
cout << s[i];
cnt++;
}
for (i=0; i<cnt; i++)
{
tmp[i] = s[cnt - i - 1];
}
tmp[i] = '\0';
return tmp;
}
VII. AND ANSWER
VIII. REFERENCES
Zak, Dianne (2016). An Introduction to Programming with C++
Deitel, Paul & Deitel, Harvey (2012). C++ How To Program, Eighth Edition
https://www.cprogramming.com/tutorial/lesson6.html