Pointer arithmetic output issues [duplicate] - c

This question already has answers here:
C/C++: Pointer Arithmetic
(7 answers)
Closed 4 years ago.
Here are we use typecasting from pointer to integer, but output of arithmetic operation are different from expected answer, Why?
Source Code :
int main(){
int *p,*q;
p = 1000;
q = 2000;
printf("%d",q-p);
return 0;
}
Output: 250

The same reason due to which p++ will give you 1004. Since sizeof(int) on your machine is 4, hence each operation is shown wrt sizeof(int), even the difference i.e. 1000/4.

Related

sizeof() in C conditional operator [duplicate]

This question already has answers here:
How to compare signed and unsigned (and avoiding issues)
(1 answer)
Why is this happening with the sizeof operator when comparing with a negative number? [duplicate]
(2 answers)
Closed 11 months ago.
#include<stdio.h>
int main(){
int i;
// printf("%d",sizeof(i)) ;
printf("%d",(sizeof(i) > (-1))) ;
return 0;}
why does the code print 0 when sizeof(i) gives 4 in 64 bit OS?
why does (sizeof(i) > (-1))) gives false(0) ?
Use a better compiler and enable warnings. Under any sane compiler you should have gotten a warning about comparing an unsigned and a signed value.
This should be closer to what you want:
printf("%d", (int)sizeof(i) > -1);
Or at least this:
printf("%d", sizeof(i) >= 0);
However your code is a no-op anyway, because it's impossible to have a negative size of a type.

why is sizeof() returning 4 bytes rather than 2 bytes of short int? [duplicate]

This question already has answers here:
Why does sizeof(char + char) return 4?
(2 answers)
Why must a short be converted to an int before arithmetic operations in C and C++?
(4 answers)
sizeof operator returns 4 for (char + short ) [duplicate]
(3 answers)
What happens here? sizeof(short_int_variable + char_variable)
(5 answers)
How does sizeof work for different data types when added and calculated? [duplicate]
(1 answer)
Closed 5 years ago.
To print the size of the variables using sizeof()
#include <stdio.h>
main()
{
int a = 10,b = 20;
short int c;
short int d = sizeof(c = a+b);
int e = sizeof(c*d); //e holds the value of 4 rather than 2
double f = sizeof(e*f);
printf("d:%d\ne:%d\nf:%lf\n",d,e,f);
}
Why is sizeof() returning the size of int rather than short int which is meant to be 2 bytes?
The statement
sizeof(c = a+b);
doesn't measure the size of variable c but the size of the value computed from expression c = a+b. It is the value of a+b that is assigned to c but it is also the value of the entire expression.
The integral values whose storage type is smaller than int that appear in an arithmetic expression are promoted to int (or unsigned int) for the computation. The storage type of the result of the arithmetic expression is int. This is not affected that the fact that you store it in a short int variable. Hence the value returned by sizeof().
The same for sizeof(c*d).

how does this initialisation in c works? [duplicate]

This question already has answers here:
C comma operator
(4 answers)
Closed 7 years ago.
int a=3,1;
int b=(5,4);
I am a beginner in c and I noticed in a book this type of initialization . what does this initialisation mean?
int b = (5,4) will first evaluate 5 then 4. The last thing that is evaluated will be assigned to the variable. For example
int b = (5,4,3,2,1)
in this case the value of b will be 1.

Value difference between two pointers is not making sense [duplicate]

This question already has answers here:
Pointer Arithmetic In C
(2 answers)
Closed 8 years ago.
I'm not getting the output. Why it is happening?
#include <stdio.h>
int main(void){
int a[3][3];
int *p, *q;
p=a[0];
q=a[1];
printf("%d\n",sizeof(int));
printf("%d\n",q-p);
printf("%d %d\n",q,p);
return 0;
}
Output
4
3
2686728 2686716
I thought (q-p) should be 12! Is my math degrading?!
I thought (q-p) should be 12
No. (q-p)==3 shall hold true since they have type int*. Meanwhile it's true that q == p + 3.
Also this is true: (char*)q - (char*)p == 12
You are getting output value as 3, because both pointers are pointing to integer data type and the difference between them is three objects.

Memset issue with floating point array in C [duplicate]

This question already has answers here:
Initializing a float array with memset
(5 answers)
Closed 9 years ago.
I'm trying this simple example to populate a floating point array with 5.6, but upon printing the values out, every value is just 0.0.
#include <string.h>
float testArr[20];
memset(testArr, (float)5.6, 3*sizeof(float));
printf("Value 1: %lf\n",testArr[0]);
printf("Value 2: %lf\n",testArr[1]);
printf("Value 3: %lf\n",testArr[2]);
printf("Value 4: %lf\n",testArr[3]);
I've also tried not casting 5.6 as a float, setting testArr[20] = {} and testArr[20] = {0}, but they also result in the same 0.0.
memset fills the memory with 1 char, not float.

Resources