Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to learn C. I want to fill a 2D array from a 1D array that I have got from a File.

Now, I am trying to take the elements of array pointer that outputs (Hello,My,name,is,Ram.) into a 2D array called words. the purpose is to remove commas and highlight each word in a different row. I have achieved that too However I am getting some gibberish along with my output. I have checked my entries one by one and found that the first line(Hello,My,name,is,Ram.) is stored in from row 1 to 5 in words[][]. the values contained in words[][] between row 5 to 10 is gibberish. However, the next line(I,own,20,thousand,bucks.) starts at row [10] of word that I don't understand why? plus my output looks weird with some unknown values. Please inform me what should I do to correct my output and size the 2 D array equal to the value it contains

Here's my code:
int main(int argc, const char * argv[]) {
    // insert code here...
    FILE *fp;
    char (*points)[50];
    char *array;
    int width=20,height=50;
    char *word;
    char words [width][height];
    int counter=0; // To traverse through array and tracks the current position in array.
    points = malloc(sizeof(*points) * 2);

    word=malloc(width*height*sizeof(char));
    if (points == NULL) {
        perror("malloc");
        exit(0);
    }
    fp = fopen("/Users/shubhamsharma/Desktop/data.txt", "r");
    if (fp == NULL) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }
    fgets(points[0], sizeof(*points), fp);
    fgets(points[1], sizeof(*points), fp);
    array=points[0];
    printf("%s", points[0]);
    printf("%s", points[1]);

    for(int i=0;i<width;i++)
    {
        for(int j=0,p=counter;j<height;j++,p++)
        {
            if(array[p]==','||array[p]=='\0')
            {
                words[i][j]='\n';
                counter=++p;
                break;
            }
            else
                words[i][j]=array[p];

        }}

    printf("\n%c",words[16][0]);
    for (int i=0; i<width; i++) {
        for (int j=0; j<height; j++) {

            if(words[i][j]=='\n')
            {
                break;
            }
            printf("\nPrinting element in word\n");
            printf("%c",words[i][j]);

        }
    }

    printf("\n");
    fclose(fp);

    free(points);
    return 0;
    return 0;
    }


Output:

Hello,My,name,is,Ram. I,own,20,thousand,bucks. Printing element in word
HelloMynameisRam.+̮\213\377\310`\267t\377̮\213\377Iown20thousandbucks.
Program ended with exit code: 0

Lines in my file looks something like.-

Hello,My,name,is,Ram.
I,own,20,thousand,bucks.
Posted
Comments
[no name] 24-Jan-23 5:01am    
I don't see how the code provided ever compiled much less ran without segfault before any output.

char (*points)[50] is an array of 50 8 byte ptrs to char.

malloc sizeof(*points) * 2, which is 16 bytes because *points is an address.

No cast of void ptr from malloc but malloc tries to do the right thing and gives an address to a 400 byte block of memory for 50 ptrs, 8 bytes each.

Allocate memory for 'word', 1000 bytes, and never use it.

Use fgets to read sizeof(*points) which is 8 bytes, so fgets reads 7 bytes
and adds a null byte back into points[0] which is a ligit address.

Next fgets reads 8 more bytes and trys to put it in points[1] but points[1] is not an address to the malloc'd memory and segfault occurs.

lots is wrong but whats the point?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900