Thread: Passing and returning strings from a function

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    Passing and returning strings from a function

    I have a situation where I have to map a 3 letter code of Amino Acid to its 1 letter code. Like ALA=>A. So if I have 'ALA' in my array, it should match and print 'A'. The following is a snippet of the code I had written..its giving a segmentation fault...kindly help
    ***This is the full code**
    Code:
    char* dictionary_func(char*);
    
    manin()
    {
    FILE *fp;
    char amino[4];
    char line[80];
    char chain='0';
    char chain1='1';
    char code[1];
    int i,j,k,count=0;
    int chain_count=0;
    
    fp=fopen("test.pdb","r");
    
        if(fp!=NULL)
        {
    
        while(fgets(line,80,fp )!=NULL)
        {
        
        if((line[0]=='A')&&(line[1]=='T')&&(line[2]=='O')&&(line[3]=='M'))
            {
            count++;
                chain=line[21];
                if(chain1!=chain)
                {
                    if(chain==' ')
                        printf(">Sequence:\n");
                    else{
                        chain_count++;
                        printf("\n>Chain %c \n",chain);;
                        }
                }
                chain1=chain;        
                for(i=17,j=0;i<=19,j<3;i++,j++)
                {
                
                    amino[j]=line[i];
        
                }
                //printf("%s",amino);
               
                printf("%s",dictionary_func(amino));                 //This is the part giving the segmentation fault
                if(count%70==0)
                    printf("\n");
            }
        
        }
        fclose(fp);
    
        }
    
    }
    
    char* dictionary_func(char*s)
    {
    
       if(strcmp(s,"ALA")==0)
          {
              return ("A");
               }
    
    
    }
    Last edited by Sanchari Sircar; 11-07-2012 at 03:01 PM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Strings have a terminating null character at the end. If you want to use "ALA" you will need space for 4 characters, not 3.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    Tried..didnt work

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You are still getting a segfault? Or are you getting some errors? Post your revised code.
    Give an example input when it segfaults. You need to have an else return in your function just in case ALA!=0
    Last edited by camel-man; 11-07-2012 at 03:04 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What include files are you including?

    You may want to see about increasing your compiler warnings, these are the warnings I received when I compiled your code:
    main.c||In function ‘main’:|
    main.c|40|warning: left-hand operand of comma expression has no effect [-Wunused-value]|
    main.c|15|warning: unused variable ‘k’ [-Wunused-variable]|
    main.c|14|warning: unused variable ‘code’ [-Wunused-variable]|
    main.c|9|warning: unused variable ‘fi’ [-Wunused-variable]|
    main.c||In function ‘dictionary_func’:|
    main.c|69|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build finished: 0 errors, 5 warnings ===|

    Jim

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    Thank you so much ...that was the problem, I should have given the else condition.......its working fine now!!!

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Don't forget about that first warning. It is telling you your for() statement is malformed.

    Jim

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    oh..ok..thanks..i will try to find a way around it..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function returning array of strings.
    By Pingoo in forum C Programming
    Replies: 11
    Last Post: 04-12-2012, 01:48 AM
  2. Passing an array of strings to a function.
    By dazman19 in forum C Programming
    Replies: 10
    Last Post: 09-16-2009, 06:32 PM
  3. Passing Array of Strings to a function ??
    By Taper in forum C Programming
    Replies: 20
    Last Post: 12-07-2008, 02:58 PM
  4. returning strings - function.
    By Vber in forum C Programming
    Replies: 11
    Last Post: 03-23-2003, 05:24 PM
  5. Passing & Returning Strings from Functions
    By nisaacs in forum C Programming
    Replies: 1
    Last Post: 01-30-2002, 05:34 AM

Tags for this Thread