Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Several silly questions

3 views
Skip to first unread message

Sam Hu

unread,
Aug 10, 2009, 5:15:18 PM8/10/09
to
Hello Sirs and Madams,

To save time,forgive me direct getting into the point:I am wondering
whether it is worth to learn C
1.Nowadays there are so many *morden languages* why should one learn
C,esp. there is a C++?
2.Like Erlang,someone told me this language solved mutli-core/threads
issues among other languages,so does that imply C is short to solve
mult-core/threads issues?
3.Like below function,which purpose to me is to create a new string
from an existing one:
3,1,Why key word *restrict* is illegal here? Mingw3.4.5 GCC does not
recognize it?
3.2.It seems that this function does not work properly,since in
below program StringB and StringC both contain garbage
characters.But I don't know why?

void copyRestrict(char* /*restrict*/ p,char* /*restrict*/ q)
{

while(*q)
{
*p++=*q++;
}
p[strlen(p)]='\0';


}

Full source:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void copyRestrict(char* p,char* q)
{

while(*q)
{
*p++=*q++;
}
p[strlen(p)]='\0';


}
void pause()
{
printf("Press any key to continue...");
getchar();

}

char* test()
{
char* msg="msg";
char* result=malloc(sizeof(msg));

strcpy(result,msg);

return result;
}
char* getString(char* const source)
{
char* result=malloc(sizeof(source)+1);
strcpy(result,source);
return result;
}

int main(int argc,char *argv[])
{
char* stringA="This is a string";

char stringB[strlen(stringA)];

char* stringC= malloc(strlen(stringA));
copyRestrict(stringB,stringA);
copyRestrict(stringC,stringA);

printf("stringA=%s\n",stringA);
printf("stringB=%s\n",stringB);

printf("stringC=%s\n",stringC);

char* should=test();
printf("should be \"msg\"?=> %s\n",test());

char* makeAnewString=getString(should);
printf("new string \"msg\"?== %s\n",makeAnewString);

pause();
return 0;
}

4.To copy a string,you may have noticed that I used both strlen() and
sizeof,but I don't really understand what is the proper way to do this
kind of *string copy* manipulations;So I tested in my program it seems
that both char* test() and char* makeAnewString(char* const source)
work well,but I am wondering whether they really work well without any
bugs.

Thank you so much for your help in advance.

Best regards,
Sam
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Dag-Erling Smørgrav

unread,
Aug 11, 2009, 11:05:28 PM8/11/09
to
Sam Hu <samhu...@gmail.com> writes:
> To save time,forgive me direct getting into the point:I am wondering
> whether it is worth to learn C
> 1.Nowadays there are so many *morden languages* why should one learn
> C,esp. there is a C++?

The answer depends on many variables. There are fields where C and C++
are still the dominant languages, and others where they are, at best,
distant memories.

> 2.Like Erlang,someone told me this language solved mutli-core/threads
> issues among other languages,so does that imply C is short to solve
> mult-core/threads issues?

I'm sorry, I don't understand the question.

> 3.Like below function,which purpose to me is to create a new string
> from an existing one:
> 3,1,Why key word *restrict* is illegal here? Mingw3.4.5 GCC does not
> recognize it?

Because restrict is a C99 feature, and GCC 3.4.5 is not a C99 compiler.

> 3.2.It seems that this function does not work properly,since in
> below program StringB and StringC both contain garbage
> characters.But I don't know why?

