C - How to properly pass a struct containing char arrays from one function to another
I am working on a c program to read in a file containing data on thousands of people into an array of PERSON structs, and then output that data however I may need it to. I am trying to read in the data from the file and save it into an array of PERSON structs in the function
Code:
char* input_data_from_file( char filename[], char file_format[], int individuals, int categories ) Code:
char* input_data_from_file( char filename[], char file_format[], int individuals, int categories ) Code:
#include <stdio.h> |
I am not sure I understand your question.
You probably are looking for the following language idiom: Code:
void f(SOME_TYPE *output_struct_ptr, <the_rest_of_args>) |
Sergei,
The example that you have shown is actually very much like what I wish to accomplish. It looks from your example, like you are understanding me correctly. I think your example has already given me enough of an answer to work with. A further question I have, though, is am I allowed by the C language to modify the variable that I pass to void f() as a pointer within the void f() function? For example, I would like void f() to read in data on thousands of people from my data file, and then add their data into SOME_TYPE *output_struct_ptr (which might be a pointer to an array of PERSON structs). Will C allow me to do this, and if so, what would be the proper syntax for passing an array of PERSON into the void f(*) function where you have used SOME_TYPE *output_struct_ptr as your example? Thanks! |
Quote:
By the way, the extended idiom is: Code:
ERROR_CODE_TYPE f(SOME_TYPE *output_struct_ptr, <the_rest_of_args>) |
Sergei, I found a way that appears to work, based off of the example that you gave. I had to change one thing, but other than that, it is very close to what your example does. The only thing I changed from your example is that I have to pass the actual array of PERSON structs itself as a parameter to the function, rather than passing a pointer to the array of person structs as a parameter. The reason for this is that if I pass it as a pointer, and then try to modify the contents of the PERSON structs within the array, I get a compiler warning that I am trying to modify something that is not a struct or a union. That is true, because if it is a pointer to an array of structs, then it is not a struct; it is a pointer. Passing it as itself rather than as a pointer to itself is the only thing I changed, though. Your example of a passing the original variable as a parameter to a void function, and modifying its contents that way, seems to be working a lot better than what I was doing, which was attempting to modify a local variable within the function and return it to assign it's contents to the original variable in main(). Thanks for your help! Here is how I changed the code, based upon your example, and it seems to work properly now:
Code:
#include <stdio.h> |
Quote:
Code:
sergei@amdam2:~/junk/pointer_to_array> cat -n f.c |
Yes, I see what you are saying. Is it better to pass it as a pointer?
|
Quote:
I am saying that pointer to an array lets the compiler know how many elements are in the array while regular pointer does not imply this knowledge. This knowledge is compile time only. |
Code:
#include <stdio.h> |
Quote:
In real programs I have separate files for typedefs and yet other separate files for function prototypes - the a latter #include the former. |
Yes, the lines in red do need to be outside of main(). I had them in main() temporarily, while I was experimenting with things to try to figure out a way to set the size of typedef PERSON PERSON_DATABASE based upon how many people are in the file that is read in. If you notice, I had the line typedef PERSON PERSON_DATABASE[4000] outside of main(), while I was experimenting with things, and that was actually the line of code that was making the program work, because the lines in red inside of main() were doing nothing. I had a very difficult time figuring out a way to set the size of PERSON PERSON_DATABASE variably, based upon how many people are read in from the data file. I ended up eventually finding a way that seems to work quite well though:
Code:
#include <stdio.h> |
All times are GMT -5. The time now is 12:05 PM. |