小编Fre*_*rey的帖子

在 C 中创建一个通用的“函数指针”联合是一个坏主意吗?

对于我正在处理的项目,最好有一个通用的“函数指针”类型。但是,在 C 中,要拥有指向函数的指针,您需要在函数指针的类型中指定原型。

例如,如果我有 function void setdata(short data),我不能将它存储在与 function 相同的指针中int getdata(),因为它们的参数和返回值是不同的。

经过一些横向思考,我想出了以下解决方法:

typedef long (* PFL)(); /* pointer to function that returns a long... */
typedef short (* PFI)(); /* pointer to function that returns a short... */
typedef void (* PFV)(); /* pointer to function that returns a void... */
typedef void (* PFVAL)(long); /* pointer to function that returns a void...
                                but has a long argument...*/
typedef void (* PFVAI)(short); /* pointer to function that …
Run Code Online (Sandbox Code Playgroud)

c union pointers typedef function-pointers

9
推荐指数
1
解决办法
186
查看次数

如何递归删除某种类型的文件

我误读了gzip文档,现在我必须从许多目录中删除大量的".gz"文件.我尝试使用'find'来查找所有.gz文件.但是,只要名称中有空格的文件,rm就会将其解释为另一个文件.只要有破折号,rm就会将其解释为新旗帜.我决定用'sed'用"\"代替空格,用空格破坏用"\ - "代替,这就是我想出来的.

find . -type f -name '*.gz' | sed -r 's/\ /\\ /g' | sed -r 's/\ -/ \\-/g'
Run Code Online (Sandbox Code Playgroud)

当我在一个文件上运行find/sed查询时,例如,其名称为"Test - File - for - show.gz",我得到了输出

./Test\ \-\ File\ \-\ for\ \-\ show.gz
Run Code Online (Sandbox Code Playgroud)

这对于rm来说似乎是可以接受的,但是当我跑步时

rm $(find . -type f -name '*.gz'...)
Run Code Online (Sandbox Code Playgroud)

我明白了

rm: cannot remove './Test\\': No such file or directory
rm: cannot remove '\\-\\': No such file or directory
rm: cannot remove 'File\\': No such file or directory
rm: cannot remove '\\-\\': No such file or directory …
Run Code Online (Sandbox Code Playgroud)

regex sed find

5
推荐指数
1
解决办法
2778
查看次数

C 结构成员是否继承编译器属性?

对于大多数 C 编译器,可以在结构上指定编译器属性,该属性定义该结构的成员在内存中的对齐方式。前任:

typedef struct{
    char a;
    char b;
} __attribute__((aligned(2))) TwoChars;

Run Code Online (Sandbox Code Playgroud)

如果char a以地址 0xA 结束(为简单起见),则char b不会在地址 0xB 处,而是在 0xC 处,因为它与 2 个字节对齐。

我的问题是:这个属性是由结构成员继承的吗?前任:

typedef struct{
    char a;
    char b;
} TwoChars;

typedef struct {
    TwoChars tc;
    char c;
} __attribute__((aligned(1))) ThreeChars;
Run Code Online (Sandbox Code Playgroud)

这在内存中最终会是什么样子?怎么样} __attribute__((aligned(2))) TwoChars

c gcc struct memory-alignment

3
推荐指数
1
解决办法
74
查看次数

标签 统计

c ×2

find ×1

function-pointers ×1

gcc ×1

memory-alignment ×1

pointers ×1

regex ×1

sed ×1

struct ×1

typedef ×1

union ×1