
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Why strict aliasing is required in C?
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.
- Related Articles
- Aliasing in PostgreSQL?
- Why is f required while declaring floats in C#?
- Effects of Undersampling (Aliasing) and Anti-Aliasing Filter
- In JavaScript Why do we use \"use strict\"?
- Why do we use "use strict" in JavaScript?
- Why @SafeVarargs is required in Java 9?
- Why confidentiality is required in information system?
- PHP Aliasing/Importing namespaces
- Why is sodium chloride required in our body?
- What is Strict mode in JavaScript?
- What is the difference between "strict" and "non-strict" modes of JavaScript?
- What is Scrum Call and Why is It Required?
- Why is an electric fuse required in all electrical appliances?
- Strict Mode in ReactJS
- What is Strict Mode in JavaScript and How to Enable It?

Advertisements