strtol将重用参数 [英] strtol reusing param

查看:181
本文介绍了strtol将重用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code似乎按预期方式工作,填充使用单一指针数字数组

This code seems to work as expected, populates an array of numbers using a single pointer

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

int main(void)
{
    int arr[4], count = 0, i;
    char *p, s[32] = "  \t  10,  15  \n  ,20,   25  , ";

    p = s;
    do {
        arr[count++] = (int)strtol(p, &p, 10);
        while (isspace(*p) || *p == ',') p++;
    } while (*p);
    for (i = 0; i < count; i++) {
        printf("%d\n", arr[i]);
    }
    return 0;
}

我的问题是:

这是有效的为使用p作为参数1(源)和放大器; 2 P作为strtol将参数2(第一个无效字符的地址)

It is valid to use p as param1 (source) and &p as param 2 (address of the first invalid character) in strtol?

推荐答案

是的,它是安全的。

请参见 HTTP://en.cp$p $ pference.com / W / CPP /字符串/字节/ strtol将完整的使用参考。这个例子的第11行说明了使用相同的变量为第1和第2个参数的电话。

Please refer to http://en.cppreference.com/w/cpp/string/byte/strtol for complete usage reference. Line 11 of the example illustrates a call using the same variable for the 1st and 2nd parameters.

这篇关于strtol将重用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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