Pointer Questions in C In this lab we are using the C langua
Pointer Questions in C++
In this lab, we are using the C++ language to demonstrate the concept of pointers. Even if you are not familiar with C++, it is OK since we are just looking at a few small programs.
This is a simple program to familiarize yourself (it’s from http://www.cplusplus.com/doc/tutorial/):
#include <iostream> using namespace std;
int main () { int i; cout << \"Please enter an integer value: \"; cin >> i; cout << \"The value you entered is \" << i; cout << \" and its double is \" << i*2 << \"\ \"; return 0; }
You can run C++ programs either on our server, or in Visual Studio .Net. On our server, if you create a file, such as test.cpp,
you can compile it with: g++ test.cpp
and run it with: ./a.out
In C++, for user input / output, we include the iostream header file. The keyword cin (stands for console in), is for getting user input from the keyboard. The keyword cout (stands for console out), is for displaying to the screen. You can see their usage in the above example.
(If you are interested in learning C++ for your own knowledge outside of this course, you can look at the tutorial mentioned above; however, it would be best to get a textbook in C++ for more complete coverage of the language. In our course, we are only looking at the concept of pointers, but there is much much more, including classes, etc, etc….)
Q1) What is the difference between a pointer and a regular variable? Describe the advantages and disadvantages to having pointers as a feature in a programming language.
Q2) Describe the * and & operators as they are used in the following program.
#include<iostream> using namespace std; int main( ) { int x, *p; x = 20; p = &x; cout<< \"X IS: \" << x << \" IT’S ADDRESS IS: \" << &x << endl; cout<< \"P IS: \" << p << \" IT’S CONTENT IS: \" << *p << endl; return 0; }//MAIN
Q3) Will a change made to the contents of the pointer p indirectly change x? What is the output?
#include<iostream> using namespace std; int main( ) { int x, *p; x = 20; p = &x; *p = *p+1; cout<< *p <<\" \"<< x <<endl; x = x-1; cout<< x <<\" \"<< *p <<endl; return 0; }//MAIN
Q4) What would be the output of the following program? Hint: The name of an array is an address to the first element of the array.
#include<iostream> using namespace std; int main( ) { int x[] = {1, 3, 5, 7}; int *p = x; p = p+3; cout<< *p <<endl; p--; cout<< *p <<endl; return 0; }//MAIN
Q5) In the code segment below, create a pointer variable called ptr that will point to the array nums. Re-write the statement inside the loop to use the pointer ptr to traverse the array of integers. We will need to use the pointer notation to access the array elements.
#include<iostream> using namespace std; int main( ) { int nums[] = {100, 90, 80, 70, 60, 50, 40, 30, 20, 10};
for(int i=0; i<10; i++) { cout<< nums[i] << \"\ \"; }
return 0; }//MAIN
Q6) What is happening in the following function? Describe how the function works and what it accomplishes (Note that character arrays end in a null character ‘\\0’, that will stop the loop).
void funct(char *s, char *t){ while( *s++ = *t++ ); }
(Extra credit) Write a program to generate a linked list of 100 nodes where each node stores an integer value: first node stores value 1, second node stores value 2, third node stores value 3, … (Use a for or while loop). Store the address of the first node in a pointer variable named “list”.
Use a struct node (self-referential structure) as follows:
struct node { int value; node * next; };
Or a class node:
class node { public: int value; node * next; };
Solution
1. Pointer stores address of variables
Advantages:
We can access a data from outside a function,
Memory allocation will be dynamic.
with this we can handle any datastructure.
It increases execution time of program.
Disadvantages:
It is Hard To Debug
It may leads to memory leaks
Pointer arithmetic
moreover syntax will be complex.
2. * operator means Pointers
& means address of the variable.
3. 4
20
19
4. 6
12
5.
100
90
80
70
60
50
40
30
20
10
6.compile time error.


