How do I pass the address of the first char of a string to a function write to it?

2

The problem is this: I made a function that takes the output of a certain command from the OS and stores it in a string. The idea would now be to declare a string of just one char in my main function using malloc , call my function by passing the command I want to take the output and also passing the address of the byte that was allocated to my char. From this, I would expand my string by initially 1 char using realloc inside the other function to store the values that fscanf returns directly in those spaces.

How could this be done?

Code sample:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <stdint.h>

int SystemGetOut_char();


int main()
{
    char *teste = malloc(1);
    char command[] = "ls";
    SystemGetOut_char(command, &teste);
    return 0;
}


int SystemGetOut_char(char *command, char *output) 
{
    int chunk = 1;
    FILE *fp = popen(command, "r");
    int charnumber = 0;
    while(fscanf(fp, "%c", &output[charnumber]) != EOF)
    {
        chunk++;
        output = realloc(output, chunk);
        charnumber++;
    }
    pclose(fp);
    return 0;
}

NOTE: I know the code will not work, just to get an idea of the structure.

    
asked by anonymous 21.06.2018 / 01:24

1 answer

6

In fact the code you have is very close to working, needing only a few adjustments.

You are already passing the address of string to the function SystemGetOut_char :

SystemGetOut_char(command, &teste);
//                     ----^

It will allow you to change the string within the function, however the type specified in the function parameter is not correct:

int SystemGetOut_char(char *command, char *output) 
//                                --------^

You can think in generic terms. If it has a char* and passes its address, then it gets a char** that should be exactly that type of the second parameter. This means that your function has to deal with a double pointer, and with the changes that this implies in the rest of the operations.

Your role with these changes looks like this:

int SystemGetOut_char(char *command, char **output){
//          agora duplo ponteiro  --------^

    int chunk = 1;
    FILE *fp = popen(command, "r");
    int charnumber = 0;

    //aqui a posição de memoria onde guarda o char é dada por *output + charnumber
    while(fscanf(fp, "%c", *output + charnumber) == 1)
    {
        chunk++;
        *output = realloc(*output, chunk); //aqui com *output para ser o apontado
        charnumber++;
    }
    (*output)[charnumber] = '
int main()
{
    char *teste = malloc(1);
    char command[] = "ls";
    SystemGetOut_char(command, &teste);
    printf("%s", teste); // <----
    return 0;
}
'; //coloca o terminador no fim pclose(fp); return 0; }

Note that I changed the condition in while to while (fscanf(...) == 1) . fscanf returns the number of elements that you can assign, and in this case you can not assign 1 means it has come to an end and should stop.

I also did not have to save space for the terminator because the string already came with an available space, so it always has 1 more than it needs, and this caratere can be used more for the terminator.

Since% wc_width has already been changed within the function, just show normally in wc%:

SystemGetOut_char(command, &teste);
//                     ----^

Example run for the command string you have in the code, on my machine:

    
21.06.2018 / 02:07