为什么函数getline的第一个参数的指针,指针"焦炭** QUOT;而不是" char *之"? [英] Why is the first argument of getline a pointer to pointer "char**" instead of "char*"?

查看:267
本文介绍了为什么函数getline的第一个参数的指针,指针"焦炭** QUOT;而不是" char *之"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

函数getline 的原型是:

ssize_t getline(char **lineptr, size_t *n, FILE *stream);

我用这个作为测试程序,它从 HTTP GET:// 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;
}

这工作得很好。

我的疑惑是谁?


  1. 为什么要用的char ** lineptr 而不是的char * lineptr 作为函数的参数函数getline

  1. Why use char **lineptr instead char *lineptr as a parameter of function getline?

为什么这是错误的,当我使用下面的code:

Why it is wrong when I use the following code:

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


  • 我很困惑与 * &放大器;

    下面是警告的一部分:

    testGetline.c: In function ‘main’: 
    testGetline.c:34: warning: pointer targets in passing argument 2 of  
      ‘getline’ differ in signedness 
    /usr/include/stdio.h:671: 
      note: expected ‘size_t * __restrict__’ but argument is of type ‘int *’  
    testGetline.c:40: warning: passing argument 1 of ‘putchar’ makes integer 
      from pointer without a cast 
    /usr/include/stdio.h:582: note: expected ‘int’ but argument is of 
      type ‘char *’
    

    我用GCC 4.4.5版(Ubuntu的/ Linaro的4.4.4-14ubuntu5)。

    I use GCC version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5).

    推荐答案

    由于函数getline()如果你在一个指针传递给一个空指针分配内存为你

    Because getline() will allocate the memory for you if you pass in a pointer to a null pointer.

    手册页

    函数getline()读取一整行
      流,存储的地址
      包含文本到缓冲区
      * lineptr。缓冲区是空值终止,并包括
      换行符,如果被发现了。

    getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr. The buffer is null-terminated and includes the newline character, if one was found.

    如果* lineptr为NULL,则函数getline()
      将分配一个缓冲区用于存储
      线,这应当由被释放
      用户程序。 (在这种情况下,该
      在* N值被忽略。)

    If *lineptr is NULL, then getline() will allocate a buffer for storing the line, which should be freed by the user program. (In this case, the value in *n is ignored.)

    您需要在字符传递** (即指针的指针为char),这样的功能是能够更新<$值C $ C>的char * 它指向的。

    You need to pass in a char** (ie a pointer to a pointer to a char) so that the function is able to update the value of the char* that it points to.

    您也可以使用:

    char *my_string = NULL;  // getline will alloc
    
    puts("Please enter a line of text");
    
    bytes_read = getline(&my_string, &nbytes, stdin);
    

    不要忘了,如果你这样做你负责免费() -ing通过分配函数getline)内存(

    这篇关于为什么函数getline的第一个参数的指针,指针&QUOT;焦炭** QUOT;而不是&QUOT; char *之&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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