Static Polymorphism >>

Categories: C++

To explain the polymorphism nothing is better than see how stuff used to be. If you were a twenty old C programmer in the past and created the following functions:

int sum(int x, int y);
double sum(double x, double y);
 
int main()
{
    int zi = sum(2, 3);
    double zd = sum(2.5, 3.4);
    return 0;
}
 

Immediately the compiler would blame you about the following errors:

Funky do-while >>

Categories: C++

It's a known habit to use do-while constructions when there's a need to define a macro that has more than one command instead of using the { simple multicommand brackets }. What was never clear is why this is so.

C and C++ Operators Precedence Table >>

Categories: C++
Wanderley,your explanation about why a program compiles in C++ and not in C seems to me logic and correct, but gave me some doubts, because I always learned that the C and C++ operator precedence are the same thing.I checked out the Appendix A in the "C ++ - How To Program" (sixth edition) and the book table is equal to the C operators precedence table and it is different from the C++ precedence table presented by you in the article.I went to the internet and found out in two websites the table and both are equal to the book table.http://en.wikipedia.org/wiki/Operators_in_C_and_C
http://www.cppreference.com/operator_precedence.html
From where did you get the presented C++ table?[]s,Márcio Andrey Oliveira

What happens inside the sizeof operator >>

Categories: C++

The question: how to get the size of a struct member without declaring it as a variable in memory? In pseudocode:

static const size_t FIELD_SIZE_MSGID = 15;
 
struct FEEDER_RECORD_HEADER
{
   char MessageID[FIELD_SIZE_MSGID];
   char MessageIndex[10];
};
 
// error C2143: syntax error : missing ')' before '.'
char MessageIndexBuffer[sizeof(FEEDER_RECORD_HEADER.MessageIndex) + 1];
 
// error C2070: '': illegal sizeof operand
char MessageIndexBuffer[sizeof(FEEDER_RECORD_HEADER::MessageIndex) + 1];

In this first try (even being a nice one) we can clearly see by instinct that the construction is not supposed to work. The compiler error is not even clear. The member access operator (the point sign) needs to have as its left some variable or constant of the same type of the struct. Since the operand is the type itself, there is no deal.

Precedence difference >>

Categories: C++

Once upon a time my old friend Kabloc wrote this little and "harmless" function in order to print the multiplication table:

#include <stdio.h>
 
int main()
{
	int f1,f2,s=0;
	   for(f1=1;(f1==11&&s!=5)?s=5,f1=0,putchar(10):(f1<=10)?f1=f1:f1=12,f1<=11;f1++)
			for(f2=1+s;f2<=5+s;f2++)printf("%dx%d=%d%c",f2,f1,f1*f2,(f2==5+s)?10:9);
	return 0;
}

Despite the fact the result is a strong candidate to "The International Obfuscated C Code Contest", the linux guys told him the code was not successful on GCC, and somewhere inside those four lines there was a non-standard piece of code.

Disassembling the array operator >>

Categories: C++

Arrays are fascinating in C language because they are so simple and so powerful at the same time. When we start to really understand them and realize all its power we are very close to understand another awesome feature of the language: pointers.

When I was reading the K&R book (again) I was enjoying the language specification details in the Appendix A. It was specially odd the description as an array must be accessed: