jackjop jackjop - 1 year ago 125
C Question

How to print char **

I have a method that read the file and assign the lines to char **. But when I try to print its first element it prints random characters. What may be wrong?

int main()
char **lines_from_file = readFromFile(file_name);
return 0;

void obeyCommands(char **command_lines)
printf("%s", command_lines[0]);

char** readFromFile(char *file_name)
int size;
int c;
char *buffer;
char** all_lines= (char**)malloc(sizeof(char*));

FILE *f = fopen(file_name, "r");
int i=0;
do // read all lines in file
size = 0;
buffer = (char *)malloc(size+1);
do // read one line
c = fgetc(f);
if(c != EOF) buffer[size++] = (char)c;
buffer = (char*)realloc(buffer, size+1);
while(c != EOF && c != '\n');

*(all_lines+i) = (char*)malloc(sizeof(buffer));
**(all_lines+i) = *buffer;
all_lines = (char**)realloc(all_lines, sizeof(char*) * i);

while(c != EOF);
return all_lines;

Answer Source

Problem 1

A problem is in the line:

        **(all_lines+i) = *buffer;

That copies just a single character from buffer. As a consequence, none of the strings in all_lines is null terminated.

You should replace the lines:

        *(all_lines+i) = (char*)malloc(sizeof(buffer));
        **(all_lines+i) = *buffer;


        all_lines[i] = buffer;

Problem 2

The next problem I see is in the line:

        all_lines = (char**)realloc(all_lines, sizeof(char*) * i);

In the first iteration of the loop, i is 1. Hence, that line is the same as:

        all_lines = (char**)realloc(all_lines, sizeof(char*));

which is not what you want. You want

        all_lines = (char**)realloc(all_lines, sizeof(char*) * 2);

since you will be using all_lines[1] in the next iteration. That lines needs to be:

        all_lines = (char**)realloc(all_lines, sizeof(char*) * (i+1));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download