简体   繁体   English

如何使用带指针的 For 循环消除最后一个零?

[英]How to eliminate the last zero using a For loop with pointers?

#include<stdio.h>
#include<string.h>

int main(){
    char num[6];
    char pass[6];
    int digits=4, i;
    char *p;
    memset(pass, '\0', sizeof(pass));
    memset(num, '\0', sizeof(num));
    strncpy(num, "000104",6);
    p = (char *)&num;
    //printf("p has this [%6.6s]\n",p);

    for(i = 0; i < 5, *p == 0; i++, p++){
    //printf("for [%d]\n",i);
    //printf("pointer [%c]\n",*p);
    strncpy(pass, num+(6-digits), digits);
    //printf("pass: [%6.6s]\n", pass);
    }
 
}

This is my code.这是我的代码。 I want to delete the last zero in the room variable using that pointer in the if condition.我想在 if 条件中使用该指针删除房间变量中的最后一个零。 I'm new using pointers.我是使用指针的新手。 This is the last output I'm getting from the variable:这是我从变量中得到的最后一个 output:

pass: [ 0104] (it should be 104).通过:[0104](应该是 104)。

Can someone explain me how that pointer condition is working?有人可以解释一下指针条件是如何工作的吗? Why is not detecting and deleting the last zero character in the string?为什么不检测和删除字符串中的最后一个零字符?

Your question title is confusing.您的问题标题令人困惑。 To me, deleting the "last zero" in your string would mean the zero between 1 and 4 .对我来说,删除字符串中的“最后一个零”意味着14之间的零。 What you're actually trying to do is remove leading zeroes .您实际上要做的是删除前导零 That is very simple:这很简单:

while (*p == '0') ++p;

Now, p points to the first character that is not '0' .现在, p指向第一个不是'0'的字符。

It's not clear what you're trying to do in your loop, which is why I haven't attempted to fix it.目前尚不清楚您要在循环中做什么,这就是我没有尝试修复它的原因。 I will say that copying an entire string every time around the loop is generally bad practice.我会说每次循环复制整个字符串通常是不好的做法。

As @paddy noticed, the question is rather unclear, but there are a couple of things that catch the eye and are worth pointing out.正如@paddy 注意到的那样,这个问题相当不清楚,但有几件事引起了人们的注意并且值得指出。
I've highlighted them in the multiline comments /**/我在多行注释中突出显示了它们/**/

#include<stdio.h>
#include<string.h>

int main()
{
    char  num[6];
    char  pass[6];
    int   digits = 3;
    int   i;
    char *p;
    
    memset(pass, '\0', sizeof(pass));
    memset(num, '\0', sizeof(num));
    strncpy(num, "000104", 6);

/**
 * Casting &num to (char *) silences the compiler, but it was highlighting a mistake!
 * Also, the cast was not needed at all,
 * because num is a char[] and it can be assigned to a char * 
 */ 
    p = (char *)&num; // p = num; is the right way
    // printf("p has this [%6.6s]\n", p);

/**
 * Only the first 5 elements would be accessed,
 * if they were accessed at all, because *p == '0', then *p != 0,
 * therefore this loop is never executed
 */
    for(i = 0; i < 5, *p == 0; i++, p++)
    {
        // printf("for [%d]\n",i);
        // printf("pointer [%c]\n",*p);
    /**
     * Here strncpy() always copies the last 4 bytes of num,
     * which is { '0', '0', '0', '1', '0', '4' },
     * that's why pass is always "0104" instead instead of "104"
     */
        strncpy(pass, num+(6-digits), digits);
        // printf("pass: [%6.6s]\n", pass);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM