This question already has answers here:
Pointer addition vs subtraction
(8 answers)
Pointer subtraction confusion
(8 answers)
Closed 7 months ago.
#include<bits/stdc++.h>
using namespace std;
int main(){
float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5};
float *ptr1 = &arr[0];
float *ptr2 = ptr1 + 3;
cout<<*ptr2<<" ";
cout<<ptr2 - ptr1;
return 0;
}
Correct output of this code is -> 90.5 3
But I am unable to get the concept behind it. please if anybody can explain how we got this output.
Related
This question already has answers here:
Structure padding and packing
(11 answers)
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 18 days ago.
# include <stdio.h>
# include <iostream>
using namespace std;
struct complex{
int real;
int img;
char x[5];
int add(struct complex c2){
real += c2.real;
img += c2.img;
}
void display(){
printf("%d + %d i",real,img);
}
};
int main(){
struct complex c1={10,15
};
struct complex c2 = {12,15
};
cout<<sizeof(c1)<<endl;
c1.add(c2);
c1.display();
}
I am getting 16 as its size in sizeof();how does it work ,Can you guys please help me?,I am getting 16 as its size for character array from size 5 to 8,from 9 it is 20.
This question already has answers here:
How can I declare and define multiple variables in one line using C++?
(10 answers)
Value assignment in C while declaring multiple variable in 1 line
(1 answer)
What happens in C when you print a declared, but unassigned variable?
(2 answers)
Why does the C standard leave use of indeterminate variables undefined?
(5 answers)
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 1 year ago.
I am new to C/C++ and I noticed my program calculating wrong values. I found the problem to be my understanding of how the declaration of variables works in C/C++.
x,y,z = 1,2,3 works fine in Python where as x,y,z = 1 does not.
int x,y,z = 1,2,3 doesnt not work in C/C++ but int x,y,z = 1 does, kind of, since cout << x; outputs 16 and I expected it to be 1.
#include <iostream>
using namespace std;
int main() {
int x, y, z = 1;
cout << x;
return 0;
}
// The output is 16
What is the operation int x,y,z = 1 doing, if not assigning 1 to each?
It is shorthand for:
int x;
int y;
int z = 1;
x and y are left unassigned.
int x,y,z = 1 initializes ONLY z to 1, x and y are left uninitialized.
This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
why reference size is always 4 bytes - c++
(2 answers)
Closed 2 years ago.
This is printing 16 and 4 as the answer but it should be printing as 8 and 4 because both a and b are integer type variables. So can we think this as the compiler stores the address of the variable a in a separate variable while copying to b that is why it is resulting in 4 + 4 + 8 = 16? If not then what is it?
#include <iostream>
using namespace std;
class C {
public:
int a = 45;
int &b = a;
};
int main() {
C ob1;
cout << sizeof(ob1) << endl;
cout << sizeof(ob1.b);
return 0;
}
This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Closed 4 years ago.
#include<iostream>
using namespace std;
int main(){
float a=1.11;
if(a==1.11){cout<<"yes";} else {cout<<"no";}
return 0;
}
Result:
no
Process exited after 0.2579 seconds with return value 0
Press any key to continue . . .
Captured Result here
Type 1.11f
#include<iostream>
using namespace std;
int main(){
float a=1.11f;
if(a==1.11f){cout<<"yes";} else {cout<<"no";} //here not
return 0;
}
This question already has answers here:
C++: How to round a double to an int? [duplicate]
(5 answers)
round() for float in C++
(23 answers)
Closed 8 years ago.
I am stuck in problem where the double number is not getting properly converted to integer.
In this case->
int x=1000;
double cuberoot=pow(x,(1/(double)3));
int a=cuberoot;
cout<<"cuberoot="<<cuberoot<<endl;
cout<<"a="<<a<<endl;
Output:
cuberoot=10
a=9
Why here a=9 and not 10?
Any solution to this problem??
Also I don't want to round the value..if a=3.67 then it should be converted to 3 only
and not 4.
Because the cuberoot is very close to 10 but not quite 10. std::cout truncates and rounds the number to 10, but a double to integer conversion will strip the decimal which is why a = 9. To solve this problem, you can use std::round():
int a=round(cuberoot);
Try this and see why!
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(){
int x = 1000;
double cube = pow(x, 1.0/3.0);
int a = cube;
cout<<"cube="<< fixed << setprecision(16) << cube<<endl;
cout<<"a="<<a<<endl;
}