Thread: const keyword when initialize pointers

  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    5

    const keyword when initialize pointers

    Hello,
    I have a confusion about the keyword const.


    In the file header data.h I declared a structure

    Code:
    typedef struct
    {
        int date_1;
        int date_2;
        int date_3;
    }Date;
    In the source file data.c, I declare 100 instances of the Date type:
    Code:
    Date date_000
    Date date_001
    Date date_002
    .
    .
    .
    Date date_099
    Each instance must be passed as a parameter of a function, thus the call is made by reference.

    For this I declare an array of pointers of 100 elements:

    Code:
    Data *dataArray[100]
    Each pointer is initialized with the address of an instance:

    Code:
    Date *dataArray[100] = {&date_000,
                            &date_001,
                            &date_002,
                            .
                            .
                            .
                            &date_099};
    The problem is that, if I do such an initialization, the code does not compile, it gives me an error.
    Instead, if I initialize each pointer separately, it works.

    Code:
    Date *dataArray[0] = &date_000;
    Date *dataArray[1] = &date_001;
    Date *dataArray[2] = &date_002;
    .
    .
    .
    Date *dataArray[99] = &date_099;
    Looking for information on the Internet, I understood that the initialization must be done in the following way:

    Code:
    Date *const dataArray[100] = {&date_000,
                                  &data_001,
                                  &data_002,
                                  .
                                  .
                                  .
                                  &data_099};
    And, it really works.


    I did not understand why the const keyword must be added?
    Why array pointers must be declared const array pointers ?
    What is the difference between
    Code:
    Data *dataArray[0] = &data_000
    and
    Code:
    Data *const dataArray[100] = {&data_000, &data_001, &data_002, ... ,&data_099};
    ?


    Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Don't just say "there was an error"! Copy/paste the exact text of the error.

    At any rate, it works for me:
    Code:
    typedef struct { int x, y, z; } Date;
    
    int main() {
        Date a0, a1, a2;
        Date *d[10] = {&a0, &a1, &a2};
        return 0;
    }
    BTW, it is generally a dumb idea to make a bunch of numbered variables. Use an array instead. That way you don't need this extra pointer array.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Dunno, works for me.
    Code:
    typedef struct
    {
        int date_1;
        int date_2;
        int date_3;
    }Date;
    
    Date date_000;
    Date date_001;
    Date date_002;
    
    Date *dataArray[100] = {
        &date_000,
        &date_001,
        &date_002
    };
    
    int main ( ) {
        Date *another[100] = {
            &date_000,
            &date_001,
            &date_002
        };
        return 0;
    }
    
    
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:19:11: warning: unused variable ‘another’ [-Wunused-variable]
       19 |     Date *another[100] = {
          |           ^~~~~~~
    If you want a better answer, read this -> Short, Self Contained, Correct Example
    Post an actual compilable example, not random paraphrased lines out of context.
    Post actual error messages, not vague "there was an error".

    FWIW, just doing
    Date dates[100];
    would save you a lot of bother.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use of const keyword with pointers?
    By Kyle Flores in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2016, 04:21 AM
  2. const keyword.
    By Aslaville in forum C++ Programming
    Replies: 3
    Last Post: 06-25-2013, 11:50 PM
  3. Usages of const keyword
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 09-16-2007, 11:45 PM
  4. c++ Pointers and the const keyword
    By ncallaway in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2006, 05:07 PM
  5. Initialize const variables in constructors
    By mvgian in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2006, 09:40 AM

Tags for this Thread