scanf(“%d”,char *) - char-as-int格式字符串? [英] scanf("%d", char*) - char-as-int format string?

查看:319
本文介绍了scanf(“%d”,char *) - char-as-int格式字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char-as-number的格式字符串修饰符是什么?



我想读入一个永不超过255



使用典型

  ] =x32; 
char separator;
unsigned char dest;
int len;

len = sscanf(source,%c%d,& separator,& dest);
//验证并继续...

我得到预期的警告:参数4的sscanf是类型char *,int *预期。



根据我的理解规范,没有char的修饰符(例如%sd简写或%lld 64位元)




  • 是危险的吗? (将溢出只是溢出(翻转)变量或将它写在所分配的空间之外?)

  • 是否有一个更好的方式来实现分配一个临时int变量?


解决方案 div>

您可以在glibc的%hhd ncorfer> scanf() ,MSVC似乎不支持整数存储到一个char直接(参见MSDN scanf Width Specification 了解有关支持的转换的详情)


What is the format string modifier for char-as-number?

I want to read in a number never exceeding 255 (actually much less) into an unsigned char type variable using sscanf.

Using the typical

 char source[] = "x32";
 char separator;
 unsigned char dest;
 int len;

 len = sscanf(source,"%c%d",&separator,&dest);
 // validate and proceed...

I'm getting the expected warning: argument 4 of sscanf is type char*, int* expected.

As I understand the specs, there is no modifier for char (like %sd for short, or %lld for 64-bit long)

  • is it dangerous? (will overflow just overflow (roll-over) the variable or will it write outside the allocated space?)
  • is there a prettier way to achieve that than allocating a temporary int variable?
  • ...or would you suggest an entirely different approach altogether?

解决方案

You can use %hhd under glibc's scanf(), MSVC does not appear to support integer storage into a char directly (see MSDN scanf Width Specification for more information on the supported conversions)

这篇关于scanf(“%d”,char *) - char-as-int格式字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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