Thread: initializing static int**

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    12

    initializing static int**

    Hi, as you see from the title I'm wondering is it possible to initialize global variable like

    Code:
    static const int daytab[2][12] = {
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
    };
    except that I'm trying to make a pointer version so I tried to initialize
    Code:
    static int** digits = /*...*/
    It seems to not work even with creating function initialize() and even if I remove the static keyword
    Code:
    int** initialize()
    {
        int** p = (int**)malloc(sizeof(int**) * 2);
    
    
        for (int i = 0; i < 2; ++i)
            *(p + i) = (int*)malloc(sizeof(int) * 12);
        
        /*initialize elements*/
        return p;
    }
    
    
    int** digits = initialize();
    I'm getting the error that this function call is not allowed in a constant expression.

    It was an exercise to do some stuff without using [] so I'm trying to do this. Is this even possible to do? What would be the closest thing to do this if some [] have to be used like

    Code:
    static int* digits[2] = /*...*/
    And even if I would succeed there would be no way to release the resources allocate by malloc when the program terminates. This is probably why its not allowed right?

  2. #2
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    In c you don't need to cast the malloc call. And you may want to try int **p = malloc(sizeof(*p) * 2);

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    12
    Tnx for checking this out and tnx for the tips. But still is there any way to initialize global variable
    Code:
    static int** digits = /* . . . */ ?
    Last edited by etrusks; 04-09-2016 at 06:13 AM.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    I don't think that is going to work. That is a pointer to a pointer vs an array with dimensions which tells the compiler how much memory to allocate.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why does it need to be an int**? Why not something like:

    Code:
    static const int daytab[][12] = {
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
    };
    Jim

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    12
    Tnx for replies! I was just wondering if its possible to do it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Initializing an array of static structures
    By rakeshkool27 in forum C Programming
    Replies: 4
    Last Post: 11-19-2012, 09:16 AM
  2. initializing a static string
    By juice in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2012, 11:04 AM
  3. initializing static stdlib containers
    By skewray in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2008, 07:23 PM
  4. initializing static members
    By steve1_rm in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2008, 05:45 AM
  5. Initializing static const memebers in a class
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2007, 05:39 PM

Tags for this Thread