![]() |
| | #1 |
| Registered User Join Date: Aug 2006
Posts: 61
| fscanf with pointer of strings *string[] I am reading a file that contains both numbers and strings; by reading it sequentially, everything goes well when reading floats and assigning their values to an array previously declared, BUT, when I read strings and try to assign them to a pointer defined as Code: const char *problem[2]; Code: for(i=0; i<2; ++i ) problem[i] = (char*) malloc(12 * sizeof(char)); The error is avoided ONLY if I assing the string to simple strings instead of the elements of a array of strings. Could anyone help? Code: //Declare:
char header[12];
const char *problem[2];
//Allocate:
for(i=0; i<2; ++i )
problem[i] = (char*) malloc(12 * sizeof(char));
//Read file:
file_ID = fopen(input_file, "r");
fscanf(file_ID, "%s %s %s\n", header, &problem[0], &problem[1]);
printf(" Verify problem 0: %s\n", problem[0]);
printf(" Verify problem 1: %s\n", problem[1]);
the file to be read is the following: Code: problem: problem_name problem_type All the best S.M. |
| simone.marras is offline | |
| | #2 |
| Registered User Join Date: Aug 2006
Posts: 61
| I solved it in the following way, although I would have liked for the pointers to be assgigned the strings directly through fscanf: Code: fscanf(file_ID, "%s %s %s\n", header, prob, prob_type); strcpy(problem[0], prob); strcpy(problem[1], prob_type); S.M. |
| simone.marras is offline | |
| | #3 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| This will work: Code: fscanf(file_ID, "%s %s %s\n", header, problem[0], problem[1]);
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 03-21-2009 at 10:48 AM. |
| MK27 is offline | |
| | #4 |
| Registered User Join Date: Aug 2006
Posts: 61
| |
| simone.marras is offline | |
| | #5 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #6 |
| Registered User Join Date: Aug 2006
Posts: 61
| Thank you S. |
| simone.marras is offline | |
| | #7 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| However, it is equally true if problem[0] were a pointer to a regular char; you do need the address of operator when passing a pointer to a scanf function. You only need the address of if you are passing a variable which is not a pointer, such as an int. This can lead to confusion because you CAN use &charptr with scanf and it will work, but that is not the way you SHOULD have been doing it.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #8 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| only because pointer to array and pointer to the first element of the array contain same address you can sometimes get through with this incorrect code. when charptr is really a pointer its address is different from its cotents... that's why the OP's code crashed
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
![]() |
| Tags |
| *string[], file, fscanf, pointer, strings |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help.. confusion.. passing multi-array to pointer (incompatable pointer type) | LeeDavies | C Programming | 1 | 03-24-2008 10:16 AM |
| Direct3D problem | ahluka | Game Programming | 10 | 04-09-2006 03:36 AM |
| How did you master pointers? | Afrinux | C Programming | 15 | 01-17-2006 08:23 PM |
| pointers | InvariantLoop | C Programming | 13 | 02-04-2005 09:32 AM |
| Struct *** initialization | Saravanan | C Programming | 20 | 10-09-2003 12:04 PM |