regarding "initializer element is not constant"

This is a discussion on regarding "initializer element is not constant" within the C Programming forums, part of the General Programming Boards category; why do i get this error test.c:6: warning: initializer element is not constant test.c:6: warning: (near initialization for ‘len’) when ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Smile regarding "initializer element is not constant"

    why do i get this error
    test.c:6: warning: initializer element is not constant
    test.c:6: warning: (near initialization for ‘len’)

    when i compile this with "len" as a global int (as shown). It doesn't happen if "len" is localized within main().
    Code:
      1 #include <string.h>
      2 #include <stdio.h>
      3 
      4 #define this "that"
      5 
      6 int len = strlen(this);
      7 int main () {
      8         printf("%d\n", len);
      9 }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    strlen is evaluated at runtime, not compile-time, as such it must be inside a function.
    Note that some compilers might be able to optimise it to a compile-time constant, but that doesn't make it valid to assume that you're allowed to do what you're trying to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    well i guess that makes sense, since there really is no reason to apply strlen to a global constant anyway -- except sometimes I am lazy about counting...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    You can use sizeof() to get the length of a literal string.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Just be careful,

    1. You can't use it on pointers to literals
    2. It includes the NUL terminator, ie strlen(x) == sizeof(x) - 1

    Not that 1 matters in this case

    Code:
    [zac@neux sandbox]$ gcc -W -Wall -std=c99 -pedantic -O0 sandbox.c -o sandbox
    [zac@neux sandbox]$ cat sandbox.c
    #include <stdio.h>
    
    #define test "four"
    
    const int len = sizeof(test);
    
    int main(void)
    {
        printf("&#37;s = %d\n", test, len);
        return 0;
    }
    [zac@neux sandbox]$ ./sandbox 
    four = 5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-05-2008, 10:30 PM
  2. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  3. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. sorting
    By penny_731729 in forum C Programming
    Replies: 3
    Last Post: 04-28-2003, 10:56 AM

Tags for this Thread


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