Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I recently started learning C++ and I'm a bit confused with pointers. Could you please explain me WHY in the following example variable "a" equals 1 and z = 0?????? I'm really confused!!!!!!
#include<iostream>
using namespace std;
void main()
{
int a;
int Z[3] ={1, 2, 3};
int *z;
z=Z;
a = (*z)--;
cout<<a<<" "<<*z<<"\n";
system ("pause");
}
logically ,I believe, first of all *z points to the 0-th element of the array - that is 1
then -- operator decreases 0-th element's value by 1 and now z[0] should be 0
but WHY it still returns 1 for "a" variable????
The order of your operations is this:
a = *z //*z = 1 here
*z = *z - 1 //*z = 0 here
Decrement operator happens after the assignment.
It is because the decrement operator is after the expression.
a = (*z)--;
Here first *z is evaluated and a is assigned the value (1). After that *z is decremented to zero.
If it had been
a = --(*z);
Then *z would have evaluated and decremented 1st. After that the value would have been assigned to a. Hence in this case both would be zero.
Post-decrement, thing--, yields the value before decrementing; so a is assigned the previous value of *z, which is 1.
Pre-decrement, --thing, yields the value after decrementing, so changing to a = --(*z); would set a to zero.
Related
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 2 years ago.
Improve this question
I just practice on c++ and want to get the area of circle from a given radius but the result giving me a memory address instead of value
#include <iostream>
#include<math.h>
using namespace std;
int main() {
double a, n, r;
n = 3.14159;
cin >> r;
a = pow(r,r) * n;
cout << "A=" << a<<endl;
}
while the input is 100.64 i got the output
A=1.13759e+202
but the result giving me a memory address instead of value
while the input is 100.64 i got the output
A=1.13759e+202
Your assumption is wrong. That is not a "memory address". That is the correct result of π × rʳ.
However, your calculation is not the correct one for area of a circle. Correct formula is π × r².
Bonus hint: r * r is typically better than calling std::pow.
Bonus hint 2: C++20 has constant std::numbers::pi in the <numbers> header. It provides you with the closes representable approximation of π.
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 4 years ago.
Improve this question
As documented here, std::bitset::operator^= returns *this. From that and from the "usual" interpretation of operators such as +=, |=, *= one could reasonably assume that given std::bitset instances (of the same size) a and b, the expression (a^=b).count() will store the result of a bitwise XOR operation in a, and that count() would return the number of bits in a that are set to true. However, as the following minimal example demonstrates, something unexpected happens:
#include <iostream>
#include <bitset>
int main()
{
constexpr unsigned int N=6;
std::bitset<N> a;
std::bitset<N> b;
a.flip();//111111
b[0]=1;
b[4]=1;//b is now 010001 (assuming least significan bit on the right end of the string)
std::cout<<"a=="<<a.to_string()<<std::endl;
std::cout<<"b=="<<b.to_string()<<std::endl;
std::cout<<"(a xor b) to string=="<<(a^=b).to_string()<<std::endl;
//Here is the unexpected part!
std::cout<<"(a xor b) count=="<<(a^=b).count()<<std::endl;
//Note that the following lines would produce the correct result
//a^=b;
//std::cout<<a.count()<<std::endl;
return 0;
}
The output is
a==111111
b==010001
(a xor b) to string==101110
(a xor b) count==6 //this is wrong!!!!! It should be 4...
A quick look at the implementation of std::bitset (see here) seems to indicate that the reference that is returned is indeed a reference to the lhs object (a in my example). So... Why is this happening?
This has nothing to do with the bitset. Consider this code:
int a = 2;
int b = 3;
std::cout << std::to_string(a *= b) << std::endl; // Prints 6.
std::cout << std::to_string(a *= b) << std::endl; // Prints 18.
You are using an assignment operator, so your variable/bitset changes every time. In your case, the second evaluation yields ((a ^ b) ^ b), which is of course the original a (which did have 6 bits set).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I am trying to use string::length() inside a function I wrote, named match(), but I'm getting garbage values.
Why does the marked line is outputting garbage values?
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
void match(string st1,string st2)
{
for(int i=0;i<st1.length();i++)
{
cout<<"Why this value is garbage "<<(i-st1.length()-1)<<"\t";
// this expression gives wrong values ^^^^^^^^^^^^^^^
}
}
int main()
{
string st1,st2;
cout<<"Enter the required string\n";
cin>>st1>>st2;
match(st1,st2);
return 0;
}
imagine a string "foo":
i-st1.length()-1 means:
when i is 0:
0 - 3 = -3
- 1 = -4
but st1.length() is a size_t, which is unsigned, so all terms in the expression are promoted to unsigned values.
(unsigned)0 - (unsigned)3 = 0xfffffffffffffffd
- 1 = 0xfffffffffffffffc
= 18446744073709551612
The problem is that i is an int value, while string::length will return you a size_t value. The first one is a signed value, while the second is unsigned. One way to prevent this is to cast your st1.length() as an int, so all the elements in your operation are signed values. You will then get the value you are looking for.
i-(int)st1.length()-1
This is not garbage, you are implicitly converting/promoting a signed type (i is signed int) to an 'unsigned' one (the return type of length() is size_t which is unsigned).
It happens implicitly because an unsigned type is more powerful than a signed one. This is a common source of bugs in the C/C++ world.
This is what you need to modify:
cout<<" **NOT** garbage "<<(i-(int)st1.length()-1)<<"\t"<<endl;
Happy programming!
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 6 years ago.
Improve this question
Debug why the below mentioned code goes into an infinite loop
int a[10];
int i;
for(i=0;i<=10;i++) {
a[i]=0;
}
i am not able to find any valid explanation for it.
Although one possibility is a[9] pointing back to i ... but it doesn't seem convincing
Classic side effect of "Buffer overflow". In this case what is happening is that the value of i is getting overwritten. Check the range of variable a it's an array of 10 bytes 0 through 9. However you loop for 11 bytes 0 through 10.
Change the loop as
int a[10];
int i;
for(i=0;i<10;i++) {
a[i]=0;
}
and it won't go in infinite loop. Again this is a problem of "buffer overflow" and can have undefined behavior. In your case that undefined behavior is infinite loop.
For the value of i equals to 10,
a[i]=0;
is off by one access. It invokes undefined behavior. Anything can happen.
This code has undefined behavior. But if it is going into infinite loop the most suitable explanation would be that a[10] = 0 is overwriting i with 0, since i is defined immediately after a, therefore, in memory is most probably placed as a contiguous element after array elements.
You could try this so that you don't get undefined behavior:
int i, a[10];
for(i = 0; i <= 9; i++)
a[i]=0;
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 8 years ago.
Improve this question
I have this error in Dev C. It says there is an error regarding pointers, but I'm not using pointers.
[Error] invalid conversion from 'int*' to 'int' [-fpermissive]
The error is in this line:
E=suma1 + distancias [x,y];
(Where suma1 and E are integers, and distancias is a matrix)
The expression x,y is actually a single value in C and C++. It evaluates both x and y but gives you the single value y. You can see this in action if you try:
#include <stdio.h>
int main (void) {
int a;
a = (1, 2, 3, 4, 5);
printf ("%d\n", a);
return 0;
}
which will output 5.
Hence what your current expression distancias [x,y] is being evaluated is is a simple distancias [y] (because evaluating x here has no side effects), which is why it's complaining about an int pointer being used where an int is expected.
The correct syntax for multi-dimensional arrays would be distancias [x][y].