Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #112834
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Warning: assignment from incompatible pointer type |
| Date | 2017-06-22 17:26 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87efuc7zvj.fsf@bsb.me.uk> (permalink) |
| References | (5 earlier) <oidlf0$gne$1@dont-email.me> <oif9q6$s7i$2@dont-email.me> <lIM2B.59754$OJ2.17773@fx37.am4> <87k24489ep.fsf@bsb.me.uk> <uLP2B.31163$9u1.10034@fx06.am4> |
bartc <bc@freeuk.com> writes:
> On 22/06/2017 14:00, Ben Bacarisse wrote:
>> bartc <bc@freeuk.com> writes:
>>
>>> On 22/06/2017 03:30, DFS wrote:
>> <snip>
>>>> But python is easy to learn, and has a big bunch of libraries. And I
>>>> love the python list comprehension.
>>>>
>>>> One line to count the unique characters in a string.
>>>>
>>>> msg = "Now is the time for all good men to come to the aid of their country"
>>>> for t in sorted(set([(x,msg.count(x)) for x in msg])):
>>>> print t[0],t[1]
>>
>> <snip>
>>> One advantage of a language like C is that it forces you to work more
>>> efficiently. Here's the same task in C:
>>
>> No the C you show is for slightly a different task, but it's different
>> in a way that matters to how good C is at doing it. Python (at least
>> modern Python) is Unicode-aware actual letters are counted, not just
>> bytes. I'm sure the C code for the same task would still beat Python
>> hands down, but it would require a little more fiddling about.
>
> So the C also requires you to make decisions about how in-depth a
> solution needs to be. If that information is available, then again you
> can choose a more efficient solution. Such as working only with 8-bit
> characters, or with 7-bit ASCII.
That's equally true of Python though. Python allows you to count bytes
as well if that's really what you want to do. C is not special in this
regard. The main difference is that Python offers a higher-level
interface to Unicode strings.
<snip>
>>> #include <stdio.h>
>>>
>>> int main(void) {
>>>
>>> char* msg = "Now is the time for all good men"
>>> " to come to the aid of their country";
>>> char* s;
>>> int counts[256]={0};
>>> int c;
>>>
>>> s=msg;
>>>
>>> while (c=*s++) ++counts[c];
>>>
>>> for (c=' '; c<256; ++c)
>>> if (counts[c]) printf("'%c': %d\n",c,counts[c]);
>>> }
>>
>> You allocate space for 256 byte counts but char might be signed. This
>> is another example of where script languages win over C -- usually fewer
>> bugs in corner cases.
>
> This is just the result of silly decision in C to make char signed.
Well, it's the result of the possibility that char might be signed but
I'm wary of trivialising it by saying "just".
> ... (But FWIW, I was testing with my own compiler where char is
> unsigned.)
>
> You would expect thorough testing however to pick up things like
> this.
Unless you test with compiler that uses unsigned char. If a program is
distributed as source you need to be sure it makes no unfounded
assumptions about other compilers.
<snip>
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 12:48 -0400
Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-20 18:08 +0100
Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-21 09:32 -0400
Re: Warning: assignment from incompatible pointer type scott@slp53.sl.home (Scott Lurndal) - 2017-06-21 13:48 +0000
Re: Warning: assignment from incompatible pointer type Richard Heathfield <rjh@cpax.org.uk> - 2017-06-20 18:15 +0100
Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 18:00 -0400
Re: Warning: assignment from incompatible pointer type "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-20 18:47 -0400
Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-21 00:10 +0100
Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 19:56 -0400
Re: Warning: assignment from incompatible pointer type "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-20 13:23 -0400
Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 18:59 -0400
Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-20 19:23 +0100
Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 18:59 -0400
Re: Warning: assignment from incompatible pointer type Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-20 19:46 -0400
Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-21 02:05 +0100
Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-20 21:37 -0400
Re: Warning: assignment from incompatible pointer type Richard Heathfield <rjh@cpax.org.uk> - 2017-06-21 09:25 +0100
Re: Warning: assignment from incompatible pointer type David Brown <david.brown@hesbynett.no> - 2017-06-21 13:35 +0200
Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-21 22:30 -0400
Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-22 11:03 +0100
Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 11:10 +0100
Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 11:42 +0100
Re: Warning: assignment from incompatible pointer type David Brown <david.brown@hesbynett.no> - 2017-06-22 13:49 +0200
Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 14:40 +0100
Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 17:00 +0100
Re: Warning: assignment from incompatible pointer type David Brown <david.brown@hesbynett.no> - 2017-06-22 20:08 +0200
Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-22 15:46 -0400
Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-22 14:00 +0100
Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 14:38 +0100
Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-22 17:26 +0100
Re: Warning: assignment from incompatible pointer type Keith Thompson <kst-u@mib.org> - 2017-06-22 10:21 -0700
Re: Warning: assignment from incompatible pointer type bartc <bc@freeuk.com> - 2017-06-22 18:26 +0100
Re: Warning: assignment from incompatible pointer type David Brown <david.brown@hesbynett.no> - 2017-06-22 20:41 +0200
Re: Warning: assignment from incompatible pointer type Richard Heathfield <rjh@cpax.org.uk> - 2017-06-22 19:59 +0100
Re: Warning: assignment from incompatible pointer type Keith Thompson <kst-u@mib.org> - 2017-06-22 12:28 -0700
Re: Warning: assignment from incompatible pointer type Jerry Stuckle <jstucklex@attglobal.net> - 2017-06-21 09:49 -0400
Re: Warning: assignment from incompatible pointer type DFS <nospam@dfs.com> - 2017-06-21 10:36 -0400
Re: Warning: assignment from incompatible pointer type Jerry Stuckle <jstucklex@attglobal.net> - 2017-06-21 14:15 -0400
Re: Warning: assignment from incompatible pointer type Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-21 15:10 +0100
csiph-web