1

Don't mind my code writing style. I specifically created this for testing purposes...

Now to the problem:

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

#define EMB 31
#define NAME_MAX 50

struct TRIP {

    char TRIP_NAME[EMB];
    int TRIP_TIME;

};

struct DATE {
    int day;
    int month;
    int year;
};

struct TRIP_INFORMATION {

  char TRIP_NUMBER[EMB];
  char EMBARKATION_POINT[EMB];
  char SPECIFIC_DROPOFFPOINT[EMB];
  char EXIT_DROPOFFPOINT[EMB];
  struct DATE TRIP_DATE;
  struct TRIP SPECIFIC_TRIP;

};

struct EMBARKATION_CARD{

  //struct DATE TRIP_DATE;
  char NAME[NAME_MAX];
  int ID_NUMBER;
  int PRIORITY_NUMBER;
  //int TRIP_TIME;
  //char EMBARKATION_POINT[EMB];
  //char DROPOFFPOINT[EMB];

  struct TRIP_INFORMATION TRIP_INFORMATION;

};

This is for the reference of declaration.

int BeginEmbarkationProcess(int *PASSENGER_COUNT, struct EMBARKATION_CARD * PASSENGER_TO_SAVE, int curr_day, int curr_month, int curr_year){

    //struct EMBARKATION_CARD * P;
    if(*PASSENGER_COUNT>1){
        PASSENGER_TO_SAVE = realloc(PASSENGER_TO_SAVE, *PASSENGER_COUNT * sizeof(struct EMBARKATION_CARD));
        if(PASSENGER_TO_SAVE == NULL){
            puts("PASSENGER_TO_SAVE VARIABLE = HAS NOT ALLOCATED MEMORY");
            return -1;
        }
    }

    if(PASSENGER_TO_SAVE==NULL){
            puts("PASSENGER TO SAVE POINTER HAS UNABLE TO ALLOCATE MEMORY");
        return -1;
    }

    int x = 0;


    for(x=0;x<*PASSENGER_COUNT;x++){
        ((PASSENGER_TO_SAVE+x))->ID_NUMBER = (x+1)*30;
        ((PASSENGER_TO_SAVE+x))->PRIORITY_NUMBER = (x+1)*17;          
    }


    for(x=0;x<*PASSENGER_COUNT;x++){           
    printf("%d %d\n", (PASSENGER_TO_SAVE+x)->ID_NUMBER , (PASSENGER_TO_SAVE+x)->PRIORITY_NUMBER);
    }



    *PASSENGER_COUNT = *PASSENGER_COUNT + 1;

    int r;
    printf("ENTER -1 TO TERMINATE THIS LOOP\n");
    scanf("%d", &r);
    return r;
}

int main(){

    //doIt();
    struct EMBARKATION_CARD* E = malloc(sizeof(struct EMBARKATION_CARD));
    int ct = 1;
    int s = BeginEmbarkationProcess(&ct, E, 3, 3, 2020);


    while(s!=-1){
        s = BeginEmbarkationProcess(&ct, E, 3, 3, 2020);

    }

    return s;

}

Since I copy pasted this (and removed some commented out lines but eventually got tired of it), this copy pasted code might have some syntax error. Ignore those syntax error please.

The issue is that realloc WILL keep returning NULL. This prevents me from readjusting it.

Can someone tell me what the hell is going on. I know I may have made some errors here but I want to learn about it.

Yes I am just a student learning C language.

Vlavlad
  • 11
  • 2
  • `P = realloc` and `if(PASSENGER_TO_SAVE == NULL)` isn't correct. You need to check/use `P` not `PASSENGER_TO_SAVE`. `realloc` returns the pointer to the relloced memory. It can't (and doesn't) change the pointer you pass in. – kaylum Mar 03 '20 at 09:09
  • I don't see that you check the return value of `realloc` which is `P`. You compare the input pointer `PASSENGER_TO_SAVE` with `NULL`. – Bodo Mar 03 '20 at 09:10
  • Oshhh wait, I think I forget to replace P with PASSENGER_TO_SAVE. Changed it because of alternative options. P was originally PASSENGER_TO_SAVE in P = realloc while I was writing this problem. Even so, I still ran the code and still got the same problem. – Vlavlad Mar 03 '20 at 09:13
  • Please remember that C passes function parameter via copying it. Whatever you do inside your function, has no effect to the variable passed: `struct EMBARKATION_CARD * PASSENGER_TO_SAVE` You cannot change `PASANGER_TO_SAVE`inside your function. This means after first call to `realloc` your `E` in `main` may no longer be valid. – Gerhardh Mar 03 '20 at 09:18
  • I will recode it and see for itself later. Thanks for the quick reply and I learned something new today. – Vlavlad Mar 03 '20 at 09:23
  • BTW untelated to your problem: don't use all caps identifiers: `PASSENGER_COUNT` -> `passenger_count`. All caps identifiers are usually used for macros only. – Jabberwocky Mar 03 '20 at 09:36

0 Answers0