小编use*_*982的帖子

警告:return在没有强制转换的情况下从整数生成指针但是根据需要返回整数

我试图找出从C中的void*函数调用返回一个整数的正确方法.

即......

#include <stdio.h>

void *myfunction() {
 int x = 5;
 return x;
}

int main() {
  printf("%d\n", myfunction());
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但我一直在:

警告:返回从整数中生成指针而不进行强制转换

我需要做一个演员来完成这项工作吗?它似乎没有问题地返回x,真正的myfunction返回指向结构和字符串的指针,它们都按预期工作.

c integer casting function void

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

是否可以使用panic/recover作为测试成功类型断言的方法?

我一直在尝试解析嵌套的JSON响应而不将信息映射到预定义的结构.

使用空白界面,它会返回:

map[name:My Folder parentId:1 created:2014-10-09T16:32:07+0000 deleted:false description:Sync Dir id:3 links:[map[rel:self entity:folder href:https://web.domain.org/rest/folders/3 id:3] map[href:https://web.domain.org/rest/folders/1 id:1 rel:parent entity:folder] map[entity:user href:https://web.domain.org/rest/users/1 id:1 rel:creator]] modified:2014-12-18T18:07:01+0000 permalink:https://web.domain.org/w/SpJYGQkv syncable:true type:d userId:1]

所以我使用以下内容来浏览此信息:

func NFind(input interface{}, refs...interface{}) (output interface{}) {
    defer func() {if r := recover(); r != nil { output = nil }}()

    for _, ref := range refs {
        switch cur := ref.(type) {
            case string:
                output = input.(map[string]interface{})[cur]
            case int:
                output = input.([]interface{})[cur]
        } 
    }
    return output
}

func NMap(input interface{}) (output …
Run Code Online (Sandbox Code Playgroud)

json recover go panic type-assertion

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

标签 统计

c ×1

casting ×1

function ×1

go ×1

integer ×1

json ×1

panic ×1

recover ×1

type-assertion ×1

void ×1