C / C ++ and #define topic --typedef

Title: macro definitions and difference operators.

【answer】

Macro definition is one of the three kinds of pre-processing functions provided by the start of the C language. These three pretreatment are: macro definitions, and files containing conditional compilation. A macro definition is a loop operation, do calculations and solve the expression, do not take up memory and compile time.

Title: The following macro MIN , the output is how much.

#define MIN(a,b) (a)<(b)?(a):(b)
int main()
{
         int a =2;
int b =4;
int min = MIN(++a, b);
printf("%d",min);
         return 0;
}

【answer】

4

【answer】

Wang Min as an analog function, the compiler will recognize it as (++ a) <(b)? (++ a) :( b), after a self-imposed conditions are met, but also self-imposed time.

Title: Write swap (x, y) using macro definitions, i.e., the exchange of two numbers.

【answer】

#define swap(x, y) (x)=(x)+(y);(y)=(x)–(y);(x)=(x)–(y);

Topic: I would like to define some functional macros, for example:

#define square(x) x*x

But they are not always correct. why?

[ Answer ]

Macro expansion is purely text expansion. To avoid accidents, the definition of functional macro, remember three rules listed below.

(1) macro expansion brackets must be used in order to protect the expression of low priority operator. For example, the top problem (error) of the square () macro call

1/square(n)

It will be extended to

1/n*n

This is equivalent to (1 / n) * n. And you need is

1/(n*n)

Here, the problem lies in the combination rather than a priority, but the effect is the same.

(2) defined inside the macro, it occurs all the parameters must be enclosed in parentheses, in order to protect any argument low priority operator not affect other parts of the macro expansion. Also to square (), for example, call

square(i++)

It will be extended to

i++*i++

And then undefined.

Title: parameterized with reference to the difference between the macro function (say at least 5:00)?

【answer】

 

Parameterized macro

Parameterized function

Processing time

Compile time

Runtime

Parameter Type

no

Need to define

Program length

lengthen

constant

Take up storage space

no

Yes

operation hours

It does not account for the running time

Calls and returns occupancy

Title: For a short function is frequently used, what applications implemented in C language, the application in C ++ to achieve what?

[Answer] c with macro definition, c ++ with inline

Title: For user-defined types, typedef and #define What is the difference?

【answer】

typedef: keywords with type checking . Defined array, a pointer, structure and other more convenient. typ edef having compliance scoping rules advantageous, not only can the function declarations external , may be declared inside a function or block .

define: preprocessing directives, do not do the type checking , and only when you compile the source code has been deployed will find possible errors and error. Defined constants, variables, compiler switches (such as switch character encoding format) and the like. define have judged advantages , the use of #ifdef.

[ Resolved ]

For example analysis:

typedef char *String_t;
#define String_d char*
String_t s1,s2;
String_d s3,s4;

s1, s2 and s3 are defined become char *, char but became s4 type was defined.

Topic: What's the difference between structure and union?

[Answer]
1. Joint structure and are made of a plurality of different types of data members, but at any one time, only stored for a joint selected member (all members of a common address space), and all members of the structure there are (different members of different storage address).

2. For different members of a joint assignment will override the other members of the original value of the member does not exist, but for different members of the assignment structure is independently of each other.

Title: The following output on the "joint" of the topic?

(a)

#include<stdio.h>
union
{
int I;
char x[2];
}a;

void main()
{
a.x[0]=10;
a.x[1]=1;
printf(“%d”,a.i);
}

【answer】

266 (low-low address, high address high, memory usage is 0X010A)

If x is not initialized array, the result is zero.

If the ax [1] = 1, the result is 522

Topic: I can not seem to define a linked list successfully, I tried

typedef struct
{
char *item;
NODEPTR next;
}*NODEPTR;

But the compiler reported an error message, do not in the C language structure can not contain a pointer to itself it?

【answer】

typedef defines a new type name, but a new type name at the end. In the structure using the name is wrong, because before that, has not been achieved to define a new type name in the true sense, but at the end of the structure.

Solution 1 :

typedef struct node
{
char* item;
struct node *next;
}*NODEPTR;

Solution 2 :

struct node
{
char *item;
struct node *next;
};
typedef struct node *NODEPTR;

Topic: How to define a structure refer to each other? I tried

typedef struct
{
int afield;
BPTR bpointer;
}*APTR;

typedef struct
{
int bfield;
APTR apointer;
}*BPTR;

But the compiler encounters the first time BPTR when it is not defined.

[ Answer ]

And on a similar topic.

struct a;
struct b;
typedef struct a *APTR;
typedef struct b *BPTR;
struct a
{
int afield;
BPTR bpointer;
};

struct b
{
int bfield;
APTR apointer;
};


Topic: "typedef int (* funcptr) ();" What does this mean? 【answer】

It defines a type funcptr, showing point type int return value (parameter unspecified) function pointer.

Topic: I define a extern array in a file, and then use another file:

Source File 1:

int array[]={1,2,3};

Source File 2:

extern int array[];

Why in the source file 2, sizeof married less than the size of the array?

【answer】

Extern digital unspecified size is an incomplete type. Sizeof not use it, because the role sizeof occurs at compile time, it can not obtain the array size is defined in another file.

1 resolved :

Source File 1:

int array[]={1,2,3};

int arrayz=sizeof(array);

Source File 2:

extern int array[];
extern int arraysz;

Solve 2 :

1 header file:

#define ARRAYSZ 3

Source File 1:

#include”头文件1”
int array[ARRAYSZ];

Source File 2:

#include”头文件1”
extern int array[ARRAYSZ];

Solve 3 :

Source File 1:

int array[]={1,2,3,-1};//最后一个元素是”哨兵”值(通常是0、-1、NULL)来确定数组的长度。

Source File 2:

extern int array[];

Title: this code why not?

struct x{…};
x thestruct;

[ Answer ]

C is not C ++. Not automatically generate structural type definition name tag. In fact, C language structure is such that the keyword struct declaration: struct x thestruct;

Solve 2 : Use typedef.

typedef struct {…} tx;
tx thestruct;

 

 

Guess you like

Origin blog.csdn.net/chen1083376511/article/details/92069640