#include <stdio.h>
#include <iso646.h>
#include <stdlib.h>
#define FAIL exit(EXIT_FAILURE);
void check_params(const int params_count, char ** params) // const char ** const params - error
{
if (not ((params_count == 2) and (params != NULL) and (params[1] != NULL)))
{
puts("Input params check failed. Program terminated.");
FAIL
}
}
int main(int params_count, char * params[])
{
check_params(params_count, params);
}
如何在被调用函数中正确创建指向常量数据的常量指针,这可能吗?函数中必须不能更改参数中的任何数据。
更新 @Harry 提出的解决方案抛出一个错误:
$ gcc test.c -Wall -Wextra --pedantic --std=c23
test.c: In function ‘main’:
test.c:19:32: error: passing argument 2 of ‘check_params’ from incompatible pointer type [-Wincompatible-pointer-types]
19 | check_params(params_count, params);
| ^~~~~~
| |
| char **
test.c:7:70: note: expected ‘const char * const* const’ but argument is of type ‘char **’
7 | void check_params(const int params_count, const char * const * const params)