如何将C ++指针设置为零 [英] How to set c++ pointer to zero

查看:95
本文介绍了如何将C ++指针设置为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据

i have a data like this

 char bufget[]="RE000022701500Ì200 0.00 6.113.937";

char preSendBuf[138];
int ptr=0;

int q=strlen(bufget);
for(int i=0;i<q;i++)
{
    while(*digits)
    {
    if((bufget[i]==*digits++))
    {
        preSendBuf[ptr]=bufget[i];
        ptr++;
//i know here we should decrement *digits index again to zero but how

    }
    }


}
preSendBuf[ptr]='\0';
ptr=0;
return 0;


实际上,我应该在


actually data that i should get in

preSendBuf should be "RE000022701500200 0.00 6.113.937"

推荐答案

似乎有人从昨天删除了我对此问题的评论.我已经多次建议您使用制造商提供的信息来处理这些数据记录.每个记录都有一个预定义的结构,使您可以访问各个字段并根据数据规范对其进行验证.利用这些信息,而不是不断尝试发明复杂的解析规则并使您的程序变得更困难.
It appears that someone has deleted my comments from yesterday on this issue. I have suggested to you a number of times how to process these data records, by using the information provided by the manufacturer. Each record has a predefined structure allowing you to access individual fields and validate them against the data specification. Make use of that information rather than continually trying to invent complicated parsing rules and making your programs more difficult that they need to be.


您已经忽略了该信息,但是digits(最初)似乎是一个以0终止的可接受"字符数组.

您要滥用符号digits来遍历该字符集,从而丢失对该数组开头的引用.您应该做的是为此目的引入一个辅助变量(即迭代),例如g .:

You have omitted that information, but digits (initially) appears to be a 0-terminated array of "acceptable" characters.

You are abusing the symbol digits for iterating over that set of characters, thereby losing the reference to the start of this array. What you should do instead is introduce a helper variable for that particular purpose (i. e. iterating), e. g.:

for (int i = 0; i < q; ++i) {
   char* pdigits = digits;
   while (*pdigits) {
      if (budget[i] == *pdigits++) {
         // ...
      }
   }
}


这篇关于如何将C ++指针设置为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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