LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to tell what to do with a man page? (https://www.linuxquestions.org/questions/programming-9/how-to-tell-what-to-do-with-a-man-page-4175735425/)

jmgibson1981 03-28-2024 07:25 PM

How to tell what to do with a man page?
 
Maybe this is a moron question. I'm not sure. I'm finding that as I try to use various things in the C standard lib + a few others that it is a bit of trial and error to get them to work. This is an example of my biggest problem.

From the manpage for getline
Code:

      #include <stdio.h>

      ssize_t getline(char **restrict lineptr, size_t *restrict n,
                      FILE *restrict stream);
      ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
                      int delim, FILE *restrict stream);

Yet this is the code that is needed to make it work. I wrapped it in a function for easy reuse.

Code:

char *
getline_stdin_mem_alloc(void)
{
  // declare & initialize
  size_t buflen = 0;
  char * buffer = NULL;
  if (getline(&buffer,
              &buflen,
              stdin) == -1) {
    free(buffer); buffer = NULL;
  }
  buffer[strcspn(buffer,
                "\n")] = '\0';

  return(buffer);
}

So I suppose my question is how do I know when I'm supposed to target the address of the pointer verses passing a pointer. I'm worried this will be stupidly obvious and I'll slap my forehead when someone tells me but I just can't see it, short of trial and error anyway.

Code:

&ptr vs *ptr
??

smallpond 03-28-2024 07:54 PM

Pass ptr when you need the value of the pointer. Pass *ptr when you just need the value that it points to. Pass &ptr when you need to modify the pointer in the function.

jmgibson1981 03-28-2024 09:00 PM

Must have missed it. Pointers still mess with my head. Thank you very much for explaining that.


All times are GMT -5. The time now is 09:04 AM.