通过unsigned char型指针,未投给atoi [英] Pass unsigned char pointer to atoi without cast

查看:405
本文介绍了通过unsigned char型指针,未投给atoi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些嵌入式设备,我已经通过了 unsigned char型指针的atoi 没有铸造。

  unsigned char型C [10] =12;
与atoi(C);

问题:?是它明确定义

<我一个href=\"http://stackoverflow.com/questions/24767522/passing-unsigned-char-array-to-string-functions\">saw某个地方,这是确定的字符串函数,但不知道的atoi

编辑:顺便说一句。一些担心已经pssed下面,它可能甚至不是字符串函数,如strcpy的是OK的答案之一EX $ P $ - (?),但如果我得到正确的笔者也意味着它可以在实践中这可能是OK。


另外,我在这里,是确定这样做下面的分配 unsigned char型指针确定吗?因为我使用的一些工具,它抱怨类型不匹配(转让)(师生比为符号/无符号),

 无符号字符* PTR =的strtok(unscharbuff, - );
    //是分配也确定无符号的字符?


解决方案

没有,这不是很好的界定。这是一个违反约束,需要一个编译时诊断。在实践中这是非常非常有可能像您期望的工作,但它不能保证这样做,恕我直言这是作风不好。

的atoi 函数声明在&LT;&stdlib.h中GT; 为:

  INT的atoi(为const char * NPTR);

您正在传递一个无符号字符* 参数期望一个的char * 参数的函数。这两种类型的不兼容,并有从一个到另一个的隐式转换。一个符合编译器可能会发出警告(计数作为诊断),然后进行生成一个可执行文件,但该可执行文件的行为是不确定的。

由于C99的,对函数的调用没有可见的声明是违反约束,所以你不能通过省略的#include&LT摆脱它; stdlib.h中&GT;

Ç确实还允许用可见声明函数的调用,其中该声明是不是一个原型(即,没有定义的参数的类型(多个)的数量)。因此,而不是通常的的#include&LT;&stdlib.h中GT; ,你可以添加自己的声明:

  INT的atoi();

这将允许与无符号字符* 参数调用它。

这几乎肯定会工作,并有可能构造从标准,其行为被很好地定义一个参数。在字符的无符号的字符 1 '2'都保证具有相同的重presentation

但它要容易得多加投,而不是证明它是没有必要的 - 或者更好,来定义 C 字符,而不是 unsigned char型数组,因为它打算举行一个字符串。

 无符号字符* PTR =的strtok(unscharbuff, - );

这也是一个约束冲突。有一个从无符号字符* 的char * 在第一个参数的隐式转换strtok的调用,并有来自的char * 的隐式转换为无符号字符* 的初始化 PTR

On some embedded device, I have passed an unsigned char pointer to atoi without a cast.

unsigned char c[10]="12";
atoi(c);

Question: is it well defined?

I saw somewhere it is ok for string functions, but was not sure about atoi.

Edit: Btw. Some concerns have been expressed on one of the answer below that it might not be OK even for string functions such as strcpy - but if I got right (?) the author meant also it can be that in practice this can be OK.


Also that I am here, is it ok to do following assignment to unsigned char pointer ok too? Because I used some tool which is complaining about "Type mismatch (assignment) (ptrs to signed/unsigned)"

unsigned char *ptr = strtok(unscharbuff,"-");
    // is assignment also ok to unsigned char? 

解决方案

No, it's not well defined. It's a constraint violation, requiring a compile-time diagnostic. In practice it's very very likely to work as you expect, but it's not guaranteed to do so, and IMHO it's poor style.

The atoi function is declared in <stdlib.h> as:

int atoi(const char *nptr);

You're passing an unsigned char* argument to a function that expects a char* argument. The two types are not compatible, and there is no implicit conversion from one to the other. A conforming compiler may issue a warning (that counts as a diagnostic) and then proceed to generate an executable, but the behavior of that executable is undefined.

As of C99, a call to a function with no visible declaration is a constraint violation, so you can't get away with it by omitting the #include <stdlib.h>.

C does still permit calls to functions with a visible declaration where the declaration is not a prototype (i.e., doesn't define the number of type(s) of the parameters). So, rather than the usual #include <stdlib.h>, you could add your own declaration:

int atoi();

which would permit calling it with an unsigned char* argument.

This will almost certainly "work", and it might be possible to construct an argument from the standard that its behavior is well defined. The char and unsigned char values of '1' and '2' are guaranteed to have the same representation

But it's far easier to add the cast than to prove that it's not necessary -- or, better yet, to define c as an array of char rather than as an array of unsigned char, since it's intended to hold a string.

unsigned char *ptr = strtok(unscharbuff,"-");

This is also a constraint violation. There is no implicit conversion from unsigned char* to char* for the first argument in the strtok call, and there is no implicit conversion from char* to unsigned char* for the initialization of ptr.

这篇关于通过unsigned char型指针,未投给atoi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