Result should be 11 after incrementing, it is 12, why? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
using namespace std;
int main()
{
int n, *p1, *p2;
n = 10;
p1 = &n;
p2 = p1;
(*p1)++;
(*p2)++;
cout << *p1 << " " << *p2 << " "<< n << endl ;
return 0;
}

*p1 refers to the value pointed by the pointer p1. (*p1)++ will increment the value of n by 1 and (*p2)++ will again do the increment on n, since it is pointing to the same location of p1. So the n will be incremented to 12. *p1,*p2 and n will thus have 12. So it prints 12.

Related

Why are the two output results different? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
int* p= NULL;
int** pp = &p;
cout << &p << endl;
cout <<*pp << endl;
I think the output results of the two should be consistent, but the first output is the address of p, and the second is 00000000. I want to know why.
You are mistaken, pp is the address of p
int** pp = &p; // set pp to the address of p
But *pp is the value of whatever pp is pointing at. In this case that is p which has a value of NULL.
The code is essentially no different to this
int x = 123;
int* px = &x;
cout << &x << endl; // print address of x
cout << *px << endl; // print value of x i.e. 123
The only difference being the type of the original value (int vs int*).
pp holds the address of p, so print pp, not *pp.

Queries on vectors in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
int main() { vector g1; vector :: iterator i; vector :: reverse_iterator ir;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end\t:\t";
for (i = g1.begin(); i != g1.end(); ++i)
cout << *i << '\t';
cout << endl << endl;
cout << "Output of rbegin and rend\t:\t";
for (ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << '\t' << *ir;
return 0;
}
Here in this code variable "i" has been declared as a iterator as well as a variable inside a for loop. isn't that a error?
If we see the first for loop it say that the loop will run till i!=g1.end() that means that the value of *(g1.end()) should not be displayed by *i but it is giving. ide shows output 1 2 3 4 5 for me it should be 1 2 3 4.
i is defined as an iterator in the argument list. When you redefine it in the first for loop, this is a new definition only for the scope of the loop -- this is perfectly legal, though not good practice.
vector::end() points to memory after the final item, not to the final item. So, yes, the final item in the vector will be printed.

How do you print an Nth character from a string in an array c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am very new at coding and I just had a quick question.
How would you find the nth character from a string in an array for c++?
so for example
{"Hey you" "echo" "lake" "lend" "degree"}, n=0
would equal "hello"
thanks! I'm not going to ask for the full code but just tips on where to get started.
Here are some examples:
unsigned int n = 3;
static const char hello[] = "hello";
cout << hello[n] << "\n";
const std::string apple = "apple";
cout << apple[n] << "\n";
static const char * many[] = {"These", "are", "many", "strings"};
cout << many[n][n] << "\n";
cout << many[n] << "\n";

fill missing places in c++ program to get desired output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Fill up the blanks with appropriate keyword to get the desired output according to the test cases. (Language use c++)
Sample Test Cases
input output
Test Case 1 4 square = 16, ++ square = 25
test case 2 -8 square =64, ++ square = 49
#include <iostream>
using namespace std;
______ int SQUARE(int x) { ______ x * x; }
​
int main() {
int a , b, c;
cin >> a ;
b = SQUARE(a);
cout << "Square = " << b << ", ";
c = SQUARE(++a);
cout << "++ Square = " << c ;
return 0;
}
Second blank place is "return"
First may be "inline" or nothing

I am new to c++ why is casting working in first case but not in second cout after masking one of the characters in char* [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
why is casting working in the first case but not in second cout after masking one of the characters in char*
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
int n = 10;
char* ch = (char*) calloc(6, sizeof(*ch));
ch = strdup("ab");
cout << strlen(ch) << endl;
int* p = (int*) ch;
cout << (char*)p << endl;// works fine it prints "ab"
*p = *p & 65280;
cout << "cast not working\t" << (char*)p << endl; // it does not work here
free(ch);
return 0;
}
Written as hex, 65280 is 0x0000FF00. So, on common systems with int being 4 bytes, you have set ch[0] to be 0. This is a null terminator, so when you try to print the string, you see an empty string.
Note: writing to *p causes undefined behaviour by writing past the end of the allocated area too; strdup("ab") allocates 3 bytes. On common systems this probably will have no ill effect as heap allocations are done in chunks of a certain size.