Because p[strlen(p)] = '\0' doesn't work. The strlen() function only
works with terminated strings, and p isn't terminated - in fact, the
purpose of that statement is to terminate it. Use *p = '\0' instead.

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> void copyRestrict(char* p,char* q)
> {
>
> while(*q)
> {
> *p++=*q++;
> }
> p[strlen(p)]='\0';

See above.

> }
>
> void pause()
> {
> printf("Press any key to continue...");
> getchar();

getchar() is platform-specific.

> }
>
> char* test()
> {
> char* msg="msg";
> char* result=malloc(sizeof(msg));

Wrong. The sizeof operator returns the size of its argument, which in
this case is msg, which is a pointer to char.

It seems to work because, by coincidence, you are running on a machine
where sizeof(msg) is 4, which is the correct value.

You should check the value returned by malloc().

> strcpy(result,msg);
>
> return result;
> }
>
> char* getString(char* const source)
> {
> char* result=malloc(sizeof(source)+1);

Also wrong, for the same reason as above. In this case, it won't even
work by coincidence. Props for the +1, though.

The correct value is strlen(source) + 1 (or, strictly speaking, any
value larger than strlen(source) that will fit in an int).

You should check the value returned by malloc().

> strcpy(result,source);
> return result;
> }
>
> int main(int argc,char *argv[])
> {
> char* stringA="This is a string";

Indeed, but stringA isn't - it's a pointer to your string.

> char stringB[strlen(stringA)];

Wrong: strlen() returns the length of the string minus the terminating
null character. You need to add 1 to make room for it.

> char* stringC= malloc(strlen(stringA));

See above.

> copyRestrict(stringB,stringA);
> copyRestrict(stringC,stringA);
>
> printf("stringA=%s\n",stringA);
> printf("stringB=%s\n",stringB);
>
> printf("stringC=%s\n",stringC);
>
> char* should=test();
> printf("should be \"msg\"?=> %s\n",test());
>
> char* makeAnewString=getString(should);
> printf("new string \"msg\"?== %s\n",makeAnewString);
>
> pause();
> return 0;
> }
>
> 4.To copy a string,you may have noticed that I used both strlen() and
> sizeof,but I don't really understand what is the proper way to do this
> kind of *string copy* manipulations;So I tested in my program it seems
> that both char* test() and char* makeAnewString(char* const source)
> work well,but I am wondering whether they really work well without any
> bugs.

You get bonus points for asking the right questions, and not assuming,
like many do, that because it works, it must be right.

DES
--
Dag-Erling Smørgrav - d...@des.no

Sam Hu

unread,
Aug 21, 2009, 4:09:11 PM8/21/09
to
On 8月12日, 上午11时05分, Dag-Erling Smørgrav <d...@des.no> wrote:
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line.  Sorry.- 隐藏被引用文字 -
>
> - 显示引用的文字 -

Thank you so much for all your detailed and clear answer!!
Regards,
Sam

Curtis Dyer

unread,
Nov 11, 2009, 10:07:00 AM11/11/09
to
On 11 Aug 2009, Dag-Erling Sm�rgrav <d...@des.no>
wrote:

> Sam Hu <samhu...@gmail.com> writes:
>> To save time,forgive me direct getting into the point:I am
>> wondering whether it is worth to learn C
>> 1.Nowadays there are so many *morden languages* why should one
>> learn C,esp. there is a C++?
>
> The answer depends on many variables. There are fields where C
> and C++ are still the dominant languages, and others where they
> are, at best, distant memories.

There are, of course, the cases where you write in a language
simply because you want to. :-)

<snip>

>> 3.Like below function,which purpose to me is to create a new
>> string from an existing one:

Consider the following:

char data[] = "Hello, world!";
char *p = data;

It may be tempting to think that the string data itself was
copied, but this is not the case. We simply get a pointer to the
first element in the `data' array. If you want to actually copy
the string data, you'd need to acquire memory for storing it and
then copy it.

Also, if you have a copy of K&R2, check out Chapter 5, "Pointers
and Arrays." If you don't have a copy, definitely consider
obtaining one.

<snip>

> getchar() is platform-specific.

As far as I can tell, `getchar()' has been defined since ISO C90;
so it should be portable.

<snip>

--
"Don't worry about efficiency until you've attained correctness."
~ Eric Sosman, comp.lang.c

Keith Thompson

unread,
Nov 12, 2009, 12:06:14 AM11/12/09
to
Curtis Dyer <dye...@gmail.com> writes:
> On 11 Aug 2009, Dag-Erling Smørgrav <d...@des.no>
> wrote:
[...]

>> getchar() is platform-specific.
>
> As far as I can tell, `getchar()' has been defined since ISO C90;
> so it should be portable.

I think getchar() is older than that; my copy of K&R1 isn't handy, but
I wouldn't be surprised if it's mentioned there.

Dag-Erling was probably thinking of getch(), which is
platform-specific.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Curtis Dyer

unread,
Nov 12, 2009, 8:26:23 PM11/12/09
to
On 11 Nov 2009, Keith Thompson <ks...@mib.org> wrote:

> Curtis Dyer <dye...@gmail.com> writes:
>> On 11 Aug 2009, Dag-Erling Smørgrav <d...@des.no>
>> wrote:
> [...]
>>> getchar() is platform-specific.
>>
>> As far as I can tell, `getchar()' has been defined since ISO
>> C90; so it should be portable.
>
> I think getchar() is older than that; my copy of K&R1 isn't
> handy, but I wouldn't be surprised if it's mentioned there.

I believe you're right; I was going by my man page for getchar() at
the time.

<snip>

--
Don't worry about efficiency until you've attained correctness.
~ Eric Sosman

Keith Thompson

