小编ct5*_*586的帖子

为什么getline的第一个参数指向指针"char**"而不是"char*"?

我用getline函数来读取一行STDIN.

原型getline是:

ssize_t getline(char **lineptr, size_t *n, FILE *stream);
Run Code Online (Sandbox Code Playgroud)

我使用它作为测试程序,来自http://www.crasseux.com/books/ctutorial/getline.html#getline

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

int main(int atgc, char *argv[])
{
    int bytes_read = 1;
    int nbytes = 10;
    char *my_string;

    my_string = (char *)malloc(nbytes+1);

    puts("Please enter a line of text");

    bytes_read = getline(&my_string, &nbytes, stdin);

    if (bytes_read == -1)
    {
        puts ("ERROR!");
    }
    else
    {
        puts ("You typed:");
        puts (my_string);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这很好用.

我的怀疑是?

  1. 为什么使用char **lineptr而不是char …

c gcc function

27
推荐指数
3
解决办法
6万
查看次数

如何使用正则表达式分隔字符串中的数字和字符,如"30M1000N20M"

我正试图将这些[0-9][A-Z]字符串分开:

100M
20M1D80M
20M1I79M
20M10000N80M
Run Code Online (Sandbox Code Playgroud)

我尝试使用Python re模块,以下是我使用的代码:

>>>import re
>>>num_alpha = re.compile('(([0-9]+)([A-Z]))+')
>>>str1="100M"
>>>n_a_match = num_alpha.match(str1)
>>>n_a_match.group(2), n_a_match.group(3)

100,M   #just what I want

>>>str1="20M10000N80M"
>>>n_a_match = num_alpha.match(str1)
>>>n_a_match.groups()

('80M', '80', 'M')  #only the last one, how can I get the first two?
#expected result ('20M','20','M','10000N','10000','N','80M','80','M')
Run Code Online (Sandbox Code Playgroud)

此正则表达式适用于仅包含一个匹配但不包含多个匹配组的字符串.如何使用正则表达式处理它?

python regex

5
推荐指数
1
解决办法
1824
查看次数

标签 统计

c ×1

function ×1

gcc ×1

python ×1

regex ×1