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

Asked

Viewed 263 times

2

The problem is this: I made a function that takes the output of a given OS command and stores it in a string. The idea now would be to declare a single char string in my main function using malloc, call my function by passing the command I want to pick up the output and also passing the byte address that was allocated to my char. From that, I would go on expanding my string from initially 1 char using the realloc within the other function to store the values that the fscanf return directly to these spaces.

How could this be done?

Code example:

#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 won’t work, it’s just to get an idea of the structure.

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 for the function SystemGetOut_char:

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

That 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 general terms. If you have a char* and give your address, then you get a char** that this should be exactly the type of the second parameter. This means that your function has to move on to dealing with a double pointer, and the changes that this entails in the rest of operations.

Your function with these changes is 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] = '\0'; //coloca o terminador no fim

    pclose(fp);
    return 0;
}

Note that I changed the condition on while for while (fscanf(...) == 1). The fscanf returns the number of entries you can assign, and when you cannot assign 1 means that it has come to an end and must stop.

Also I didn’t have to save space for the terminator because the string already came with a space available, so it always has 1 more than it needs, and this character can be used for the terminator.

As the string changed already inside the function, just show normally in the main:

int main()
{
    char *teste = malloc(1);
    char command[] = "ls";
    SystemGetOut_char(command, &teste);
    printf("%s", teste); // <----
    return 0;
}

Execution example for the command ls that you have in the code, in my machine:

inserir a descrição da imagem aqui

  • Your reply was great. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.