Thread: i need help with sorting an array

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dwks View Post
    I've learned something today. And now I can correct people whenever they use this.
    In case you're wondering... The reason you must not declare main(void) is because of parameter passing conventions. Under most ABI's, C parameters are evaluated and passed right-to-left, with the CALLER of the function being responsible for managing the stack. However it is conceivable that on some platform somewhere it is the CALLEE's responsibility to clean the stack.

    The problem is, if the function is specifically declared to take a void argument list, the compiler (rightly) assumes that it will never be passed any arguments, so it will not produce the code necessary to fix the stack if arguments had been passed. Then, when the main(void) gets invoked, the C library DOES pass arguments (argc, argv), but main is not aware of this. So when main() returns, the stack becomes corrupt and things go wrong.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The FAQ contradicts what you say: http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    For C

    Under C89, main() is acceptable, although it is advisable to use the C99 standard, under which only these are acceptable:

    int main ( void )
    int main ( int argc, char *argv[] )

    Slight variations of the above are acceptable, where int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.

    The first option is used when you do not require access to the command line arguments.

    The names argc and argv are identifiers that can be changed if you so desire, but sticking to argc/argv is convention.

    The return type of main() must always be an int, this allows a return code to be passed to the invoker.

    Under C89, the return statement at the end of main() is required, whereas under C99 if no return statement is present, return 0 is implied. However, it is good programming practice to always use a return statement, even if you don't have to.
    That's not the first error anyone's found in the FAQ . . . .

    I'll read the page linked to by this and see if it is mentioned. [edit] It isn't. [/edit]

    [edit=2] It makes sense once I think about it, though. Why else could main be declared with argc and argv (and sometimes envp) or without parameters? I guess I just never thought about it. [/edit]
    Last edited by dwks; 04-18-2007 at 04:49 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The quote from the C standard in the following link says that "int main(void)" is one of the allowed declarations.

    http://www.thescripts.com/forum/thread212123.html

    Compiling with gcc with -W -Wall -ansi -pedantic, and in either C89 or C99 mode (-std=c99), either of the following compiles without any warnings:
    Code:
    int main(void) {
      return 0;
    }
    Code:
    int main() {
      return 0;
    }
    If I give a bogus argument:
    Code:
    int main(float x) {
      return 0;
    }
    I get the following warnings in either C89 or C99 mode:

    foo.c:2: warning: first argument of ‘main’ should be ‘int’
    foo.c:2: warning: ‘main’ takes only zero or two arguments
    foo.c:1: warning: unused parameter ‘x’

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robatino View Post
    The quote from the C standard in the following link says that "int main(void)" is one of the allowed declarations.

    http://www.thescripts.com/forum/thread212123.html
    I admit that it's been a while since I looked at this part of the standard. But as an implementor, I can see that there are serious problems with declaring a function to take a void argument list when in fact it takes arguments. This is the sort of thing that creates massive headaches for compiler writers. "It would be so easy if it weren't for this darn section 1.5.12!" Etc.

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Under the hood, the compiler could just treat main(void) as a special case and interpret it differently. It does have the advantage that to the user it makes main() appear more similar to other functions.

  6. #21
    Registered User
    Join Date
    Apr 2007
    Location
    Bosnia
    Posts
    5
    hehe nice....so we all learned something about useing main(void) Like i said, i didn't use it in my code, and i never use it..... But can anyone tell me, why i see "void main(void)" in some codes???

  7. #22
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    But can anyone tell me, why i see "void main(void)" in some codes???
    Pick at least 1:
    - Ignorance
    - Stubbornness
    - Laziness
    If you understand what you're doing, you're not learning anything.

  8. #23
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    I always wrote "int main()" but then I remember a thread (that was lost during the reset) where someone explained in great detail why we should write all "int main(void)" and how that would be the correct way ... guess it's back to the empty braces.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    As the quote from the C99 standard from the above URL indicates, "int main(void)" is explicitly blessed (5.1.2.2.1 Program startup). I'm not sure about "int main()", although I wasn't able to get gcc to emit any errors or warnings with either version.

    http://www.google.com/search?hl=en&q...=Google+Search

  10. #25
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robatino View Post
    As the quote from the C99 standard from the above URL indicates, "int main(void)" is explicitly blessed (5.1.2.2.1 Program startup). I'm not sure about "int main()", although I wasn't able to get gcc to emit any errors or warnings with either version.
    It's okay in all cases, not only for main(). Following code is warning-free:

    Code:
    void foo(void);
    void foo()
    {
    }

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The FAQ contradicts what you say
    And the FAQ is correct.

  12. #27
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    here is another solution

    Code:
    #include<stdio.h>
    #define MAX 20
    
    int main(void)
    {
        int tab[MAX],nb_item,i,j,tmp;
        
        do{
            printf("Enter the number of elements ");
            scanf("&#37;d",&nb_item);
            
        }while( nb_item<1 && nb_item>=MAX );
        
        for( i=0 ; i<nb_item ; i++)
        {
             printf("T[%d]=",i+1);
             scanf("%d",&tab[i]);
        }
        
        //the sorting start here
        for( i=0 ; i<nb_item ; i++)
        {
             if(tab[i]>=0)
             {
                 tmp=tab[i];
                 for(j=i-1 ; j>=0 && tab[j]<0 ; j--)
                 {
                       tab[j+1]=tab[j];  
                 }
                 tab[j+1]=tmp;
             }
         }
         
         for( i=0 ; i<nb_item ; i++)
         printf("%d  ",tab[i]);
         
        
         return 0;
    }
    Last edited by redche; 04-20-2007 at 06:33 PM.

  13. #28
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by redche View Post
    here is another solution
    Type something like "oops" at the prompt.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #29
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    what do you mean???

  15. #30
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    1) Run program.
    2) When prompted for input, enter the string "oops".
    3) Watch program run in circles like The Six Million Dollar Man.



    I believe he's just pointing out that your program cannot accept every kind of input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM