Answer by Alex Lop. for MSVC const correctness : spurious C4090 warning
You can disable this warning right before the line which invokes it and restore it back afterwards https://godbolt.org/z/W-XR-Q:#include <string.h>void dummy(void){ int i[42]; int *pi[42]; const...
View ArticleAnswer by Alex Lop. for How do I fix an error in a header file typedef struct...
typedef struct __attribute__((__packed__)) _bitmapThis is the way to define a struct definition without zero paddings in GCC.To prevent zero padding in MVSC, use #pragma pack:#pragma...
View ArticleAnswer by Alex Lop. for When int a[3] has been defined, is there any...
Both b and c point to the same memory but the difference is big:These are two different types (pointer to int and pointer to an array of 3 ints)b is dereferenced to a single integer pointed by b while...
View ArticleAnswer by Alex Lop. for recurrent decent expression parser yeilding a zsh:...
If you enable compiler warnings, you will see that you pass pointer to pointer to struct Node when calling for parse_atom():parse_atom(expr, &Root);while it accepts only a pointer to struct Node...
View ArticleAnswer by Alex Lop. for CODEVITA "similar char" how to reduce my time...
Current execution complexity is O(lq) while l is the length of the input array and q is the number of queries.The complexity of each query is O(l).With proper data structure, you can store the input...
View ArticleAnswer by Alex Lop. for How to check function return value in an if statement...
You can save the return value in a local variable and use it all the way:int check_rc_val = check_length(chars);if (check_rc_val == X){ return check_rc_val;}int check_rc_val = check_rc(chars);if...
View ArticleAnswer by Alex Lop. for for loop: make increment and accessing in one loop
A for loop consists of 4 parts:InitialisationConditionBody codeActionExample: for (initialisation; condition; action) { Body code }The execution order is as I enumerated it above.In your example,for...
View ArticleAnswer by Alex Lop. for Free memory in a loop regardless of return code
It is OK to use such pattern (with goto) in cases when resource management is required and there is a set of checks to be performed while each failure requires to stop the flow and perform the required...
View ArticleAnswer by Alex Lop. for C - calculate address of pointer
I can explain how to calculate the address with few assumptions:The size of int is 4 bytesThe system is little-endianThere are no 0 paddings in the structThus the locals memory layout is so that the...
View ArticleAnswer by Alex Lop. for Is there a better way to mask bits?
For your specific example you can use:if ((bits != 12 && bits != 16) || addressSize != 32) { isValid = false; printf("Invalid!");} else { offsetMask = (1u << bits) - 1;}However it depends...
View ArticleIs pointer arithmetic still well defined after casting with alignment violation?
I know that the result of pointer casting with alignment violation invokes undefined behaviour once dereferenced.But what about pointer casting for address calculation only (without dereferencing)?void...
View ArticleAnswer by Alex Lop. for Trying to confirm XOR's mathematical property
I don't think there is a name for such property. It implies from other properties of the Exclusive Or (see Exclusive Or wiki for more details).Here is the proof:If a^b = c, then a^c = a^(a^b) = <by...
View ArticleAnswer by Alex Lop. for Which kind of signed integer division corresponds to...
In case arithmetic shift right is performed, floor by power of 2 is the best suitable operation which matches signed integer shift right (rounding toward -inf).Note that shift right of signed integer...
View ArticleAnswer by Alex Lop. for What will be the output of the given code below?
You can split this code into something longer but probably more readable like this:#include<stdio.h>int main(){ int a=0, b=1, c=2; int *ptr; if (a + 1 == 1) { // this will be true ptr = &b; }...
View ArticleAnswer by Alex Lop. for Output ',' between number fibonaci in c program
The easiest way is to add additional printf for the ", " string under the condition that it is not the last for loop iteration #include<stdio.h> int main() { int x, y = 0, z = 1, r, i;...
View ArticleAnswer by Alex Lop. for static variable changing value with no error displayed?
printf signature isint printf ( const char * format, ... );The first parameter is the format string and all the following parameters are the format specifiers (subsequences beginning with %).printf("%d...
View ArticleAnswer by Alex Lop. for The behavior of the for loop split between switch cases
default is just a label (address to where the code jumps if n is not -1 or 0).Thus when n is not -1 or 0, the flow enters the body of the for loop skipping the initialisation of j. You can write the...
View ArticleAnswer by Alex Lop. for clang behaves differently with global variables
I understand that extern is the default storage-class specifier for objects defined at file scopeThat's true but the linkage breaks because of "redefinition" of the gv symbol, isn't it?That's because...
View ArticleComment by Alex Lop. on Compiler Optimizations
@Paroz This is very much dependent on the CPU architecture for which the compiler optimizes the code. You can just google for some C "tips and tricks", performing for loops which count down to 0 is...
View Article