Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Smita Kapse

Here we will see, why we should use the strict aliasing in C. Before discussing that part, let us see one code, and try to analyze the output.

Example

#include<stdio.h>
int temp = 5;
int* var = &temp;
int my_function(double* var) {
   temp = 1;
   *var = 5.10; //this will change the value of temp
   return (temp);
}
main() {
   printf("%d", my_function((double*)&temp));
}

Output

1717986918

If we call the function my_function, then it will return 1. We can also call this using my_function((double*)&temp). This is supposed to return 1, but here we can see that this is returning something else. This code was made to return constant 1 only. To fix this problem, we can use Strict Aliasing.

Use restrict qualifier keyword. It indicates that we are promising to the compiler that something is not aliased with the pointer restrict keyword. If we break our promise, there will be some problems.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.