Click here to Skip to main content
11,790,366 members (63,819 online)
Click here to Skip to main content

C & C++ Language Tricks

, 3 Mar 2000 78.1K 24
Rate this:
Please Sign up or sign in to vote.
More tricks of use to a programmer who shuns run-time libraries.


This document is a collection of some of the interesting constructs that are possible in C and C++. While "nice", these features may not serve to increase code clarity but to obfuscate it. Your mileage may vary.

Playing with the C stack

Parameters on the stack appear in memory with the leftmost item at the lowest memory location. Thus you can get a pointer to your functions stack frame, and walk the parameter list, or even cast your stackframe to a struct that can be passed to other functions.

The first sample demonstrates how to retrieve the pointer to any variable parameters passed to the function.

int __cdecl printf(char const* pszFmt,...)
  char buf[1024];
  return wvsprintf(buf,pszFmt,&pszFmt+1);

This sample demonstrates how the functions passed to a function can be passed to another function.

BOOL InvalidateRect(
  HWND hwnd,
  LONG left,
  LONG top,
  LONG right,
  LONG bottom,
  BOOL fErase)
  return InvalidateRect(hwnd,(LPRECT)&left,fErase);

Constructing variable length structs

A trick to using structs that have a variable sized member is to use an overloaded operator new to allocate space for the struct.

struct string {
  int cb; // a data member
  char sz[]; // the string data

  void* operator new(int cb, int cbExtra){
    return new char[cb+cbExtra];

Now contrast the following code to create such a struct:

string* pStr = new(strlen(pszInString)) string;

The "C" method of working with variable sized looks like this:

string* = (string*)new char[sizeof(string) + strlen(pszInString)];

Working with big endian data

A have always favored little endian number representation over big-endian, which why I was saddened when I found that TCP/IP, and most internet protocols (such as SSL3) use a big endian representation for numbers. Following is the type of construct I use when I need to work with a non-native data type in structs that I pull off of the network.

typedef unsigned char uint8;

struct uint16 {
  uint8 n[2];

  operator unsigned short(){
    return ((unsigned short)n[0] << 8) + n[1];

Using a struct like this has two advantages:

  1. If a typedef unsigned short was used you could accidently use the number in situations without converting it.
  2. The operator unsigned short ensures that the struct can be used as a normal unsigned short where required.

Please send any comments or bug reports to me via email. For any updates to this article, check my site here.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Chris Becke
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalproject c++ Pin
emansamiee6-Feb-08 5:24
memberemansamiee6-Feb-08 5:24 
Generalc++ Pin
2rtl0v3r9-Oct-07 20:11
member2rtl0v3r9-Oct-07 20:11 
QuestionEver heard of Winsock? Pin
Mike Smith23-May-02 12:05
memberMike Smith23-May-02 12:05 
GeneralThat's not the C way Pin
Bob Smith8-Aug-00 16:50
sussBob Smith8-Aug-00 16:50 
GeneralCool, but not portable! Pin
Eddie Velasquez5-Mar-00 14:15
sussEddie Velasquez5-Mar-00 14:15 
GeneralRe: Cool, but not portable! Pin
James Curran 9-Mar-00 9:59
sussJames Curran 9-Mar-00 9:59 
GeneralRe: Cool, but not portable! Pin
STEP4-May-00 19:23
sussSTEP4-May-00 19:23 
GeneralRe: Cool, but not portable! Pin
Fab7-Jul-00 6:34
sussFab7-Jul-00 6:34 
GeneralRe: Cool, but not portable! Pin
Jason Douglas10-Jan-01 7:20
memberJason Douglas10-Jan-01 7:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1509028.1 | Last Updated 4 Mar 2000
Article Copyright 2000 by Chris Becke
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid