fscanf with pointer of strings *string[]

This is a discussion on fscanf with pointer of strings *string[] within the C Programming forums, part of the General Programming Boards category; Hi there, I am reading a file that contains both numbers and strings; by reading it sequentially, everything goes well ...

  1. #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.

  2. #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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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)
    Last edited by MK27; 03-21-2009 at 11:48 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #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!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Thank you
    S.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  3. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

Tags for this Thread


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