This question already has answers here:
C/C++: Pointer Arithmetic
(7 answers)
Closed 6 years ago.
Consider the code:
int arr[20]{};
int * ptr1=arr, * ptr2=&arr[1];
std::cout<<ptr1<<std::endl<<ptr2<<std::endl<<ptr2-ptr1;
Output:
0x7fff4003e0d0
0x7fff4003e0d4
1
Why it isn't 4 instead?
If it didn't to that you'd just end up dividing the difference by sizeof(whatever) all the time. The number of elements is far more useful than the raw difference. When you need the latter, cast the two pointers to char* for the subtraction.
Related
This question already has answers here:
C++: what does (a<<b) mean?
(8 answers)
What is 1 << 0?
(8 answers)
Closed 4 months ago.
I know this initialization: bitset<20>.
But what does this initialization mean: bitset<1<<20>?
But what this initialization means?
Parsing headache for the compiler perhaps.
You can use equivalent snippets: bitset< 1<<20 > - i.e. 1 left-shifted by 20 bits - bitset<1048576>
This question already has answers here:
C++ cast syntax styles
(10 answers)
What is the difference between C-like casting and functional casting? [duplicate]
(4 answers)
Closed 3 years ago.
Suppose I have a floating point number x and I want to convert it into an integer. Is there any difference between int(x) and (int)x in C++.
I am new to C++ coming from Python background and naturally I tried int(x) to convert x to an integer but reading about C++ I came across type-casting i.e. (int)x and in this case it also works.
I was wondering what's the difference between these two approaches and in a wider context which one to use where?
float x = 5.5;
cout<<int(x)<<endl; //outputs 5
cout<<(int)x<<endl; //outputs 5
This question already has answers here:
Is it possible to create a type in c++ that takes less than one byte of memory?
(5 answers)
Closed 5 years ago.
I require a 4 bit integer in a design for less memory use. In any version of c++ ,c++11 ,c++14 any can be used for the design.
There is no native 4bit datatype. But you could use an 8bit one to hold two 4bit values in the high/low nibble.
no, but you can use:
struct A {
unsigned int value1 : 4;
unsigned int value2 : 4;
};
This question already has answers here:
Handle arbitrary length integers in C++
(3 answers)
Closed 7 years ago.
Hi I am doing an algorithm question require get the full result of
5208334^2, which is 27126743055556
I was able to do it with by represent integer using Charracter array. However can we have any better way (shorter or faster) to do that? any idea is welcome ?
Updated:
For my case, both long long and int64 work, just that I did not cast value before return:
int val (int n1, n2) {
........
return (long long) n1 * n2;
}
This number fits into long long(present in GCC and after c++11) type or int64(for some other compilers before c++11). Thus the simplest solution is to use this type.
This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 8 years ago.
when i try to calculate any expression, I always get an integer result, it's like:
float k= 5/12;
std::cout<< k<<std::endl;
the output in the console is always 0.
In C/C++, this is an integer division:
5/12
What you want is a floating point division:
5.0/12.0
Please note that this has absolutely nothing to do with GLUT or OpenGL.