What does this print to the screen?
#include <iostream>
using namespace std;
int main()
{
int c = 5;
int *p = &c;
*p *= c;
cout << *(++p);
return 0;
}
I get 0 in Xcode, but it looks to me like it should be giving me the memory location. Please step me through the process.
Below is the answer with reference to image attached
Line 7: int c = 5;
we initialize variable c with value 5
Line 8 : int* p= &c;
p is an integer pointer it points to the address of variable of
c
Line 9 : *p *= c;
This line actually means *p = *p * c which means multiplying
value at address p with and storing it at address p
i.e storing (5*5 = 25 at p)
Line 10 : cout << *(++p);
since priority of () is greater than *, therefore ++p will take
us to next address after p and *(++p) means printing the
value
at ++p, which has currently no value so it will print a garbage
value, which can be different everytime you run the code
Ouput for different runs
Thank you, please up vote.
What does this print to the screen? #include <iostream> using namespace std; int main() { int...
What does this print to the screen? #include <iostream> using namespace std; int main () { int *a; a = new int[2]; for(int i = 0; i < 2; i++) *(a + i) = i; for(int i = 0; i < 2; i++) cout << a[i] << " "; delete [] a; return 0; } I'm getting 0 1 Please step me through the process.
One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << ; cout << endl; return 0; }
What is the difference between these two programs? #include <iostream> using namespace std; int DontPanic(int & x); int z = 10; void main() { char x = 'y'; int y = 5; int z = 100; y = DontPanic(z); cout << x << " " << y << " " << z << endl; } int DontPanic(int & x) { int * p; p = & z; x = (*p)++ + 1; cout << x << " " << *p...
what is the output for the following code? explain the steps. /*#include <iostream> using namespace std; int f(int &i) { i = 10; return(5 * i); } int main() { int n = 5; f(n); cout << n << "\n"; return 0; } #include <iostream> using namespace std; int sub1(int n) { n--; return n; } int main() { int m = 10; for(int j = 0; j < 10; j++) m -= sub1(j); cout << m << "\n"; return...
//countingAnimals.cpp C++ #include <iostream> #include "Animal.h" using namespace std; int Animal::count = 0; int main(int argc, const char * argv[]) { Animal myAnimal; Animal anotherAnimal; cout << Animal::count << endl; return 0; }
Write the missing statements for the following program. #include <iostream> using namespace std; int main(void) { int Num1; cout << "Enter 2 numbers: "; cin >> Num2; if (Num1 < Num2) cout << "Smallest number is " << Num1; else cout << "Smallest number is " << Num2; return 0; }
1 #include<iostream> 2 using namespace std; int main() int number; 7 cout < "Enter a number to p cin number; cout << "start:" rint the square numbers in reverse order from numb 10 ut << 1. N was: number return e; 12 13
in c++ #include <iostream> using namespace std; int main() int beta[7] = 3, 5); for (int i = 2; i < 7; i++) beta[i] = 3 * i + 2; beta[i - 1] = beta[i - 1] + beta[i]; beta[i - 2) = betali - 2] + beta (i - 1]; for (int i = 0; i < 7; i++) cout << beta[i] << " "; cout << endl; return 0;
Write following program using Switch statement. #include <iostream> using namespace std; int main() int number; cout << "Enter an integer cin >> number; if (number > B) cout << You entered a positive integer: " << number << endl; else if (number (8) cout<<"You entered a negative integer: " << number << endl; cout << "You entered e." << endl; cout << "This line is always printed." return 0;
Compute the average lids per family. #include <iostream> using namespace std; int main() {int numKids a = 1; int numKids B = 4; int numKids C = 5; int numFamilies = 3; double avgKids = 0.0; ((your answer))] cout << "Average kids per family: " << avgKids << end1; return 0;}