problems with Dev-C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • graciano
    New Member
    • Mar 2007
    • 1

    problems with Dev-C++

    Hi

    I wrote this program ...

    Code:
    #include<stdio.h>
    #define size 3
    
    main()
    {
          int a[size];
          int i;
             
          printf("Input values for array a\n");
          for(i=0;i<size;i++)
              {
              printf("a[ %d ] = ",i);
              scanf("%d",a[i]);
              }
    }
    ... and e always an get an Windows XP error message:
    exp.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

    Can someone help me?
    Thanks
    Last edited by Banfa; Mar 15 '07, 01:40 AM. Reason: Added [code]...[/code] tags round the code
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You have 2 or 3 problems (depending on the file type C or C++)

    1. main must return int. You have no choice in this matter, not returning int invokes undefined behaviour. And as a consequence of this you need to add a return statement.

    2. If this is a C problem then main must have the parameters int argc, char ** argp

    int main(int argc, char ** argp)

    again not including them is undefined behaviour. If this is C++ then main can take no parameters.

    3. And finally on the code line
    Code:
    scanf("%d",a[i]);
    scanf requires a pointer to an int but you have provide an int meaning that it is almost certainly writing to some random memory location, this should be
    Code:
    scanf("%d",&a[i]);

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Banfa's 3rd suggestion is likely the one causing problems - earlier today, I was trying to use a C program and forgot to add the & before my variable in scanf, and I got the same error.

      Guess that's what I get for trying to go backwards in time and learn C after C++ :P

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by Ganon11
        Banfa's 3rd suggestion is likely the one causing problems - earlier today, I was trying to use a C program and forgot to add the & before my variable in scanf, and I got the same error.

        Guess that's what I get for trying to go backwards in time and learn C after C++ :P
        I thought I told you not to bother with scanf?

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          I was trying to see what was happening in a user's code! I had to do it just to see what was going wrong!

          Comment

          Working...