以下函数检查变量名称是否以字母开头,并且可能具有字母/数字的前面的字符.无论输入是什么,为什么返回值始终为1?
#include <regex.h>
#include <stdio.h>
int validate_var(char *str)
{
regex_t reg;
regcomp(®, "^[a-zA-Z]+[a-zA-Z0-9]*$", 0);
int r = regexec(®, str, 0, NULL, 0);
regfree(®);
return r;
}
int main() {
printf("%d\n", validate_var("abc")); // Reports 1, This makes sense
printf("%d\n", validate_var("17")); // Reports 1, This doesn't make sense
}
Run Code Online (Sandbox Code Playgroud) 考虑一个指向结构的指针数组.以下代码取自您可能在此处找到的示例.我想对这两排铸造进行解释.我不熟悉这种"双重铸造".
int myptrstructcmp(const void *p1, const void *p2)
{
struct mystruct *sp1 = *(struct mystruct * const *)p1;
struct mystruct *sp2 = *(struct mystruct * const *)p2;
Run Code Online (Sandbox Code Playgroud)
我认为它应该是:
int myptrstructcmp(const void *p1, const void *p2)
{
struct mystruct *sp1 = (struct mystruct *)p1;
struct mystruct *sp2 = (struct mystruct *)p2;
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
#include <stdio.h>
char** baz_alloc(int size)
{
char ** b = malloc((size+1) * sizeof(char*));
for (int i = 0; i < size; i++)
b[i] = "baz";
b[size] = NULL;
return b;
}
void baz_print(char** baz)
{
char** b = baz;
while (*b != NULL)
{
printf("%s\n", *b);
b++;
}
}
void baz_free(char** baz)
{
int len = 0;
char** b = baz;
for (; *b != NULL; len++, b++);
for (int i = 0; i < len; i++)
free(baz[i]);
free(baz); …Run Code Online (Sandbox Code Playgroud)