Thread: Help, I dont know why this wont work on linux, works on windows :(!

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    4

    Help, I dont know why this wont work on linux, works on windows :(!

    Code:
    #include<string.h>  
    #include<stdio.h>  
    
        int main()  
    
        {  
         int i,j,n;  
         char s[51][100],ch1[200],ch2[200],temp[100];  
         while (scanf("%d",&n),n)  
         {  
          for (i=1;i<=n;i++)  
              scanf("%s",&s[i]);  
          for (i=1;i<n;i++)  
          for (j=i+1;j<=n;j++)  
          {  
           strcpy(ch1,s[i]);  
           strcpy(ch2,s[j]);  
           strcat(ch1,s[j]);  
           strcat(ch2,s[i]);  
           if (strcmp(ch1,ch2)<0) {strcpy(temp,s[i]); strcpy(s[i],s[j]); strcpy(s[j],temp);}  
          }  
          for (i=1;i<=n;i++)  
          printf("%s",s[i]);  
          printf("\n");  
         }  
         return 0;  
        }
    works on Windows, not on Linux pls help

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You may want to increase your compiler warning level.

    ||=In function ‘main’:|
    main.c|12|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[100]’ [-Wformat]|
    ||=== Build finished: 0 errors, 1 warnings ===|
    You don't need the ampersand with C-strings.

    Also remember that arrays in C/c++ start at zero and stop at size - 1.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    4
    ty u saved me !

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by tmand6 View Post
    works on Windows, not on Linux.
    This is the problem:

    Code:
              scanf("%s",&s[i]);
    If a program declares an array, like char array[...], then microsoft compilers will treat &array and array as essentially the same thing, but other compilers won't. As jimblumberg posted, you need to change this to:

    Code:
              scanf("%s",s[i]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I dont get this working on gcc it works fine in windows
    By wise_ron in forum C Programming
    Replies: 8
    Last Post: 05-08-2006, 05:33 AM
  2. GUI library that works on windows and linux?
    By Logan in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2006, 08:40 PM
  3. This works on linux...but why not windows?
    By Kinasz in forum C Programming
    Replies: 6
    Last Post: 12-29-2005, 04:50 PM
  4. works on windows not linux maybe std?
    By blackgold>> in forum C++ Programming
    Replies: 9
    Last Post: 12-20-2003, 01:46 PM
  5. Replies: 6
    Last Post: 01-07-2002, 02:46 AM