Please help me with this

This is a discussion on Please help me with this within the C Programming forums, part of the General Programming Boards category; Hi! With this code I get this error message "error C2057: expected constant expression". How can I get rid of ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy Please help me with this

    Hi!

    With this code I get this error message "error C2057: expected constant expression". How can I get rid of this error? Is there any other way to declare variable "text"?


    Code:
    # include <stdio.h>
    
    void function (short NoOfChars);
    
    int main ()
    {
        short NoOfChars = 0;
    
        printf ("Enter a number: ");
        scanf ("%d", &NoOfChars);
        function (NoOfChars);
        return 0;
    }
    void function (short NoOfChars)
    {
        static char Text[NoOfChars+1] = {0};
    
        /*
        other useful code ;)
        */
    }
    Help me!!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well your having a problem because at compile-time the compiler doesn't know how many characters there will be passed to the function. You need to use dynamic memory allocation here. Get well acquainted with your friend Mr. Malloc. Try this in your function.
    Code:
    void function (short NoOfChars)
    {
        /* Allocate the memory */
        char *pText = (char*) malloc( NoOfChars + 1 );
       
        /* Make sure we got the memory!! */
        if( pText == NULL )
        {
            /* Bail out! */
            return;
        }
       
        /*
        other useful code 
        */
    
        /* Free the allocated memory */
        free( pText );
    }
    
    EDIT: You will need to inlude stdlib.h or malloc.h
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thanks for you reply. But I have a problem. Without the static declaration this code is working fine but I really need to use a static declaration. With static declaration I get error C2099: initializer is not a constant. What to do now?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by GaPe
    Thanks for you reply. But I have a problem. Without the static declaration this code is working fine but I really need to use a static declaration. With static declaration I get error C2099: initializer is not a constant. What to do now?
    I don't know what else you are trying to do. With the code you posted you shouldn't need 'static'. Then again, you did say "Other useful code" so I do not know what that entails. Post some more code and maybe I can help.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I've changed this
    Code:
    char *pText = (char*) malloc( NoOfChars + 1 );
    into this and it is working now
    Code:
    char *pText;
    
    pText = (char*) malloc( NoOfChars + 1 );
    ]
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Under C89, array sizes must be known at compile time. In your first example code (GaPe), the size isn't known, because it is specified by variable NoOfChars. Only at runtime will you know what value NoOfChars holds.
    Code:
    /* Invalid under C89 */
    static char Text[NoOfChars+1] = {0};
    I believe things have changed under C99 though, this (I think) is now allowed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I believe things have changed under C99 though, this (I think) is now allowed.
    Correct, C99 changed one word and we magically could use non-constant values as array sizes.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    All nice and good but how do I use this C99 standard. Does VC++ .NET support this and how to enable it?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Does VC++ .NET support this and how to enable it?
    I believe it may conform to some parts of the new standard (including the variable sized arrays), but as of yet there are no compilers that do so completely. Try MSDN or your documentation to see how it is done, I haven't used VC++.NET.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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