C language 12 main parameters and command line parameters, function pointers

Article directory

Parameters of main function and command line parameters

In C standard, function prototype of main function:

int main(int argc, char* argv[]);

As the first function called in C language and the last function returned in C language, main seems to have no way to interact with other functions.
Then why does main design return values and parameters.
The answer is command line arguments.
Main is indeed the first function in C language, but from the perspective of the operating system, main is also called by the operating system.
In order to be able to interact with the program (command), the operating system has made the corresponding Convention for command line passing parameters. So we can pass parameters to C program through command line.
Let's review the command line commands:

cd c:\ 

The above cd is a C program. In the command line, after the C program, the strings separated by spaces are parameters. A program can run with multiple parameters.
Two parameters of main:

  • argc: number of parameters, greater than or equal to 1
  • argv: an array of pointers, which stores multiple strings, in fact, the value of each parameter and the last element is NULL.
int main(int argc, char* argv[])
{
    printf("argc:%d\r\n", argc);
    for (size_t i = 0; argv[i]!=NULL; i++)
    {
        printf("%d:%s\r\n", i, argv[i]);
    }
    return 0;
}

Debugging with parameters in VS

For the program debugging of VS, there are no parameters by default, but you can set them in the project properties:

In the column of command line parameters, you can fill in the parameters, and fill in the format convention of the parameters, which is consistent with the command line (separated by spaces).
Based on the above knowledge, we can easily rewrite our lexical analyzer into command-line form and become more flexible.

int main(int argc, char* argv[])
{
    TokenParse(argv[1]);
    return 0;
}

Function pointer (can increase flexibility)

Function names can be printed:

int main(int argc, char* argv[])
{
    printf("%p\r\n", main);
    return 0;
}

In fact, the function name is the first address of the function in memory
So we can use pointer variable to save function name -- function pointer

Distinguish between functions that return pointers and function pointers

Next, you define a function that can return a pointer (not a function pointer).

int* Fun(int x, int y); 

How to define a function pointer?

int (*g_pfnAdd)(int x, int y);
int main(int argc, char* argv[])
{
    g_pfnAdd = &MyAdd;
    return 0;
}

Functions can be called through function pointers.

int main(int argc, char* argv[])
{
    g_pfnAdd = &MyAdd;
    printf("%d\r\n", g_pfnAdd(10, 20));//First kind
    printf("%d\r\n", (*g_pfnAdd)(10, 20));//Second, more rigorous
    return 0;
}

What's the use of function pointers?

There are three levels of language learning: grammar, principle and design.
The application of function pointer is mostly at the design level (high cohesion and low coupling). In short, it reduces the coupling of all parties in a product.
To unify the interrupt generation with the actual business logic, thus generating functions.
Callback function is a mechanism realized by function pointer.

void* pointer

In C language, there is another * * pointer without explanation**

void*

Use of void * pointer
Because the void * pointer has an address and has no explanation, it cannot be used to dereference. It is characterized by:

  • Any type of address can be converted to a void * type address without warning
  • Address of type void * cannot be directly assigned to other pointers

In C language, using this feature, write some "universal type" functions. For example:

void *memset( void *dest, int c, size_t count ); //Set the count byte space at the dest address to the c value. 

Precompiled instructions - macros

The so-called precompiled instruction refers to the instruction that processes the source code before "compiling C language code".
Precompiled instruction, not C language.
Precompiled instructions in C begin with.
In fact, the compiler's compilation process is:

  • Preprocessing: according to the precompiled instructions, get the final C source code
  • Compile I: change C source code into assembly code
  • Compile II: compile assembly code into machine code

For example, cl.exe shows:

cl /c /P first.c
cl /c /Fas first.c

The macro will be a precompiled instruction. The essence of macro is to find and replace

No reference macro

#define MYMSG "hello, world" 

There are reference macros.

#define MYADD(a,b) a+b
#define MYMUL(a,b) a*b

int main(int argc, char* argv[])
{
    printf("%d\r\n", MYMUL(5‐4, 6‐2)/* 5‐4*6‐2 */);
    return 0;
}

Because the macro is a direct search replacement, it is easy to cause problems if it is not used properly. So the suggestion is to put more brackets.

#define max(a,b) (((a) > (b)) ? (a) : (b))

String and token in macro

If there is a parameter in the macro, it will string the parameter

#define MYMACRO(a) "hello: "#a
int main(int argc, char* argv[])
{
    printf(MYMACRO(hahahaha));
    return 0;
}

For example, the above macro is replaced with:

"hello: ""hahahaha" 

token

Using macros, you can quickly declare the following series of functions by laws:

int MyAddint(int x, int y);
double MyAdddouble(double x, double y);

Expect a macro:

DeclareMyFun(type);

main()
{
    DeclareMyFun(int);//int MyAddint(int x, int y);
    DeclareMyFun(double);//double MyAdddouble(double x, double y);
}

The following attempts may have been made:

#define DeclareMyFun(type) type MyAddtype(type x, type y)

DeclareMyFun(int);
DeclareMyFun(double);

However, this is wrong. When they unfold, they look like this:

int MyAddtype(int x, int y);
double MyAddtype(double x, double y);

If you need to emphasize a part of a token, which is actually a macro parameter, you need to use the.

#define DeclareMyFun(type) type MyAdd##type(type x, type y)
DeclareMyFun(int);
DeclareMyFun(double);
int main(int argc, char* argv[])
{
	return 0;
}
Published 22 original articles, won praise 13, visited 1457
Private letter follow

Tags: C

Posted on Mon, 13 Jan 2020 01:10:08 -0800 by shivani.shm