I am trying to debug this. #include <iostream> se it is an array of characters t
ID: 3836625 • Letter: I
Question
I am trying to debug this.
#include <iostream>
se it is an array of characters terminated with a , it is seen as a string.
*/
#include <cstdlib> //for NULL
using namespace std;
//#define _DEBUGPRINT
#ifdef _DEBUGPRINT
#define _DPRINT(explain, x) cout << explain << x << endl;
#else
#define _DPRINT(explain, x)
#endif
int main()
{
//declarations
int value = 0;
int *valuePtr = NULL;
char array[] = {'h','e','l','l','o', ''};
char *arrayPtr = NULL;
valuePtr = &value;
_DPRINT("&value = ", &value);
_DPRINT("valuePtr = ", valuePtr);
//demonstrating values vs addresses
value = 10;
_DPRINT("value = ", value);
_DPRINT("*valuePtr = ", *valuePtr);
arrayPtr = array;
_DPRINT ("array[0] = ", array[0]);
_DPRINT ("array = ",array);
_DPRINT ("arrayPtr = ", arrayPtr);
_DPRINT ("*arrayPtr = ", *arrayPtr);
//printing out the entire array
cout << "Print as array ";
cout << array << endl;
/*Normally you can not print out an array, but becau
int i =0;
while (array[i] != '')
cout << array[i++];
cout << endl;
cout << "Printing with arrayPtr ";
while (*arrayPtr != '')
cout << *arrayPtr++;
cout <<endl;
}
Explanation / Answer
Ans)
In your code only the below code executes and following is the output of it. Note that character arrays can be printed just by using their name or individualy accessing elements out from them because they have '' terminating character in the end of string. You can also access the character array using pointers provided the address of array is stored in a pointer. Here in this case we did that as arrayptr = array. This is same as arrayptr = &array or arrayptr = &arrayptr[0]
//printing out the entire array
cout << "Print as array ";
cout << array << endl;
// You can print out character array either by just name or by fetching individual elements of the array
int i =0;
while (array[i] != '')
cout << array[i++];
cout << endl;
cout << "Printing with arrayPtr ";
// As arrayptr is assigned the address of array. i.e. arrayptr = array in the beginning of the program. *arrayptr can now
// refer to the individual elements of the array
while (*arrayPtr != '')
cout << *arrayPtr++;
cout <<endl;
Normal Output:
Print as array
hello
hello
Printing with arrayPtr
hello
If you want to print debugged output where you were trying to do in your code as "_DPRINT" then you need to pass the _DEBUGPRINT to -D option to your compiler as shown below. Your compiler name might not be same so please change it accordingly.
Debugged output:
Compiling the code and passing _DEBUGPRINT option using -D :
sh-4.2$ g++ -D_DEBUGPRINT -o main *.cpp
Executing the script:
sh-4.2$ main
&value = 0x7fff000ae974
valuePtr = 0x7fff000ae974
value = 10
*valuePtr = 10
array[0] = h
array = hello
arrayPtr = hello
*arrayPtr = h
Print as array
hello
hello
Printing with arrayPtr
hello
Hope i clarified your queries. If not, please revert back with clear questions so i can help you understand the same.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.