1

So following up on a previous question i asked Dynamic arrays clarification in C the code works fine exept when I try to print the elements, all the elements are printed correct exept the 3rd element weirdly enough even when i change the input file always the 3rd element ir printet wrong. This are the print results: host0host1♥host3. Thanks for any help in advance! Updated to a complete code with the results again beeing host0host1♥host3, This is the file that i read from:

0 host0
1 host1
2 host2
3 host3
4 host4

Code:

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

int total = 0;

struct names
{
    int number;
    char *name;
};

void readNames(FILE *fpN, struct names **name)
{
    char sentence[512];
    struct names *local_name = *name;
    while (fgets(sentence, sizeof(sentence), fpN))
    {
        local_name = realloc(local_name, total + 1 * sizeof(*local_name));

        char *token = strtok(sentence, " ");    // grab the number
        local_name[total].number = atoi(token); // add it to the array
        token = strtok(NULL, " \n");

        local_name[total].name = malloc(strlen(token) + 1);
        strcpy(local_name[total].name, token);

        total++;
        // printf("TOTAL:%d",*total);

        // local_numbers = (int*)realloc(local_numbers,(total+1)*sizeof(int)); //allocate more space for the int array
        // local_names = (char**)(realloc(local_names, (total+1)*sizeof(char*)));  //allocate more space for the string array
        // printf("%d", sizeof(names));
    }
    *name = local_name;
    printf("%s", local_name[0].name);
    printf("%s", local_name[1].name);
    printf("%s", local_name[2].name);
    printf("%s", local_name[3].name);
}

int main(int argc, char **argv)
{
    FILE *fpN = NULL;
    FILE *fpG = NULL;

    if (argc != 2)
    {
        printf("Wrong arguments");
        exit(0);
    }

    if ((fpN = fopen(argv[1], "r")) == NULL)
    { // check if the file exists
        printf("File not found!");
        exit(0);
    }

    struct names *name = NULL; // Allcation will be done later
    readNames(fpN, &name);
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Begin by creating a [mre] to show us, which includes a sample `main` function which calls your `readNames` function. Then when you copy-paste it into your question make sure it's well-formatted with consistent indentation. – Some programmer dude Oct 08 '22 at 17:33
  • What does `name` point to on inception of this function? It *must* either be pointing to a null pointer, or a hot pointer acquired via dynamic allocation. Live example data and a proper [mcve] would answer that question, a ton of others, and make this properly answerable. – WhozCraig Oct 08 '22 at 17:40
  • Sorry i am new to stack overflow i will update the post – George Pettemeridis Oct 08 '22 at 17:42
  • I updated the post sorry for the inconvenience again! – George Pettemeridis Oct 08 '22 at 17:48
  • 1
    In your case I would suggest something like `sscanf(sentence, "%d %s", &local_name[total].number, local_name[total].name)` instead of the more complicated parsing with `strtok` (but then you need `name` to be an array and not a pointer). – Some programmer dude Oct 08 '22 at 17:55
  • You should probably also make the `readNames` function return `total`, otherwise the caller will not know how many elements there are in the array. – Some programmer dude Oct 08 '22 at 17:56
  • By doing the sscanf would i need to realloc and malloc? Also the total is a public variable because i use it in the entire program i am writing so i declared it as public. – George Pettemeridis Oct 08 '22 at 17:58
  • 1
    `total + 1 * sizeof(*local_name)` should be `(total + 1) * sizeof(*local_name)` on your `realloc` call. That is *very* likely the root of your evil. – WhozCraig Oct 08 '22 at 17:58
  • My guy @WhozCraig you are right, i have been looking at the code for the past hour and did not see that stupid mistake! – George Pettemeridis Oct 08 '22 at 18:04
  • The printing tag is irrelevant here, btw. – mmixLinus Oct 08 '22 at 20:33

0 Answers0