0

I have submitted the code please help me. The program is about creating a string data structure for containing dictionary only for alphabet a.

[   #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    char *string[1000];
    /*This program is about creating a string data structure 
     *for 'a' alphabet only.
     */
    main()
    {
        char buffer[50];
        int i=0,size = 0;
        FILE *fp = fopen("A_dict.txt","r");
        while(fgets(buffer,1000,fp)!=NULL)
        {
            string[size] = (char *)malloc(strlen(buffer)+1);
            strcpy(string[size],buffer);
            size = size + 1;
        }
            string[size] = NULL;
        printf("the size of string array is %d\n",size);
        printf("The content are:\n");
        for(i = 0; i < size; i++)
        {
            puts(string[i]);
        }
        fclose(fp);
    }]

These is the error that I'm getting -

enter image description here

GAURANG VYAS
  • 689
  • 5
  • 16
Abbas Ali
  • 11
  • 1
  • 2
    You have an array `buffer` of 50 characters, then you allow `fgets` to read up to 999 characters into it? Yes that's definitely going to lead to bad things and undefined behavior if the input is more than 49 characters (including possible newline). – Some programmer dude Jul 03 '17 at 13:09
  • 4
    Stop trying to put 999 characters into a 50 character buffer would be a start. [`man fgets`](https://linux.die.net/man/3/fgets). – Oka Jul 03 '17 at 13:09
  • Don't post images of text. And follow [ask]. – too honest for this site Jul 03 '17 at 13:33

0 Answers0