Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #109576
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: error: initializer element is not constant| |
| Date | 2017-05-09 12:16 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87inlab7ra.fsf@bsb.me.uk> (permalink) |
| References | (2 earlier) <oepfak$1t91$1@gioia.aioe.org> <gmXPA.1653357$Pm3.1121619@fx42.am4> <oepglk$1vlt$1@gioia.aioe.org> <ufYPA.134876$QR.27637@fx34.am4> <oersjf$1q6f$1@gioia.aioe.org> |
jak <please@nospam.tnx> writes:
> Il 08/05/2017 13:02, bartc ha scritto:
>> On 08/05/2017 11:18, jak wrote:
>>
>>> 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:
>>>
>>> char *Date = "01-01-80";
>>>
>>> typedef char * ch;
>>> typedef ch * p_ch;
>>>
>>> p_ch foo[] =
>>> {
>>> &Date,
>>> (p_ch)&"Date"
>>> };
>>>
>>> This works but I do not know how to declare foo without using typedef.
>>
>> Those types would look like this without typedefs:
>>
>> char * str;
>> char * array_of_str[];
>> char ** ptr_to_str;
>> char ** array_of_ptr_to_str[];
>>
>> (The arrays will need a dimension, or need to be initialised.)
>>
>> But arrays of pointers to strings would be unusual, and be harder to
>> initialise as you can't just have "ABC", but need to set up a named
>> string: char* abc="ABC", then use &abc.
>>
>> I don't think you can apply a cast to "ABC" to get the pointer you need.
>> (Well, you can cast it, but it won't be a proper pointer to string,
>> it'll just be a string, and will crash when you apply the dereference
>> below.)
>>
>> Using the array would be harder too as you'll need an extra dereference
>> to extract the string:
>>
>> puts(*array_of_ptr_to_str[0]);
>>
> Ok ok. Bugs have also been put in the middle. The first time I wrote the declaration in this way:
>
> char *Date = "01-01-80";
> char **Phrases[]
> {
> &Date
> }
This looks like a typing error. You should cut and paste code.
> and gcc gave me a lot of errors.
If that was the code, you will get errors. It's just not correct C. I
suspect you meant to write:
char *Date = "01-01-80";
char **Phrases[] = {
&Date
};
which should work.
<snip>
> Now I'm using this work around:
>
> char *Phrases[]
> {
> "Date: ",
> NULL,
> "\r\n"
> };
Are you missing an = again? As shown, this should not work.
> char **DatePos = Phrases[1];
And this won't work (because the types don't match) if you have "char
*Phrases" as shown. Programming is about detail, and you will get very
confused with things that seem to work and then don't or change from
working to broken because you don't spot these details.
> foo()
> {
> *DatePos = ctime(&t);
> *DatePos[strlen(Date) - 1] = '\0';
This is all wrong. You really need to back-up, take a breath, and
re-think. For a start, *DatePos[strlen(Date) - 1] = '\0'; does not do
what you think. It means *(DatePos[strlen(Date) - 1]) = '\0' and
DatePos does not have that many elements.
What is the ultimate purpose of these pointers and arrays of pointers?
> }
>
> But I still do not understand why the compiler does not allow me this:
>
> char *Date = NULL;
> char *Phrases[]
> {
> "Date: ",
> Date,
> "\r\n"
> };
The main reason is syntax. This is not valid declaration in C. Correct
the syntax and yo may find it compiles. Whether is does what you want
is another matter altogether, but since you don't say what you want, I
can't help with that.
> In the end I'm just asking him to allocate space to write in an
> address exactly as it happens when I write NULL instead of 'Date'.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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