相关疑难解决方法(0)

为什么函数指针定义适用于任意数量的&符号'&'或星号'*'?

为什么以下工作?

void foo() {
    cout << "Foo to you too!\n";
};

int main() {
    void (*p1_foo)() = foo;
    void (*p2_foo)() = *foo;
    void (*p3_foo)() = &foo;
    void (*p4_foo)() = *&foo;
    void (*p5_foo)() = &*foo;
    void (*p6_foo)() = **foo;
    void (*p7_foo)() = **********************foo;

    (*p1_foo)();
    (*p2_foo)();
    (*p3_foo)();
    (*p4_foo)();
    (*p5_foo)();
    (*p6_foo)();
    (*p7_foo)();
}
Run Code Online (Sandbox Code Playgroud)

c c++ function-pointers

204
推荐指数
2
解决办法
1万
查看次数

分段错误:11通过struct中的函数指针返回stuct

我试图模仿一个构造函数.我试图通过让父EJB具有一个返回指向子结构的指针的函数指针来做到这一点.任何帮助将非常感激.

#include <stdio.h>
#include "stdlib.h"

typedef struct child_t {

  char *name;

} Child;

typedef struct parent_t {

  Child (*NewChild)();

} Parent;

Child *NewChild() {
  Child *child = malloc(sizeof(Child));

  child->name = "foo";

  return child;
}

int 
main()
{
  Parent parent;

  Child child = parent.NewChild();
}
Run Code Online (Sandbox Code Playgroud)

c struct pointers segmentation-fault

5
推荐指数
2
解决办法
275
查看次数

重载指向函数的指针

我正在查看以下代码:

string toUpper(string s) {
    string result;
    int (*toupperp)(int) = &toupper; // toupper is overloaded
    transform(begin(s), end(s), back_inserter(result), toupperp);
    return result;
}
Run Code Online (Sandbox Code Playgroud)

我被这条线弄糊涂了:

int (*toupperp)(int) = &toupper; // toupper is overloaded
Run Code Online (Sandbox Code Playgroud)

为什么这条线路必要?

我相信&从内存中检索指向某事物的指针.但是toupper,函数的名称已经是一个指针,不是吗?为什么我们不能这样做:

int (*toupperp)(int) = toupper;
Run Code Online (Sandbox Code Playgroud)

3. int如果函数被重载,为什么它被重载string

c++

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

如何评估以下c程序?

#include<stdio.h>
  int main() {
  int x;
  x=~!printf;
  printf("%x",x);
}
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释推导出这个程序输出的过程.

c

2
推荐指数
1
解决办法
141
查看次数