0
TRIP* ReadTravel(int* pnum_trips)
{
TRIP* tr = NULL;//initiallization for realloc
char stop = '0';
while (stop != STOP_INSERTING_TRIPS)
{
    tr = (TRIP*)realloc(tr, (1+ *pnum_trips) * sizeof(TRIP));
    tr[*pnum_trips] = ReadTrip();
    (*pnum_trips)++;
    printf("If you want to stop adding trips, type '%c'\n", STOP_INSERTING_TRIPS);
    printf("if you want to continue adding trips, type any other key\n");
    scanf(" %c", &stop);
}
return tr;
}

void main()
{
TRIP* trav;
int num_trips = 0;
int i;
trav = ReadTravel(num_trips);
PrintTravel(trav, num_trips);
}

TRIP ReadTrip()
{
TRIP tr;
printf("Enter the city you START your trip in: ");
scanf("%s", tr.starting_city);
printf("Enter the city you END your trip in: ");
scanf("%s", tr.destination_city);
    printf("Enter the starting date of the trip:\n");
tr.trip_start = ReadDate();
printf("Enter the arrival date of the trip:\n");
tr.arrival_date = ReadDate();
printf("Enter the price of the trip: ");
scanf("%lf", &tr.trip_price);
return tr;
}

TRIP is a struct in the code, let's say it works because it's not the reason i'm here. in the line with the "realloc" there seems to be a problem but i don't understand what. when i delete the "*pnum_trips" it seems to work okay, but i don't understand what is the problem with "*pnum_trips". help please Edit: i added main() and ReadTrip()

Or Baron
  • 1
  • 1
  • 2
    The code looks fine, assuming you're passing in a pointer to a zero int. It would be better to provide a minimal but complete example with a specific error. – Paul Hankin Feb 03 '21 at 16:25
  • I agree with @PaulHankin - I can't tell what's wrong with your code from your question; there is no error description. What doesn't work as expected? – Morten Jensen Feb 03 '21 at 16:26
  • `tr[*pnum_trips] = ReadTrip();` <<-- what does ReadTrip() return? – wildplasser Feb 03 '21 at 16:29
  • As a general rule of thumb, if the list might get large (for 'large' in the hundreds or more), it is better not to grow the list size by one each time as you can end up with a lot of copying that can grotesquely slow things down. It is better to double the size, but you have to keep track of the number of items allocated and the number used (reallocating when the number used equals the number allocated and you need to add another). – Jonathan Leffler Feb 03 '21 at 16:29
  • the code runs but in the debugging window it doesn't show anything, and when i remove the "*pnum_trips" in the realloc it works fine – Or Baron Feb 03 '21 at 16:31
  • @wildplasser it returs TRIP – Or Baron Feb 03 '21 at 16:33
  • What's the value in `*pnum_trips` in the function at the start? Are you sure you aren't overwriting memory when you omit `*pnum_trips`? – Jonathan Leffler Feb 03 '21 at 16:35
  • @JonathanLeffler yeah i know but it's a task for school – Or Baron Feb 03 '21 at 16:35
  • Unless the instructions say "increment the array by one entry each time", the fact that it's a task for school is largely irrelevant. The "general rule" type caveat lets you off the hook; it recognizes that it may not be relevant to you yet. – Jonathan Leffler Feb 03 '21 at 16:37
  • @JonathanLeffler the initial value is 0 and no' im not 100% sure im not overwriting memory im just a beginner.. – Or Baron Feb 03 '21 at 16:38
  • Also, the idiom `old_space = realloc(old_space, new_size);` is a recipe for memory leaks. If the allocation fails, `old_space` just got zapped with `NULL` and you can no longer free (or access) the previously allocated memory. Use `void *new_space = realloc(old_space, new_size); if (new_space == NULL) { …do appropriate error handling… } old_space = new_space; old_size = new_size;` or equivalent. – Jonathan Leffler Feb 03 '21 at 16:39
  • @JonathanLeffler actually it's exactly what im told to do – Or Baron Feb 03 '21 at 16:39
  • OK; it's simple, and may be excusable (though I'm not entirely happy about teaching bad habits). Note my comment for future reference, once you're out of school. – Jonathan Leffler Feb 03 '21 at 16:41
  • @JonathanLeffler they didnt teach us how to use void as a type the standard they ask for is pretty limited – Or Baron Feb 03 '21 at 16:42
  • 3
    My crystal ball tells me the variables whose address is received by `pnum_trips` in this function is indeterminate and you're utilizing that value without initialization in this code, without knowing it. That's the trouble with crystal balls, though. They often miss. So how about providing an actual [mcve]. I.e. this function, *and* a program that calls it, and said-program exhibits your problem. Said-program will need to know what `ReadTrip()` does, have a `main`, sample input data, all of which are utilized to reproduce. [Update your question](https://stackoverflow.com/posts/66031524/edit) – WhozCraig Feb 03 '21 at 16:42
  • If you're worried about me using `void *` instead of `TRIP *`, change `void *` to `TRIP *`. If you look at the man page for `realloc()`, it returns a `void *` — that's why I used the type. But you can use the type you're after (`TRIP *`) instead. There are those who would excoriate you for using a cast; I'm not one because I learned C on a machine (and at a time) when the cast was crucial — our biggest cause of crashes was not declaring `malloc()` correctly (`extern char *malloc();` at the time — before there was a C standard) or casting. See [SO 605845](https://stackoverflow.com/q/605845/). – Jonathan Leffler Feb 03 '21 at 16:47
  • @JonathanLeffler ok thank you i'll try it – Or Baron Feb 03 '21 at 16:48
  • `TRIP* trav;` -->> `TRIP *trav=NULL;` – wildplasser Feb 03 '21 at 16:50
  • Check the return values. `scanf(" %c", &stop);`returns `1` on success, `realloc` returns non`NULL` on success. Potentially, when `stop` is some value and `scanf` fails, your code will endlessly loop. – KamilCuk Feb 03 '21 at 16:51
  • @xing i love you! that was the missing part, thank you so much! – Or Baron Feb 03 '21 at 16:53
  • Your compiler ought to have warned you about that. If it did, don't ignore such warnings. If it did not, figure out how to increase its warning level, or get a new compiler. – Nate Eldredge Feb 03 '21 at 16:57
  • 2
    I'm glad your problem could be found, but for future reference, what you posted is still not a [mcve]. It doesn't compile by itself because of missing definitions, types, headers, etc. "let's say it works because it's not the reason i'm here" isn't good enough - many times the problem is not where you think it is, and even if not, the best way for people to be able to help find your problem is if they can actually test it, without having to guess at what to fill in. – Nate Eldredge Feb 03 '21 at 16:59

0 Answers0