[SOLVED] How to tell what to do with a man page?
by jmgibson1981 from LinuxQuestions.org on (#6KPRK)
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 ??
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 ??