unread,
Nov 13, 2009, 2:20:29 AM11/13/09
to
Keith Thompson <ks...@mib.org> writes:
> Curtis Dyer <dye...@gmail.com> writes:
>> On 11 Aug 2009, Dag-Erling Smørgrav <d...@des.no>
>> wrote:
> [...]
>>> getchar() is platform-specific.
>>
>> As far as I can tell, `getchar()' has been defined since ISO C90;
>> so it should be portable.
>
> I think getchar() is older than that; my copy of K&R1 isn't handy, but
> I wouldn't be surprised if it's mentioned there.
>
> Dag-Erling was probably thinking of getch(), which is
> platform-specific.

Confirmed: getchar() is mentioned in K&R1 (1978) and in
<http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf> (1975).

In fact, getchar() predates C. It's mentioned in the "Users' Reference
to B" <http://cm.bell-labs.com/cm/cs/who/dmr/kbman.html> (1972).

BCPL, however, appears to have used something called "Readch".

Clive D. W. Feather

unread,
Nov 13, 2009, 8:26:14 PM11/13/09
to
In message <clcm-2009...@plethora.net>, Keith Thompson
<ks...@mib.org> wrote:
>BCPL, however, appears to have used something called "Readch".

Yes: in general the BCPL functions were read* and write*. Hence writef.

--
Clive D.W. Feather | Home: <cl...@davros.org>
Mobile: +44 7973 377646 | Web: <http://www.davros.org>
Please reply to the Reply-To address, which is: <cl...@davros.org>

Ray Kulhanek

unread,
Nov 19, 2009, 8:06:27 AM11/19/09
to
Sam Hu wrote:
> 3.Like below function,which purpose to me is to create a new string
> from an existing one:
> 3,1,Why key word *restrict* is illegal here? Mingw3.4.5 GCC does not
> recognize it?

The restrict keyword was added in c99, and that compiler uses c89 by
default. Add the command line option --std=c99 to get the c99
features.

Dag-Erling Smørgrav

unread,
Nov 19, 2009, 11:11:54 AM11/19/09
to
Ray Kulhanek <kulha...@wright.edu> writes:
> The restrict keyword was added in c99, and [GCC 3.4.5] uses c89 by
> default.

No, it defaults to "gnu89", which is C89 (or something close to it) plus
a number of GNU extensions - and even with "-std=c89", it will accept
some non-C89 features, such as long long.

DES
--
Dag-Erling Smørgrav - d...@des.no

Ray Kulhanek

unread,
Nov 19, 2009, 4:43:44 PM11/19/09
to
Dag-Erling Smørgrav wrote:
> Ray Kulhanek <kulha...@wright.edu> writes:
>> The restrict keyword was added in c99, and [GCC 3.4.5] uses c89 by
>> default.
>
> No, it defaults to "gnu89", which is C89 (or something close to it) plus
> a number of GNU extensions - and even with "-std=c89", it will accept
> some non-C89 features, such as long long.
>
> DES

Apologies; you're correct. It looks like --std=c89 --pedantic will
get strict compliance to c89 (or at least it will complain whenever
a diagnostic is required by the standard).
And likewise --std=c99 --pedantic for c99.

Flash Gordon

unread,
Nov 20, 2009, 4:14:10 PM11/20/09
to
Ray Kulhanek wrote:
> Dag-Erling Smørgrav wrote:
>> Ray Kulhanek <kulha...@wright.edu> writes:
>>> The restrict keyword was added in c99, and [GCC 3.4.5] uses c89 by
>>> default.
>>
>> No, it defaults to "gnu89", which is C89 (or something close to it) plus
>> a number of GNU extensions - and even with "-std=c89", it will accept
>> some non-C89 features, such as long long.
>>
>> DES
>
> Apologies; you're correct. It looks like --std=c89 --pedantic will
> get strict compliance to c89 (or at least it will complain whenever
> a diagnostic is required by the standard).
> And likewise --std=c99 --pedantic for c99.

That won't get you C99, it will only get you as close as that particular
version of gcc+library comes to C99. GNU have not finished implementing
the C99 features of the compiler, and even when they have you still need
a conforming library.

As I understand it, MinGW uses the standard Microsoft C Runtime Library,
and this is only a C90 library, so when gcc fully complies to its parts
of C99 on MinGW you will still only have a C90 library. Also, in terms
of C90 conformance you have a problem, since I believe that there are
some types which are different between the gcc in MinGW and the MS C
Library, so for example some format specifiers for scanf/printf will not
work correctly.
--
Flash Gordon

0 new messages