Hi I'm trying to write a simple version of the tail program in linux, but I keep getting a seg fault when I run the program. I wrote a simpler version but with fixed number of lines to print and using

char lines[10][100];

instead of

char **lines;

and it was working fine.

I have the program reading form stdin for the moment and i will add the ability for n to be choosen from the command line

If anyone can point out where i'm going wrong i would really appreciate it.
Thank you again.

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#define MAXLINE 100

void allocate2d( char **p,int x,int y);

int i = 0;
int j = 0;
char **lines;

int main(void) {

        int n = 10;

        allocate2d(lines,n,MAXLINE);
        
        while(fgets(lines[i],100,stdin)) {
                ++i;
                i %= n;

        }

        --i;

        for(j= 0;j<= n;j++) {

                printf("%s",lines[i]);
                ++i;
                i %= n;
        }

        return 0;

}

void allocate2d(char **p,int x,int y) {

        int i = 0;
        printf("%d\n",sizeof(*p));

        p = (char **) malloc(x*sizeof(*p));
        if(p != NULL) {
                for (i = 0;i <= x; i++) {
                        if(!(p[i] = (char*) malloc(y*sizeof(char)))) {
                                perror("Malloc");
                                exit(-1);
                        }
                }
        }
        else {
                perror("Maloc");
                exit(-1);
        }

}

If you want to change the value of a pointer in a called function, pass a pointer to a pointer.

#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 100

void foo(char **p,int x, int y)
{
    p = malloc(x * sizeof(*p));
}

void bar(char ***p, int x, int y)
{
    *p = malloc(x * sizeof(**p));
}

int main(void)
{
    char **lines = 0;
    int n = 10;
    printf("lines = %p\n", (void*)lines);

    foo(lines, n, MAXLINE);
    printf("lines = %p\n", (void*)lines);

    bar(&lines, n, MAXLINE);
    printf("lines = %p\n", (void*)lines);

    /* free() stuff */
    return 0;
}

/* my output
lines = 00000000
lines = 00000000
lines = 003E2460
*/

Thank you very much for yor help, I change the code as advised and I got the allocation if the working now(i think), but I still get a segfault it seems when fgets gets called. I am passing the pointer in a wrong manner to the function fgets. Thank you very much again!!

oh the the modified code is:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#define MAXLINE 100

void allocate2d( char ***p,int x,int y);

int i = 0;
int j = 0;
char **lines;

int main(void) {

        int n = 10;

        /* DEAL WITH OPTIONS */

        allocate2d(&lines,n,MAXLINE);

        while(fgets(lines[i],99,stdin)) {
                ++i;
                i %= n;

        }

        --i;

        for(j= 0;j<= n;j++) {

                printf("%s",lines[i]);
                ++i;
                i %= n;
        }

        return 0;

}

void allocate2d(char ***p,int x,int y) {

      int i = 0;

        *p =  malloc(x*sizeof(**p));
        if(*p != NULL) {
                for (i = 0;i <= x; i++) {
                        if(!((*p)[i] = (char*) malloc(y*sizeof(char)))) {
                                perror("Malloc");
                                exit(-1);
                        }
                }
        }
        else {
                perror("Maloc");
                exit(-1);
        }
        

}

Never mind. it seems to be compiling now.
Thank you very much again.

Cheers

This question has already been answered. Start a new discussion instead.