0

I’m trying to initialize a char *tab[] and this causes segmentation fault, what am I doing wrong?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
int main()
{
    char **cle_par = NULL;
    cle_par = (char**) calloc (22, sizeof (char *)+1);
    int y=0;
    for (y=0; y<22; y++);
    {
        cle_par[y]=(char *) malloc(45*sizeof(char));
    }
    strcpy(cle_par[0], "this_is_a_test");
}

1 Answers1

1

Your code has a "typo" error which invalidates the for loop and makes it an empty loop.

for (y=0; y<22; y++);, the trailing semicolon makes the loop empty, and I believe it's not your intention. If you remove it. Your code should work fine.

stensal
  • 401
  • 4
  • 8