Help! Problem with strings

This is a discussion on Help! Problem with strings within the C Programming forums, part of the General Programming Boards category; I'm new to programming with C and I'm having problems with strings, pointers and structures. If someone could look at ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Help! Problem with strings

    I'm new to programming with C and I'm having problems with strings, pointers and structures. If someone could look at this code and tell me what's wrong, I'd greatly appreciate it.

    Code:
    struct FGrec_data{
    	
    	char *input_file;
    	char *start_time;
    	char *stop_time;
    	char *output_file;
    	char *output_path;
    };
    
    int main(int argc, char *argv[])                                                        
    
    {	
           struct FGrec_data FGdt;
           parse_main_args(argc, argv, &FGdt);
    
    ......
    }
    
    int parse_main_args(int argc, char *argv[], struct FGrec_data *FGdata) 
    
    {
           ....
           int i;
           for(i = (strlen(FGdata->output_file)-2); i > 0; i--) {
    		if(FGdata->output_file[i] == 92) {
    			strncpy(FGdata->output_path, FGdata->output_file, i);
    			break;
    		}
    	}     
    
            //Why does this make my program crash? 
           ....
           ....
    }

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Print the output you get, as well the input to the function...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > char *input_file;
    Compare with
    char input_file[100];

    In your case, you have no space allocated to any of the strings, so any string operation is going to blow up sooner or later.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    It looks like you've not got any memory assigned to those pointers in your struct, so when you go about accessing them, so they're just pointing wildly into your process' heap.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    So I should "malloc" all my strings?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    If you're using pointers, yes you need to consider how the memory is allocated.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Smile

    OK. Got it. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 09:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 01:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 09:05 PM

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