我有一个C程序,在命令行上接收标志和文件。为了处理文件,我将它们放入动态分配的类型的char**数组中,Valgrind抱怨使用此数组时
==14389== Memcheck, a memory error detector
==14389== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==14389== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==14389== Command: ./main
==14389==
==14389== Conditional jump or move depends on uninitialised value(s)
==14389== at 0x4838931: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==14389== by 0x10931B: Options_append_filename (main.c:53)
==14389== by 0x1094B9: parse_cmdline (main.c:85)
==14389== by 0x109544: main (main.c:98)
==14389==
==14389== Invalid write of size 8
==14389== at 0x10934A: Options_append_filename (main.c:54)
==14389== by …Run Code Online (Sandbox Code Playgroud) 我想创建一个函数,该函数需要一个数组和一个函数,并在数组中的每个元素上调用该函数
我一直在寻找解决方案,但它们似乎都使用宏,而如果可能的话,我希望使用函数。
我正在寻找可以像下面这样工作的东西
void print_string()
{
printf("%d\n", num);
}
int array[] = { 1, 2, 3, 4, NULL }; // So the foreach function knows when it has reached the end
for_each_function(array, print_number);
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
编辑:它需要是通用的,因此可能需要一个宏