Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #109489

Re: error: initializer element is not constant|

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: error: initializer element is not constant|
Date 2017-05-08 12:12 +0100
Organization A noiseless patient Spider
Message-ID <87lgq7d2ko.fsf@bsb.me.uk> (permalink)
References <oep88j$1h14$1@gioia.aioe.org> <oepakk$jif$1@dont-email.me> <oepfak$1t91$1@gioia.aioe.org> <gmXPA.1653357$Pm3.1121619@fx42.am4> <oepglk$1vlt$1@gioia.aioe.org>

Show all headers | View raw


jak <please@nospam.tnx> writes:

> Il 08/05/2017 12:01, bartc ha scritto:
<snip>
>> In C, declaring the type of a variable is optional,

Not for nearly two decades.  If you can ask the compiler to follow a
modern standard, you will be told about such things.

<snip>
> I would like to create an array of pointers, of pointers to
> strings. In this way I can have the pointers as constant and I wrote
> this:

I think you should back-up a bit.  How do you plan to use this data?  I
ask because "a pointer to a string" is not well-defined in C.  A C
strings are not a type but a format of data (essentially insisting on a
null terminator) so must program represent a "pointer to string" as
char * -- i.e. as a pointer to the first character of the string.

> char *Date = "01-01-80";
>
> typedef char * ch;
> typedef ch * p_ch;
>
> p_ch foo[] =
> {
>   &Date,
>   (p_ch)&"Date"

Woop! Woop! Woop!
In C, a cast should alert you to something that needs to be scrutinised.
It often means something is wrong in the design.  (Casts that are there
to silence an advisory warning that you otherwise like to see are not
quite the same.)

In this case there is a real problem.

> };
>
> This works but I do not know how to declare foo without using typedef.

char **foo[] = { ... };

But you can't point to the pointer that points to "Date".  The cast has
simply papered over a real problem.  If you need to change the
pointed-to pointers, you will come unstuck when using literal strings.

So, can you say what you are aiming for here?

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-08 09:55 +0200
  Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-08 10:05 +0200
  Re: error: initializer element is not constant| Noob <root@127.0.0.1> - 2017-05-08 10:38 +0200
    Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-08 11:55 +0200
      Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 11:01 +0100
        Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-08 12:18 +0200
          Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 12:02 +0100
            Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 09:00 -0700
              Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 17:50 +0100
                Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 11:37 -0700
                Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 19:49 +0100
                Re: error: initializer element is not constant| Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-08 20:13 +0100
                Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 12:44 -0700
                Re: error: initializer element is not constant| scott@slp53.sl.home (Scott Lurndal) - 2017-05-08 20:24 +0000
            Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 09:54 +0200
              Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 10:15 +0200
              Re: error: initializer element is not constant| GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-05-09 12:12 +0200
              Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-09 11:47 +0100
                Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 19:06 +0200
                Re: error: initializer element is not constant| scott@slp53.sl.home (Scott Lurndal) - 2017-05-09 17:10 +0000
                Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 19:20 +0200
                Re: error: initializer element is not constant| Barry Schwarz <schwarzb@dqel.com> - 2017-05-09 10:35 -0700
                Re: error: initializer element is not constant| jak <please@nospam.tnx> - 2017-05-09 20:04 +0200
                Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-09 11:57 -0700
                Re: error: initializer element is not constant| supercat@casperkitty.com - 2017-05-09 12:50 -0700
                Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-09 12:54 -0700
                Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-09 20:15 +0100
                Re: error: initializer element is not constant| "James R. Kuyper" <jameskuyper@verizon.net> - 2017-05-09 14:13 -0400
                Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-09 11:41 -0700
              Re: error: initializer element is not constant| Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-09 12:16 +0100
              Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-09 08:11 -0700
          Re: error: initializer element is not constant| Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-08 12:12 +0100
            Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 13:45 +0100
              Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 09:04 -0700
              Re: error: initializer element is not constant| Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-08 19:53 +0100
                Re: error: initializer element is not constant| bartc <bc@freeuk.com> - 2017-05-08 20:44 +0100
                Re: error: initializer element is not constant| Noob <root@127.0.0.1> - 2017-05-09 09:27 +0200
              Re: error: initializer element is not constant| Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-09 07:10 -0700
            Re: error: initializer element is not constant| Keith Thompson <kst-u@mib.org> - 2017-05-08 09:30 -0700
  Re: error: initializer element is not constant| John Bode <jfbode1029@gmail.com> - 2017-05-08 08:05 -0700

csiph-web