-1

I'm learning pointers in C and I have problems with copying pointers to a structure.

****Full code: https://1drv.ms/t/s!Av5xYBEjrdmuqS1sfgGB7pFCxGeu

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

struct cal {
    int date; 
    int time;
    int importance;
    char group[256];
    char title[256];
    char description[256];
};
long long int count = 0;


int main() {
    FILE *file;

    ......

    int *group;
    group = (int *)malloc(sizeof(int) * count);
    group = (int *)calloc(count, sizeof(int));
    struct cal *calendar;
    calendar = (struct cal *)malloc(sizeof(struct cal)*count);
    calendar = (struct cal *)calloc(count, sizeof(struct cal));


    for (int i = 0; i < count; i++) {
        char *ptr = strtok(arrs[i], " ");
        while (ptr != NULL) {
            calendar[i].date = 'ptr';
            ptr = strtok(NULL, " "); //date
            calendar[i].time = 'ptr';
            ptr = strtok(NULL, " "); //time
            calendar[i].importance = 'ptr';
            ptr = strtok(NULL, " "); //importance
            *calendar[i].group = 'ptr';
            ptr = strtok(NULL, " ");//group END
            *calendar[i].title = 'ptr';
            ptr = strtok(NULL, " ");//Title
            *calendar[i].description = 'ptr';
            ptr = strtok(NULL, " ");//Discription
        }
    }
    for(int i=0;i<count;i++) 
        printf("%d  %d %d %s  %s %s\n", calendar[i].date, calendar[i].time,
               calendar[i].importance, calendar[i].group, calendar[i].title,
               calendar[i].description);

}

input has to be on input.txt. like this:

YYYYMMDD HHmm [importance] [Group(string)] [title] [discription]  (\n)
YYYYMMDD HHmm [importance] [Group(string)] [title] [discription]  (\n)
YYYYMMDD HHmm [importance] [Group(string)] [title] [discription]  (\n)
YYYYMMDD HHmm [importance] [Group(string)] [title] [discription]  (eof)

Output has to be the sum of the same groups from group 0-4. ex) 1 0 1 2 0

in the

for (int i = 0; i < count; i++) {
            .....
        }

Part is not working. the pointer 'ptr' does not copy to 'calendar[i].somthing'. I've tried using calendar[i].date = '*ptr'; but it is also not working.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
krrrr0
  • 29
  • 2
  • 5
  • 4
    = 'ptr' -- this is wrong – purec Jun 03 '18 at 08:03
  • Your program has memory leaks and few other issues. Can you post the full code? – Karthick Jun 03 '18 at 08:16
  • My full code is https://1drv.ms/t/s!Av5xYBEjrdmuqS1sfgGB7pFCxGeu – krrrr0 Jun 03 '18 at 08:49
  • 2
    Why are you allocating memory using `malloc`, then loosing the pointer to it (`group` and `calendar`) by immediately allocating more memory using `calloc`? Those are memory leaks. `calendar[i].date = 'ptr';` is a syntax error, and that's not the way you copy integers (or strings). – cdarke Jun 03 '18 at 08:58
  • 1
    Where did you get this? It looks like draft. Where you can see 'ptr' you supposed to replace it with real code. For example calendar[i].date = atoi(ptr); – purec Jun 03 '18 at 08:58
  • 1
    What is `arrs`? When I tried to compile your code I got 9 warnings and 1 error (`arrs`), please supply a minimum example of your issue that we can compile. – cdarke Jun 03 '18 at 09:05

1 Answers1

0

Park, you will probably need to clean these portions of your code up and perhaps edit with your new code to make it clear what you are trying to accomplish. For starters, let's look at the responsibilities you have when allocating memory dynamically,

There is no need to cast the return of malloc, it is unnecessary. See: Do I cast the result of malloc?

In any code you write that dynamically allocates memory, you have 2 responsibilities regarding any block of memory allocated: (1) always preserve a pointer to the starting address for each block of memory you allocate so, (2) it can be freed when it is no longer needed.

int *group;
group = (int *)malloc(sizeof(int) * count);
group = (int *)calloc(count, sizeof(int));

Have you preserved a pointer to each block you have allocated? (hint: no). After your first allocation of group, it holds the first-address within the block you have allocated with malloc. Everything is OK to this point.

Then you call calloc assigning a new address to group. What happened to the old address held by group? The one you were required to preserve to insure that block of memory could be freed with no longer needed. How are you going to get that address back (hint: you can't its gone... This is what is known as a memory leak.

If you are making the 2nd call with calloc because you want all bytes in the block you allocate initialized to zero, then don't use malloc, just use a single call to calloc. There is never any reason whatsoever to both malloc and calloc the same pointer -- none.

You do the exact same thing with calendar. Since you do the exact same thing, the same bad results follow.

All you need is:

group = calloc (count, sizeof *group);

and then

calendar = calloc (count, sizeof *calendar);

This all begs the question -- why are you allocating with malloc and calloc at all? You can simply create an integer Array with automatic storage, and an instance of calendar in the same way. (no allocations required). That seems like it would be a much better approach for you at this point. There is nothing wrong with dynamic allocation, and you will use it quite a bit, but given your current difficulties with the concept, you may want to leave it for a time later and simply use the automatic storage provided for arrays and structs. (this makes even more sense given that you have nothing within calendar that requires further allocation)

Finally, with your use of strtok (or any function you use that is critical regarding input to your program) -- you must check the return every time you call it, otherwise you can have no confidence that you are processing actual data (and not garbage) in your code.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85