CCS0007 - Laboratory Exercise 5
CCS0007 - Laboratory Exercise 5
CCS007L
(COMPUTER PROGRAMMING 2)
EXERCISE
5
POINTERS
Section:
Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
Design, implement and evaluate computer-based systems or applications to meet
desired needs and requirements. [PO: C]
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;
void stringCat(char *s1, char *s2);
int main()
{
char str1[20]="The Happy";
char str2[20]=" Man ";
stringCat(str1, str2);
cout<<str1;
system("pause > 0");
return 0;
}
void stringCat(char *s1, char * s2)
{
while(+(++s1));
while(*(s1++)= *(s2++));
}
CCS007L-Computer Programming 2 Page 6 of 7
ACTIVITY 5.2: Reverse string
Create a program that will return a reverse string using pointer.
Briefly answer the questions below. Avoid erasures. For group activity, specify the name of
GROUP MEMBER/s who answered the question. Do not forget to include the source for all
NON-ORIGINAL IDEAS.