1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Array dimension by global variable not working

Discussion in 'C' started by rakoczimrks, Jul 11, 2011.

  1. rakoczimrks

    rakoczimrks New Member

    Joined:
    Jul 11, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I have the next code, actually part of a code (it's C not PHP)


    PHP:
    include <stdio.h>

    const 
    size_t SIZE 20;

    int main(void)
    {
        
    char cWord1[SIZE]=" ";   //first string
        
    char cWord2[SIZE]=" ";   //second string

    ....
    ...when i compile it, i get the next error message
    error: variable-sized object may not be initialized
    SIZE is already defined, why doesn't work?
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Arrays declared as local variables may not be initialized as these are created on the stack - declare the 2 arrays as global variables and see if you get the compilation error.
     
  3. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    1
    Trophy Points:
    0
    Char array cannot be initialized with space and make it "" instead of " "
     
  4. rakoczimrks

    rakoczimrks New Member

    Joined:
    Jul 11, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I solved the problem with #define directive, thx for your reply.
     
  5. Funkymaster

    Funkymaster New Member

    Joined:
    Jul 25, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    thanks rakoczimrks the problem id that when declaring your variables, for instance char has a single quotation mark, the only thing that has double quotation mark is a string, hope u'll gt it ryt dis tym, THANKS
     
  6. srp5789

    srp5789 New Member

    Joined:
    Jul 25, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    C is a procedural language all code statements are executed one by one, besides that execution of C program passes through 3 phases
    1.preprocessing
    2.compilation
    3.execution

    in your question

    const size_t size = 10; statement is resolved at execution time while in statement

    char cWord1[SIZE]=" "; value of SIZE is expected at compilation time, so at that time of compilation error will be generated indicating array size is not defined.

    solution to this problem is
    1.resolve in phase 1 i.e. by using preprocessing directive #define(that u already got )
    2.instead of using SIZE=20 and then cWord1[SIZE] use cWord1[20]
     
  7. srp5789

    srp5789 New Member

    Joined:
    Jul 25, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    this code will run fine in C++
     
  8. srp5789

    srp5789 New Member

    Joined:
    Jul 25, 2011
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    sorry but it will not affect replacing " " by "" , as " " means assigning only spaces to character string while "" means not assigning anything.
     

Share This Page