C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-21-2009, 10:04 AM   #1
Registered User
 
Join Date: Aug 2006
Posts: 61
fscanf with pointer of strings *string[]

Hi there,

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];
and allocated as

Code:
for(i=0; i<2; ++i )
problem[i] = (char*) malloc(12 * sizeof(char));
I obtain a segnemtation fault at the moment of reading the values.

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]);
I get a segmentation fault when printing it to file (if I comment print I dont)

the file to be read is the following:
Code:
problem:	 problem_name   problem_type
Thank you very much in advance
All the best
S.M.
simone.marras is offline   Reply With Quote
Old 03-21-2009, 10:19 AM   #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);
It now works correctly.
S.M.
simone.marras is offline   Reply With Quote
Old 03-21-2009, 10:45 AM   #3
subminimalist
 
MK27's Avatar
 
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]);
no &; problem[0] is a pointer (you are used to passing the address of an int variable)
__________________

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   Reply With Quote
Old 03-21-2009, 10:51 AM   #4
Registered User
 
Join Date: Aug 2006
Posts: 61
Quote:
Originally Posted by MK27 View Post
This will work:
Code:
fscanf(file_ID, "%s %s %s\n", header, problem[0], problem[1]);
no &; problem[0] is a pointer (you are used to passing the address of an int variable)
It indeed does, but why this though?

thank you very much for helping!
simone.marras is offline   Reply With Quote
Old 03-21-2009, 10:54 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
Quote:
Originally Posted by simone.marras
It indeed does, but why this though?
Since problem is an array of two pointers to const char, problem[0] is a pointer to const char.
__________________
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   Reply With Quote
Old 03-21-2009, 11:04 AM   #6
Registered User
 
Join Date: Aug 2006
Posts: 61
Thank you
S.
simone.marras is offline   Reply With Quote
Old 03-21-2009, 11:32 AM   #7
subminimalist
 
MK27's Avatar
 
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   Reply With Quote
Old 03-21-2009, 11:36 AM   #8
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by MK27 View Post
This can lead to confusion because you CAN use &charptr with scanf
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   Reply With Quote
Reply

Tags
*string[], file, fscanf, pointer, strings

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:28 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22