Thread: errored program

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    errored program

    can someone look at my code and tell me what I'm doing wrong here.

    I keep getting these errors.
    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0
    error C2133: 'array' : unknown size

    I have the following declared in the program and assume that it should work.
    Code:
    const int SIZE=99;
    
    int array[SIZE],
    according to my text book this syntax is correct.
    Last edited by xviddivxoggmp3; 03-16-2004 at 02:22 AM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    In 'old style' C at least you can't even use a const to specify
    the size of an array. You have to do it like this:
    Code:
    #define SIZE 99
    ...
    int array[SIZE]
    It's a pain, but there you go... I'm not sure if this is still the case
    for c99.
    DavT
    -----------------------------------------------

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Don't use void main. Try to be more consistent with your indentation.
    The world is waiting. I must leave you now.

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    how would you pass a #define to function?
    do you have to pass the size of the array with the array to the function?
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > how would you pass a #define to function?
    Code:
    #include <stdio.h>
    
    #define SIZE 99
    
    int print_number(int number);
    
    int main(void)
    {
        print_number(SIZE);
        return 0;
    }
    
    int print_number(int number)
    {
        printf("%d", number);
        return 0;
    }
    The world is waiting. I must leave you now.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You don't pass the #define to the function you use it like a global
    variable. I suppose you could do something like:
    Code:
    int array[SIZE];
    unsigned int siz = SIZE;
    
    foo(array, size);
    Alternatively, if you need to set SIZE at run time use malloc()
    Code:
    void foo (unsigned int size)
    {
      int * array;
      ...
      array = malloc(size * sizeof(*array));
      ...
      free(array);
    }
    I should maybe point out that I haven't looked at your program,
    maybe there's some complexity there that I don't know about.
    DavT
    -----------------------------------------------

  7. #7
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I'm new to c, but fluint (at least i think) in c++.
    The transition is confusing.

    I'm used to passing arrays in c++.
    This involves passing the array and a const int for the size.

    Due to the errors I have received I'm assuming that this is not so for c.

    If I'm reading your post correctly, I'm lead to believe that you do not need to pass a size to the function to reserve array memory.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Originally posted by xviddivxoggmp3

    I'm used to passing arrays in c++.
    This involves passing the array and a const int for the size.

    Due to the errors I have received I'm assuming that this is not so for c.

    If I'm reading your post correctly, I'm lead to believe that you do not need to pass a size to the function to reserve array memory.
    Your errors aren't to do with passing arrays to functions but declaring variable size arrays.
    Last edited by major_blagger; 03-16-2004 at 02:24 PM.

  9. #9
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    would this be valid?
    Code:
     
    #define SIZE 99;
    
    int array[SIZE];
    
    foo(array);
    or how about this
    Code:
     
    int * array;
    
    array = malloc(size * sizeof(*array));
    
    foo(array);
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >would this be valid?
    No. The semicolon at the end of your define directive will wreak havoc after preprocessing.

    >or how about this
    No. size is undefined in the code given. Provided size exists and has a valid value, yes, it would work.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    You might want to pass the array lengths to foo() (depending on what kind of array you have).

  12. #12
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    as for passing the length of the array.
    If i'm using a single demensional integer array, and not using the size for anything in the function, should i pass the size or length.
    I feel it as inconsiquential to the program.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you don't process the array in a way that would require the size, then you have no reason to pass it as an argument. Though I can't think of too many situations where this would be acceptable. Most of the time you need access to the array's size.
    My best code is written with the delete key.

  14. #14
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Talking It compiles

    It compiles finally, but know my logic is generating duplications of the answer. Can someone look to see if it is something stupid I missed. I'm in the middle of tracing it now, but if someone could help I would much appreciate it.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  15. #15
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Exclamation I'm with out a compiler

    I'm not sure if this works, but I'm w/o a compiler to check.
    can someone check this to see if it works.

    (1) does it compile. if not what errors does it print?
    (2) does it print acceptable output?

    Goal:
    All even numbers 2-50 can be represented by the sum of two prime numbers.

    2=1+1
    4=3+1
    ...

    [edit1]
    my trace has resolved the majority of the issues.
    [/edit1]
    Last edited by xviddivxoggmp3; 03-18-2004 at 11:38 AM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM