Thread: initializer element is not constant

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    26

    initializer element is not constant

    Hello,

    When an array pointers created inside the main, program works. But when I intialize array of pointers outside main, compiler gives error as

    Initializer element is not constant

    This works

    Code:
    #include<stdio.h>
    
    
    int main()
    {
        
        int a = 10, b = 20, c = 50, i;
        int *p =&a;
        int *arrop[3] = {p, &b, &c};
        
        for(i = 0; i < 3; i++)
        {
            printf("Address = %d\t Value = %d\n", arrop[i], *arrop[i]);
        }
    
    
        return 0;
    }
    But this doesn't


    Code:
    #include<stdio.h>
    
    
    int a = 10, b = 20, c = 50, i;
    int *p =&a;
    int *arrop[3] = {p, &b, &c};
        
    int main()
    {
        
        for(i = 0; i < 3; i++)
        {
            printf("Address = %d\t Value = %d\n", arrop[i], *arrop[i]);
        }
    
    
        return 0;
    }
    I googled the error, but I didn't understand anything

    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    OK well, by choosing to declare the array outside of main or any other function you are implicitly saying it has a static storage duration. This can mean a few things, but in this case it means that you intend to use the array as a global variable. In effect it's an array that can be used throughout the whole program. In C, though, there is a rule that global variables must be initialized with constant expressions; literal constants like 0, 1, 'a' ... etc. are safe options, provided they are the right type. What this also means is that you can't do things like what you are trying to do, when if the array wasn't globally defined, you could.

    If you just want to achieve the thing, then declaring it inside main() is by far the easiest choice to make with the least problems down the road.

  3. #3
    Registered User
    Join Date
    Jun 2018
    Posts
    26
    SO how can I make above program work if it's global. I tied putting const before array

    Code:
    const int *arrop[3] = {p, &b, &c};
    Where can I find more details about it?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's extremely unlikely that globals are what you want, unless you are actually trying to compare the two.

    But to answer the question, for a global, the bit inside the braces that you are using to do the initializing with must be actual numbers. p is a variable, not a number, so that doesn't count. &b is the address of a variable, not a number, so that doesn't count. Et cetera. (You can use expressions if all the parts are numbers, so you're allowed 4+3 or whatever, but nothing that involves a variable.) (The reason being that you can't execute "code" outside of a function, so it has to be something the compiler can evaluate itself without knowing anything about the program itself.)

  5. #5
    Registered User
    Join Date
    Jun 2018
    Posts
    26
    &b is the address of a variable, not a number, so that doesn't count.
    But the following code works

    Code:
    #include<stdio.h>
     
    int a = 10, b = 20, c = 50, i;
    
    
    int *arrop[3] = {&a, &b, &c};
         
    int main()
    {
         
        for(i = 0; i < 3; i++)
        {
            printf("Address = %d\t Value = %d\n", arrop[i], *arrop[i]);
        }
     
     
        return 0;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Athul View Post
    But the following code works
    Hey so it does! I had thought that even for globals you couldn't do address-of, but I guess you can. I'll have to look that up again. (EDIT: I've looked it up, and addresses-of-static-variables are allowed just for initializers. You might think you could get away with
    Code:
    int * const p = &a;
    but you probably can't. (The standard says an implementation might let you do that, but it doesn't have to.) C++ probably would, but I'd have to look that up too.
    Last edited by tabstop; 10-21-2018 at 11:54 PM.

  7. #7
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    138
    Quote Originally Posted by Athul View Post
    But the following code works

    Code:
    #include<stdio.h>
     
    int a = 10, b = 20, c = 50, i;
    
    
    int *arrop[3] = {&a, &b, &c};
         
    int main()
    {
         
        for(i = 0; i < 3; i++)
        {
            printf("Address = %d\t Value = %d\n", arrop[i], *arrop[i]);
        }
     
     
        return 0;
    }
    This is better:
    Code:
                printf("Address = %p\t Value = %d\n", arrop[i], *arrop[i]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "initializer element is not constant" error
    By dukester in forum C Programming
    Replies: 24
    Last Post: 05-18-2011, 12:03 PM
  2. initializer element is not constant error - help
    By jay32m in forum C Programming
    Replies: 7
    Last Post: 11-17-2010, 03:24 PM
  3. regarding "initializer element is not constant"
    By MK27 in forum C Programming
    Replies: 4
    Last Post: 08-23-2008, 11:30 PM
  4. initializer element is not constant...
    By John Connor in forum C Programming
    Replies: 12
    Last Post: 02-01-2008, 06:28 PM
  5. initializer element is not constant
    By lucaspewkas in forum C Programming
    Replies: 4
    Last Post: 05-20-2005, 05:21 AM

Tags for this Thread