My Code Does Not Work? Why?

This is a discussion on My Code Does Not Work? Why? within the C Programming forums, part of the General Programming Boards category; Hi folks, I have compiled and executed the code listed below, only to find that it crashed my MS-DOS prompt. ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    45

    My Code Does Not Work? Why?

    Hi folks,

    I have compiled and executed the code listed below, only to find that it crashed my MS-DOS prompt.

    I would greatly appreciate any help from someone who knows where the error is. It comiles fine, with no errors or warning.

    Code:
    /* interface.c: The include functions designed to control the I/O of the PowerMath System */
    #include <stdio.h>
    
    mathInput() 
    {
         int x;
         int input;
         char progInput[x];
         
         do 
         {
             x=1;
             progInput[x]=getc( stdin );
             progInput[x]=input;
             x=x+1;
         } while (input!=10);
              
    }
    
    int main() 
    {
           mathInput();
           
           return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what value is x in the red section:
    Code:
         int x;
         int input;
         char progInput[x];
    
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    I would like x really to be any number that is wanted. If this is not possible, then do I have to set a limit on the matrix size.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since this is C, rather than C++, we have to give an array a known size. You can ask the user if you like, but you HAVE to have a constant value for array sizing. Nothing else works. [And for local variables, you must stay within a few megabytes or less, due to limited stack-size].

    Edit: Alternatively, you could use dynamic memory allocation and use realloc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    x's value is undefined when first defined. Usually it's some BIG number, which means you exhaust the size of the stack and crash your app.
    You need to use dynamic memory or alternatively, input x first THEN create your array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by matsp View Post
    Since this is C, rather than C++, we have to give an array a known size. You can ask the user if you like, but you HAVE to have a constant value for array sizing. Nothing else works. [And for local variables, you must stay within a few megabytes or less, due to limited stack-size].

    Edit: Alternatively, you could use dynamic memory allocation and use realloc.

    --
    Mats
    Why have I been able to do this on most compilers I have used? Eg.

    Code:
    int x = 123;
    char array[x];
    Non-standards compliant? Talking MS and Mingw - or perhaps C++?
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    This is standard in C99. Visual Studio doesn't support it. It's not standard in C90 or C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IceDane View Post
    Why have I been able to do this on most compilers I have used? Eg.

    Code:
    int x = 123;
    char array[x];
    Non-standards compliant? Talking MS and Mingw - or perhaps C++?
    It is C99 (which isn't fully supported by MS, but nearly complete in gcc). But it is not valid to change x later on and expect the array to change size.

    But my objection is not that it's a run-time sized array, but that the original post doesn't actually set x until inside the do-while loop.

    By the way, there are several other problems in the code:
    1) the variable input is never set to anything.
    2) x is always set to 1, then changed to 2 and set back to 1 in the beginning of the loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by matsp View Post
    It is C99 (which isn't fully supported by MS, but nearly complete in gcc). But it is not valid to change x later on and expect the array to change size.

    But my objection is not that it's a run-time sized array, but that the original post doesn't actually set x until inside the do-while loop.

    By the way, there are several other problems in the code:
    1) the variable input is never set to anything.
    2) x is always set to 1, then changed to 2 and set back to 1 in the beginning of the loop.

    --
    Mats
    Ah yeah - now that I think of it, MS cries havoc if I try to declare variables in the midst of my code.

    But as to expecting the arrays to change size as x changes - no, that isn't valid, and I didn't think it was. It is a common error with newbies, however. E.g.:

    Code:
    int x, y;
    
    x = 3 * y;
    
    y = 4; 
    
    // Now they think x should be 3 * 4 = 12.
    I guess that stems from math, or something.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-15-2007, 01:36 AM
  2. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-24-2006, 12:46 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 06:23 AM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21