Thread: I have a program to compile,need help.

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15

    Unhappy I have a program to compile,need help.

    /*This program is to permute some letters of alphabet.
    However i can not compile it .
    There are always many errors.
    your help will be appreciated deeply.
    I am a new guy in Ubuntu .*/


    Code:
    #include<stdio.h>
    #include<string.h>
    int L;
    void insort(int n,char s[],char c[]);
    
    
    void insort(int n,char s[],char c[])
    {
    char d[50];
    char str1[50],*str2;
    int i;
    if(n==0) s[n]=c[n];
    else{ for(i=n;i>=0;i--)
    { memcpy(d,s,strlen(s));
    d[strlen(s)]='\0';
    memcpy(str1,d,i);
    str1[i]='\0';
    str2=d+i;
    strcat(str1,c[n]);
    strcat(str1,str2);
    //d=str1;
    memcpy(d,str1,strlen(str1));
    d[strlen(str1)]='\0';
    if (n==L)printf("%s\n",d);
    }
    }
    if (n+1<=L)
    insort(n+1,s,c);
    
    
    }
    
    
    
    
    int main(){ char str[1000];
    str[0]='\0';
    
    char S[]="abcd";
    
    L =strlen(S);
    
    insort(0,str,S);
    
    return 0;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you got errors, you should be able to say what they are.

    And I doubt anyone is going to look very carefully at the code until it's indented in some reasonable way.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15
    errors are follow:
    In function "insort"
    warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    strcat(str1,c[n]);
    You're passing a char here instead of a char*
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15
    so ,how can I correct this mistake?

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Are you trying to append a single character there, or do you want the whole string beginning at position n?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15
    I am trying to append a single character there

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Code:
    int L;
    
    int main() {
    ...
       L = strlen(S);
    ...
    }
    You're using a global variable to pass in strlen(S)?? Something that could have easily been done inside the function? Why? This is terrible style.
    Code:
    if (n == 0)
       s[n]=c[n];
    If you know that 'n' is 0 here, you may as well just write s[0] = c[0]; Its clearer to me at least. On a quick glance a reader might get confused.
    Code:
    memcpy(d,s,strlen(s));
    d[strlen(s)]='\0';
    ...
    memcpy(d,str1,strlen(str1));
    d[strlen(str1)]='\0';
    You do realize those behave exactly the same as
    Code:
    strcpy(d, s);
    strcpy(d, str1);
    right? Except the strcat's are going to be easier to read and more efficient (no need to loop through the string to calculate it's length).
    Code:
    memcpy(str1,d,i);
    str1[i]='\0';
    Once again, this is equivalent to
    Code:
    strncpy(str1, d, i);
    It's easier to read, use it!
    Code:
    strcat(str1,c[n]);
    Riiight, pass a character to a function that expects a null-terminated string. THAT's a good idea.
    Code:
    strncat(str1, &c[n], 1);
    Oh wow, much better.
    Code:
    str2=d+i;
    ...
    strcat(str1,str2);
    Really? The WHOLE reason you put an amazingly descriptive 'str2' in there is so you can temporarily store 'd + i' for no reason? If you're going to use variable names like 'str2' you may as well just use 'd + i' in the call to strcat(str1, d + i);

    You don't think it's just coincidence that, since you're working with strings, the string handling functions are useful, do you?

    Also, the code as you posted it won't even work for at least one reason that I can see. On the first iteration of the loop you are overwriting the null-terminator in 's':
    Code:
    if(n==0) s[n]=c[n];
    On the very next iteration of the loop, you assume you can use strlen(s), which obviously isn't true because you just blew away the null-terminator!
    Code:
    memcpy(d,s,strlen(s));
    And finally, FOR THE LOVE OF GOD LEARN TO INDENT PROPERLY. The forums have a freakin' preview button, why the hell can't anyone figure out how to use it!?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void insort(int n, char s[], char c[])
    {
       char d[50], str1[50];
       int i;
       
       if (n == 0)
          s[0]=c[0];
       else {
          for (i = n; i >= 0; i--) {
             strcpy(d, s);
             strncpy(str1, d, 1);
             strncat(str1, &c[n], 1);
             strcat(str1, d + i);
             strcpy(d, str1);
             
             if (c[n] == '\0')
                printf("&#37;s\n", d);
          }
       }
       
       if (c[n] != '\0')
          insort(n+1, s, c);
    }
    
    int main(void) {
       char str[1000] = "";
       char S[] = "abcd";
       
       insort(0, str, S);
       return 0;
    }
    Was that so hard? Now go fix it.
    (Btw, I still have no idea what this function is supposed to do, your choice of variable names and documentation is exquisite)
    Last edited by arpsmack; 10-17-2008 at 06:54 AM. Reason: Typed 'strcat' instead of 'strcpy'

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by wxjeacen View Post
    I am trying to append a single character there
    In that case, you shouldn't use strcat() since that appends a whole string instead of a single character.
    I haven't finished my coffee yet, but I believe this should work:
    Code:
    size_t len = strlen( str1 );
    str1[len] = c[n];
    str1[len + 1] = '\0';
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15
    Thanks very much ,masters.


    I have learned a lot from your replies.


    however ,there is still something wrong with my algorithm,


    maybe ,the algorithm of permuting strings is not perfect.


    I think ,i have to find another way to complete the algorithm.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by wxjeacen View Post
    however ,there is still something wrong with my algorithm,


    maybe ,the algorithm of permuting strings is not perfect.


    I think ,i have to find another way to complete the algorithm.
    What's wrong with it?
    It might be that you're overflowing the buffer, since you're using strcat() which doesn't know the size of your string buffer.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15
    however, i have had defined the array str1[50] and d[50],how can they be overflowed?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by wxjeacen View Post
    however, i have had defined the array str1[50] and d[50],how can they be overflowed?
    By putting 50 or more letters in them. It's hard to say whether or not that happens, just by looking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread