2012-11-23 87 views
-1

以下代码铸造u_int32到char

u_int32 *data_out 
data_out = malloc(4 * sizeof(uint32_t)); 
//connection_fd is a socket 
n = write(connection_fd, data_out[0], strlen(data_out[0])); 

产生以下错误:

Array.c:261:警告:传递的 'strlen的' 参数1时将整数指针,未作铸造 /usr/include目录/ string.h中:399:注:应为 '为const char *',但参数类型是 'uint32_t的' 的

我尝试以下为好,但仍然是一个警告的结果:

n = write(connection_fd, (char) data_out[0], strlen((char) data_out[0])); 
n = write(connection_fd, (char*) data_out[0], strlen((char*) data_out[0])); 

感谢您的任何见解:), 帕特里克

+2

百万美元的问题:*你想要做什么?* – Jon

+3

为什么你使用'strlen()'的东西肯定不是一个字符串? – Crozin

回答

2

为什么你正在使用的strlen写的诠释?
也许使用:

n = write(connection_fd, &(data_out[0]), sizeof(uint32_t)); 
+0

最好是'data_out',而不是'data_out [0]',因为这是由'write'调用的地址拉取的。 – WhozCraig

+0

你是对的,但我想他想指向第一个数组元素,所以也许更好'$(data_out [0])'明确表示我的意思是第一个元素 –

+0

我优先于'(data + n)'for指针算术,但每个人都有自己的偏好。这有效,似乎解决了OP的问题。 GJ。 – WhozCraig

1

我按照你的警告消息

第一种情况:strlen()期待一个const char *

第二种情况:转换为char只是不会工作,即使你char*你会得到警告,因为,strlen()适用于\0 null结束字符串给出字符串的长度,但data_out[i]int这样的问题

这里要传递int为指针char这就是为什么警告:

passing argument 1 of 'strlen' makes pointer from integer without a cast 

正如我所看到的一切是在消息中明确的,你为什么不理解。

Array.c:261: warning: passing argument 1 of 'strlen' makes pointer  
from integer without a cast /usr/include/string.h:399: note: expected 
'const char *' but argument is of type 'uint32_t' 

对于write()也存在问题。

(char*)data_out(char*)&data_out[0]与第二个参数相反,而不是您所做的。

+0

@帕特里克:希望你明白了.. – Omkant

+0

非常好,那就像一个治疗:) – Patrick

0

看起来编译器已经告诉过你代码出了什么问题。

strlen' makes pointer from integer without a cast

意思是:你试图在没有强制转换的情况下将一个整数显示到指针变量中。

expected 'const char *' but argument is of type 'uint32_t'

方式:函数想一个const char 指针但你给它一个uint32_t的变量实例