На страницу группы практики...
Передача параметров
Несколько аргументов:
void Func1( int x, int y, int z )
{
if (x == y && z > 0)
...
}
void main( void )
{
Func1(5, 15, 8);
}
Битовые комбинации:
#define PAR1 0x0001 /* 0000001 */
#define PAR2 0x0002 /* 0000010 */
#define PAR3 0x0004 /* 0000100 */
#define PAR4 0x0008 /* 0001000 */
#define PAR5 0x0010 /* 0010000 */
void Func2( int mode )
{
if (mode & PAR1)
set_parameter1();
if (mode & PAR2)
set_parameter2();
...
}
void main( void )
{
Func2(PAR1 | PAR2 | PAR5);
}
Группировка в структуры:
#include <string.h>
#define TRUE 1
#define FALSE 0
#define bool int
typedef struct
{
int Left, Top, Right, Bottom;
} RECT2D;
typedef struct tagDESCRIPTION
{
RECT2D Window;
char Text[27];
bool IsWindowed;
} DESCRIPTION;
void Func3( DESCRIPTION *Params )
{
if (Params->IsWindowed)
{
create_window(Params->Window.Left, Params->Window.Top,
Params->Window.Right, Params->Window.Bottom);
set_window_text(Params->Text);
}
...
}
void main( void )
{
DESCRIPTION win;
memset(&win, 0, sizeof(DESCRIPTION));
win.IsWindowed = TRUE;
strcpy(win.Text, "My Window");
win.Window.Left = 15;
win.Window.Top = 27;
win.Window.Right = 59;
win.Window.Bottom = 36;
Func3(&win);
}
Примеры структур в WinAPI
(объявлены в <windows.h>):
typedef struct tagRECT
{
LONG left;
LONG top;
LONG right;
LONG bottom;
} RECT, *LPRECT;
typedef struct tagPOINT
{
LONG x;
LONG y;
} POINT, *LPPOINT;
RECT rc;
POINT pt;
rc.left = 0;
pt.x = 1;
...
void f( LPRECT prc )
{
prc->left = 1;
...
}
f(&rc);
Строки:
#include <string.h>
...
char S[100], S1[100];
strcpy(S, "Abc");
strcpy(S1, S);
strcat(S, S1);
if (strlen(S) > 5)
S[0] = 0;
printf("%s, %s\n", S, S1);
...