![]() |
| | #1 |
| Registered User Join Date: Jan 2004
Posts: 6
| I have been having a big problem passing pointers between functions and really need some help! My program consists of a main function which calls other functions I've defined in a header file. When the program runs, main calls a function to set up an array of struct pointers. Each element in the array is assigned a particular struct (hash table). If there is a collision (more than one struct is assigned to a particular element of the array), a linked list is constructed, where the first struct points to the next in the list, and so on. When the array is finished, it looks something like this (cut down example): ARRAY 0 ----> struct ----> struct 1 ----> struct 2 ----> struct ----> struct ----> struct 3 ----> struct ----> struct 4 ----> struct ----> struct ----> struct In main, I have initialised a structure (p_car_data) which contains a pointer of type struct pointer (i.e. it's a pointer to a pointer of type struct), called mp_car_table. Main passes this structure to the first function, and when the array is complete, as above, it makes the pointer point at the array: PHP Code: PHP Code: I'm not sure why it's doing this, maybe I've made some mistake with pointers... Can anyone help? Thanks in advance Paul P.S. I have to stick with this structure (passing a structure containing the array pointer to the function, etc), as this is one part of a much larger program, and it's too late to turn back and change things... Below (and also attached) is a small program which simulates the problem (the real program is too big to post here): The example uses structs which represent cars, with make, model and value as the members. PHP Code: |
| heygirls_uk is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,661
| > p_car_data->mp_car_table = car_hash_table; This is the real problem - car_hash_table is a local array, which means it no longer exists when the function exits. Code: static car_t *car_hash_table[10] = {NULL};
Code: strncpy(s_make, "Ford", 4);
s_make[4] = '';
Besides, in these cases, you may as well just use a regular strcpy() > p_car_data = (data_return_t *)calloc(1, sizeof(data_return_t)); Casting the result of malloc generally a bad idea in C - perhaps you're using a C++ compiler and don't know it. See the FAQ You also don't include stdio.h, for prototyping printf() One more point, please use the code tag, not the php tag when posting code. |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Jan 2004
Posts: 6
| Many thanks for your help - I've got it all working now. As regards the following statement: s_make[4] = ' '; This was supposed to read: s_make[4] = '\0'; /* null character */ For some reason all NULL characters were removed from my post. I usually assign strings using strncpy to prevent buffer overflow... I always thought that if you fill the string up including the last element, there won't be room for the NULL character, so the string will terminate whenever the first NULL character is reached in memory. Do I need to insert the NULL character when using calloc to allocate a string dynamically, or is the NULL character inserted automatically? And can it be overwritten using strcpy, or will strcpy always stop when it reaches the NULL? Thanks again, Paul |
| heygirls_uk is offline | |
| | #4 |
| Deleted Account Join Date: Jan 2004
Posts: 40
| calloc zeros all the memory that is allocated, I believe so there is no need to null-terminate (unless you are shortening the string) as it has already been done for you. |
| Brian2 is offline | |
| | #5 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,661
| > I usually assign strings using strncpy to prevent buffer overflow.. In which case, you should be doing strncpy(s_make, "Ford", sizeof s_make); s_make[sizeof s_make -1] = '\0'; > For some reason all NULL characters were removed from my post. Yeah, you used php tags instead of code tags > And can it be overwritten using strcpy, or will strcpy always stop when it reaches the NULL? strcpy() doesn't care about whether you use calloc or malloc. All it cares about is havinf a nul character in the source string. |
| Salem is offline | |
| | #6 |
| Registered User Join Date: Jan 2004
Posts: 6
| Thanks for clearing that up, it makes much more sense now Paul |
| heygirls_uk is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Passing Pointers or References? | leeor_net | C++ Programming | 24 | 02-04-2009 02:29 PM |
| passing pointers in nested functions | MK27 | C Programming | 6 | 12-04-2008 03:18 AM |
| Passing lists to functions | Griever | C Programming | 23 | 11-14-2008 05:26 PM |
| Passing Pointers to Functions question.. | transgalactic2 | C Programming | 7 | 10-18-2008 03:51 PM |
| Passing variables to functions (was: Problem with functions) | OmniMirror | C Programming | 6 | 05-06-2003 03:08 PM |