473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

K&R2 , exercise 7.6

PURPOSE :: see statement in comments

GOT: Segmentation Fault

I guess the segfault is sourced in the compile-time warning but I am
giving a char* to the function already.


/* K&R2, section 7.7, exercise 7.6
*
* write a program to compare 2 files, printing that first line
* where they differ.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum MAXSIZE { ARRSIZE=1000 };

void compare_files( FILE*, FILE* );
void print_line( char* );
int main( int argc, char* argv[] )
{
FILE *pf1, *pf2;

if( argc !=3 )
{
fprintf( stderr, "You iDiOT, I expect 2 files as input. \n" );
exit(EXIT_FAILURE);
}
/* open files */
pf1 = fopen( *++argv, "r" );
pf2 = fopen( *++argv, "r" );

/* error check */
if( pf1 == NULL || pf2 == NULL )
{
fprintf( stderr, "error opening files\n");
exit(EXIT_FAILURE);
}
else
{
compare_files( pf1,pf2 );
}

/* don't forget to close the files */
fclose( pf1 );
fclose( pf2 );
return 0;
}

/* compare 2 files */
void compare_files( FILE* pf1, FILE* pf2 )
{
int c1, c2, match;
char *line1, *line2;
char *begin_line1, *begin_line2;

match = 1;

while( ((line1 = fgets( line1, ARRSIZE, pf1 )) != NULL) ||
((line2 = fgets( line2, ARRSIZE, pf2 )) != NULL))
{
begin_line1 = line1;
begin_line2 = line2;

for( c1 = *line1, c2 = *line2; c1 != '\0' && c2 != '\0'; ++line1,
++line2 )
{
if ( c1 != c2 )
{
match = 0;
print_line( begin_line1 );
printf("\n-----------------------\n"); print_line( begin_line2 );
}
}
}
}

void print_line( char* line )
{
printf("%s\n", *line++);
}

================== OUTPUT ======================
[arnuld@raj C]$ gcc -ansi -pedantic -Wall -Wextra 7-6.c
7-6.c: In function `print_line':
7-6.c:88: warning: format argument is not a pointer (arg 2)

[arnuld@raj C]$ ./a.out
You iDiOT, I expect 2 files as input.

[arnuld@raj C]$ ./a.out 7-6.c 5-4.c
Segmentation fault
--
http://lispmachine.wordpress.com/
my email ID is at the above address

Jun 27 '08
82 2730
santosh <sa*********@gmail.comwrites:
arnuld wrote:
>>On Thu, 24 Apr 2008 06:16:53 +0000, Richard Heathfield wrote:
>>Half-right. A string is a contiguous sequence of characters
terminated by the first null character, whose length is given by the
number of characters in the string not including the null character.
Thus, a string with a null as its first character is still a string,
but it has 0 length.

ah... I think I will better use this for clarity:

*buf = '\0';

No. This assigns the value of the character constant '\0' to buf.
No it does not and I am absolutely sure you know that! My code used

*buf = 0;

because, at least in C, I don't think 0 is much clearer than '\0'.
(People worry about typing = instead of == but how often have you seen
'0' used by accident where '\0' was intended?)

Both mine and the suggested clearer form ensure that buf contains a
string of length zero.
This happens to be the same as doing

*buf = 0;
Yes.
and in a declaration it's the same as

char *buf = NULL;
No, not at all the same thing. Have you not had your coffee yet?
but I don't suppose that this is what you meant. If you want to assign a
pointer to a null string, do

char *buf = "";
Different again and not safe in the context (buf gets passed to fgets
on the next line). I know the context has been snipped, but even
without it

*buf = 0;
char *buf = NULL;
char *buf = "";

are all different enough to be unlikely alternatives to each other.
Arnuld's choice to prefer *buf = '\0'; to my *buf = 0; is fine and
needs no correction.

--
Ben.
Jun 27 '08 #51
Ben Bacarisse wrote:
santosh <sa*********@gmail.comwrites:
>arnuld wrote:
>>>On Thu, 24 Apr 2008 06:16:53 +0000, Richard Heathfield wrote:

Half-right. A string is a contiguous sequence of characters
terminated by the first null character, whose length is given by
the number of characters in the string not including the null
character. Thus, a string with a null as its first character is
still a string, but it has 0 length.

ah... I think I will better use this for clarity:

*buf = '\0';

No. This assigns the value of the character constant '\0' to buf.

No it does not and I am absolutely sure you know that! My code used

*buf = 0;

because, at least in C, I don't think 0 is much clearer than '\0'.
(People worry about typing = instead of == but how often have you seen
'0' used by accident where '\0' was intended?)

Both mine and the suggested clearer form ensure that buf contains a
string of length zero.
>This happens to be the same as doing

*buf = 0;

Yes.
>and in a declaration it's the same as

char *buf = NULL;

No, not at all the same thing. Have you not had your coffee yet?
I posted without reading through the thread. My mistake was in assuming
that arnuld wanted to initialise buf to point to a empty string. Now I
see that buf already points to storage and he wants to place a '\0' at
element zero so that it would be treated as an empty string by other
code.
>but I don't suppose that this is what you meant. If you want to
assign a pointer to a null string, do

char *buf = "";

Different again and not safe in the context (buf gets passed to fgets
on the next line). I know the context has been snipped, but even
without it

*buf = 0;
char *buf = NULL;
char *buf = "";

are all different enough to be unlikely alternatives to each other.
Arnuld's choice to prefer *buf = '\0'; to my *buf = 0; is fine and
needs no correction.
Yes. Apologies to the OP for misreading a statement as an
initialisation.

Jun 27 '08 #52
On Mon, 28 Apr 2008 13:31:18 +0100, Ben Bacarisse wrote:

*buf = 0;
char *buf = NULL;
char *buf = "";

are all different enough to be unlikely alternatives to each other.
*buff = 0;

puts a zero at the place pointed by pointer.

char *buffer = NULL;
char *buffer = 0;
different in what context ? Both are same as of value they contain:

#include <stdio.h>
int main(void) {

char *buffer = 0;
char *buffer2 = NULL;

printf("%s\n%s\n", buffer, buffer2);

return 0;
}

============ OUTPUT ===============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
(null)
(null)
/home/arnuld/programs/C $

Arnuld's choice to prefer *buf = '\0'; to my *buf = 0; is fine and
needs no correction.
:)
--
http://lispmachine.wordpress.com/
my email ID is at the above address

Jun 27 '08 #53
arnuld <No****@NoPain.comwrites:
>On Mon, 28 Apr 2008 13:31:18 +0100, Ben Bacarisse wrote:
*buf = 0;
char *buf = NULL;
char *buf = "";

are all different enough to be unlikely alternatives to each other.

*buff = 0;

puts a zero at the place pointed by pointer.
right, and if buff is of type char*, the int value 0 is converted to
type char.
char *buffer = NULL;
initializes the pointer object ``buffer'' to a null pointer value; it
doesn't store a value in what buffer points to (it can't, since buffer
doesn't point to anything).
char *buffer = 0;
initializes the pointer object ``buffer'' to the value 0, which is
converted to a null pointer value; in other words, it does exactly
the same thing as ``char *buffer = NULL;''.
different in what context ? Both are same as of value they contain:
[...]

0 and '\0', as it happens, are both constants of type int with the
value 0. They're the same as far as the compiler is concerned, but
they're different to human readers. '\0' is a character constant, and
should be used when you specifically what a *character* value of 0.

NULL is a macro that expands to an implementation-defined null pointer
constant; it should be used only when you want a pointer value. If an
implementation happens to define NULL as 0, then you can get away with

char c = NULL;

but it's a bad idea, first because it's misleading to a human reader,
and second because it won't compile if the implementation chooses to
define NULL as ((void*)0) rather than just 0.

C has several different ways to say "zero", but as a matter of style
you should choose one that's appropriate to the type you want: '\0'
for character types, 0 for non-character integer types, 0.0 for
floating-point, NULL for pointer types. The compiler often (not
always, but often) won't care, but code typically spends more of its
time being read by humans than processed by compilers.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #54
On Tue, 29 Apr 2008 00:29:49 -0700, Keith Thompson wrote:
>arnuld <No****@NoPain.comwrites:
>*buff = 0;

puts a zero at the place pointed by pointer.
right, and if buff is of type char*, the int value 0 is converted to
type char.
that implicit conversion then. As a matter of style, I always make
conversions explicit (except for malloc).
... SNIP......
but it's a bad idea, first because it's misleading to a human reader,
and second because it won't compile if the implementation chooses to
define NULL as ((void*)0) rather than just 0.
since NULL can be either 0 or <void*then how come:
char *buffer = NULL;
char *buffer = 0;

are equivalent ?
if <NULL == 0then they are equivalent but if <NULL == void*then they
are different assignments .

C has several different ways to say "zero", but as a matter of style you
should choose one that's appropriate to the type you want: '\0' for
character types, 0 for non-character integer types, 0.0 for
floating-point, NULL for pointer types. The compiler often (not always,
but often) won't care, but code typically spends more of its time being
read by humans than processed by compilers.


facts I did not know. Thanks :)

--
http://lispmachine.wordpress.com/
my email ID is at the above address

Jun 27 '08 #55
arnuld said:

<snip>
As a matter of style, I always make
conversions explicit (except for malloc).
That is almost always a bad idea, because it can suppress diagnostic
messages about type matching violations.
<snip>
since NULL can be either 0 or <void*>
No. NULL may defined either as 0, or as ((void *)0). It must not be defined
as <void *>.
then how come:
char *buffer = NULL;
char *buffer = 0;

are equivalent ?
0 is a null pointer constant. NULL is a null pointer constant. Each of the
above definitions gives buffer a value that is a null pointer.
if <NULL == 0then they are equivalent but if <NULL == void*then they
are different assignments .
Consider:

int m = 6;
int m = 3 + 3;

Are these equivalent? Do they have the same effect on m? If not, how do
they vary? What value will m get in the second definition that is somehow
different to the value it gets in the first? Thinking about this very
similar issue should enable you to understand the null pointer assignments
quoted above.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #56
On 29 Apr, 14:38, arnuld <NoS...@NoPain.comwrote:

<snip>
since NULL can be either 0 or <void*then how come:
no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?

<snip>
--
Nick Keighley
Jun 27 '08 #57
On Tue, 29 Apr 2008 01:30:35 -0700, Nick Keighley wrote:
no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?

I did read the FAQ but the concept of NULL, NUL, '\0', ((void*) 0) and 0
is too complex and confusing to be grasped by oneself by just reading the
FAQs

--
http://lispmachine.wordpress.com/
my email ID is at the above address

Jun 27 '08 #58
On Tue, 29 Apr 2008 08:27:36 +0000, Richard Heathfield wrote:

That is almost always a bad idea, because it can suppress diagnostic
messages about type matching violations.
well, the programmer knows that he *intentionally* casted the type.
0 is a null pointer constant. NULL is a null pointer constant. Each of
the above definitions gives buffer a value that is a null pointer.
from FAQ, I can conclude that, use of NULL is ok where I need a <void*>
and for everything else I can use a 0 (zero).

int m = 6;
int m = 3 + 3;

Are these equivalent? Do they have the same effect on m? If not, how do
they vary? What value will m get in the second definition that is
somehow different to the value it gets in the first? Thinking about this
very similar issue should enable you to understand the null pointer
assignments quoted above.

compiler will initialize <mto a vale of 6. Compiler will get there
differently in both expressions but the end result will be <mwill be
equal 6.

Right ?
--
http://lispmachine.wordpress.com/
my email ID is at the above address

Jun 27 '08 #59
arnuld <No****@NoPain.comwrites:
>On Tue, 29 Apr 2008 00:29:49 -0700, Keith Thompson wrote:
>>arnuld <No****@NoPain.comwrites:

>>*buff = 0;

puts a zero at the place pointed by pointer.
>right, and if buff is of type char*, the int value 0 is converted to
type char.

that implicit conversion then. As a matter of style, I always make
conversions explicit (except for malloc).
No you don't. Your '\0' is of type int. It gets converted to char
int just the same way my int value 0 gets converted to char when
stored. If you want to make all conversions explicit you would have
to write:

*buf = (char)'\0';

And lets not even start on the number of different things you'll have
to put in front of NULL when you start using dynamic data structures!
I suggest you get happy with C's implicit conversions before madness
set in.

--
Ben.
Jun 27 '08 #60
arnuld <No****@NoPain.comwrites:
>On Mon, 28 Apr 2008 13:31:18 +0100, Ben Bacarisse wrote:
> *buf = 0;
char *buf = NULL;
char *buf = "";

are all different enough to be unlikely alternatives to each other.

*buff = 0;

puts a zero at the place pointed by pointer.

char *buffer = NULL;
char *buffer = 0;

different in what context ?
For the record, I never suggested they were different. I gave three
examples which *are* all different, but 'char *buf = 0;' was not one
of them. If you are asking a new question, you've had a very full
answer from Keith Thompson.

--
Ben.
Jun 27 '08 #61
On Tue, 29 Apr 2008 01:30:35 -0700, Nick Keighley wrote:
no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?

with some hard-thought I came to understand the distinction between NULL
and 0:
1.) In case of pointers, use NULL.
2 .) In cases where integral zero is requires, use 0.

except these 2 points, to me, every other discussion about NULL and 0 does
not seem important to me from programming in C perspective.

--
http://lispmachine.wordpress.com/
my email ID is at the above address

Jun 27 '08 #62
arnuld said:
>On Tue, 29 Apr 2008 08:27:36 +0000, Richard Heathfield wrote:

>That is almost always a bad idea, because it can suppress diagnostic
messages about type matching violations.

well, the programmer knows that he *intentionally* casted the type.
So what? It's still (almost always) a daft idea.

<snip>

[0 vs NULL]
Compiler will get there
differently in both expressions but the end result will be [...]
equal [...]

Right ?
Right.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #63
arnuld <No****@NoPain.comwrites:
>On Tue, 29 Apr 2008 01:30:35 -0700, Nick Keighley wrote:
>no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?


I did read the FAQ but the concept of NULL, NUL, '\0', ((void*) 0) and 0
is too complex and confusing to be grasped by oneself by just reading the
FAQs
Most of answers are in the FAQ, but I dont know why someone answers
so. Why have a newsgroup if the people to help are asking this?
Jun 27 '08 #64
arnuld wrote:
>On Tue, 29 Apr 2008 01:30:35 -0700, Nick Keighley wrote:
>no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?


I did read the FAQ but the concept of NULL, NUL, '\0', ((void*) 0) and
0 is too complex and confusing to be grasped by oneself by just
reading the FAQs
WRT to null pointers maybe these historical c.l.c threads can shed some
light for you...

<http://groups.google.com/group/comp.lang.c/browse_thread/thread/47ccaa72c414a0e>
<http://groups.google.com/group/comp.lang.c/browse_thread/thread/9a06a227e2ca1460>

NULL vs. 0 has been been debated often in c.l.c. You might want to do
your own search too.

Jun 27 '08 #65
arnuld <No****@NoPain.comwrites:
>On Tue, 29 Apr 2008 08:27:36 +0000, Richard Heathfield wrote:
>That is almost always a bad idea, because it can suppress diagnostic
messages about type matching violations.

well, the programmer knows that he *intentionally* casted the type.
It's still a bad idea. Why? Because implicit conversions are more
tightly restricted than casts (explicit conversions).

The language allows implicit conversions between closely related
types, and most of the time the conversion will be exactly the one you
really want. The language allows explicit conversions (casts) between
more distantly related types. If you use a cast, it's entirely up to
you, the programmer, to get the type right. If you get the type
wrong, you'll introduce a bug which the compiler will be unable to
diagnose for you.

In the example from a previous post, suppose you write this:

int n = some_value;
*buff = (char)n;

Is this correct? You don't know unless you know the type of buff. If
buff is a char*, then it's just fine -- and the cast is unnecessary;
you could have written
*buff = n;
and let the compiler generate the conversion for you. But if buff is
unsigned char*, or short*, or double*, then you could lose
information.

If you get the type right, the cast is harmless and completely
unnecessary. If you get the type wrong (perhaps because you changed
the declaration of buff two years after you wrote the original code),
then the cast causes a bug that could have been avoided by omitting
the cast in the first place.

Now explain to me again why the cast is a good idea.

Note that a cast can sometimes be necessary when you're passing an
argument to a variadic function such as printf. In that case, the
compiler *doesn't* know the target type, so it can't generate the
implicit conversion for you. In that one case, you just have to be
very careful to get the type right, because the compiler most likely
won't tell you if you get it wrong. An example:

printf("sizeof(double) = %d\n", (int)sizeof(double));

The result of sizeof(double) is of type size_t, but printf with "%d"
requires an int argument.
>0 is a null pointer constant. NULL is a null pointer constant. Each of
the above definitions gives buffer a value that is a null pointer.

from FAQ, I can conclude that, use of NULL is ok where I need a <void*>
and for everything else I can use a 0 (zero).
No.

What do you mean by <void*>? What are the angle brackets supposed to
indicate?

NULL is a macro that expands to a null pointer constant. You should
use it when you want a null pointer of *any* pointer type. (void* is
just one of many pointer types.)

There are a number of ways that NULL can be defined. If you use it
properly, you don't have to care just how it's defined; your code will
work correctly whether it expands to 0, ((void*)0), 0x00000000, or
__MAGIC_NULL_POINTER_CONSTANT__.

That's why
char *buffer = NULL;
and
char *buffer = 0;
are equivalent.

Note that 0 and NULL are *not* equivalent in all contexts; they're
equivalent only in contexts that require a pointer value. NULL should
*only* be used in contexts that require a pointer value. The point of
using NULL rather than 0 is to make it clear to the human reader that
you have a null pointer constant, not just an integer value of 0.

[snip]>int m = 6;
>int m = 3 + 3;

Are these equivalent? Do they have the same effect on m? If not, how do
they vary? What value will m get in the second definition that is
somehow different to the value it gets in the first? Thinking about this
very similar issue should enable you to understand the null pointer
assignments quoted above.

compiler will initialize <mto a vale of 6. Compiler will get there
differently in both expressions but the end result will be <mwill be
equal 6.

Right ?
Of course.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #66
Eligiusz Narutowicz<el*************@hotmail.comwrites:
arnuld <No****@NoPain.comwrites:
>>On Tue, 29 Apr 2008 01:30:35 -0700, Nick Keighley wrote:
>>no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?


I did read the FAQ but the concept of NULL, NUL, '\0', ((void*) 0) and 0
is too complex and confusing to be grasped by oneself by just reading the
FAQs

Most of answers are in the FAQ, but I dont know why someone answers
so. Why have a newsgroup if the people to help are asking this?
There are plenty of questions whose answers aren't in the FAQ. And
even for those that are, sometimes more explanation is required.

Steve Summit and his contributors expended a great deal of time and
effort composing the FAQ. As a result, it contains excellent answers
to a many of the questions that are asked here. If someone is having
a problem with arrayd and pointers, I *could* spend 10 minutes
composing a lengthy response that explains how they relate to each
other (with a significant risk of getting something wrong and
misleading the questioner). Or I can say "Read section 6 of the
comp.lang.c FAQ", saving myself considerable time and giving the the
questioner a better answer than I could have written myself.

The FAQ hasn't caused this newsgroup to suffer from a lack of things
to talk about.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #67
arnuld <No****@NoPain.comwrites:
>On Tue, 29 Apr 2008 01:30:35 -0700, Nick Keighley wrote:
>no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?


with some hard-thought I came to understand the distinction between NULL
and 0:
1.) In case of pointers, use NULL.
2 .) In cases where integral zero is requires, use 0.

except these 2 points, to me, every other discussion about NULL and 0 does
not seem important to me from programming in C perspective.
That's not the whole picture, but yes, it's basically correct.

(Except that it's best to use '\0' when you want a character with
value 0, and 0.0 when you want a floating-point zero.)

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #68
arnuld <No****@NoPain.comwrites:
>On Tue, 29 Apr 2008 01:30:35 -0700, Nick Keighley wrote:
no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?

I did read the FAQ but the concept of NULL, NUL, '\0', ((void*) 0) and 0
is too complex and confusing to be grasped by oneself by just reading the
FAQs
NULL is a macro that expands to a null pointer constant. Don't worry
about just what it expands to. You can use NULL in any context where
you need a null pointer of *any* pointer type. (But see my caveat in
another article in this thread about variadic functions like printf.)

NUL is not defined in C. It's sometimes used as a name for the
character with value 0, but you can and should just use a literal '\0'
instead.

'\0' is a character constant with value 0, also known as the null
character (*not* to be confused with a null pointer). This is the
value used to terminate a string. (Due to a quirk of the language,
character constants are actually of type int, not char. Don't worry
about that; due to implicit conversions, it rarely matters.)

((void*)0) and 0 are two possible expansions of the NULL macro.
They're both null pointer constants. Don't worry about using either
of them; just use NULL.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #69
Eligiusz Narutowicz wrote:

Most of answers are in the FAQ, but I dont know why someone answers
so. Why have a newsgroup if the people to help are asking this?

What is the reason for having a newsgroup Frequently Asked Question
list?


Brian
Jun 27 '08 #70
"Default User" <de***********@yahoo.comwrites:
Eligiusz Narutowicz wrote:

>Most of answers are in the FAQ, but I dont know why someone answers
so. Why have a newsgroup if the people to help are asking this?


What is the reason for having a newsgroup Frequently Asked Question
list?


Brian
I think that is very obvious. I don't know your point? So you will only
point people to the FAQ? Why do you post here then?
Jun 27 '08 #71
On Apr 29, 11:38*am, Eligiusz Narutowicz<eligiuszdotn...@hotmail.com>
wrote:
"Default User" <defaultuse...@yahoo.comwrites:
Eligiusz Narutowicz wrote:
Most of answers are in the FAQ, but I dont know why someone answers
so. Why have a newsgroup if the people to help are asking this?
What is the reason for having a newsgroup Frequently Asked Question
list?
Brian

I think that is very obvious. I don't know your point? So you will only
point people to the FAQ?
If a well prepared and correct answer is found in the FAQ, wouldn't it
be a nice place to point people?
Why do you post here then?
Brian posts here to be helpful. On those occasions when something is
not contained in the FAQ, he will post pertinant information regarding
the issue at hand.

Pretty much all usenet groups have FAQs and it is simple common
courtesy to read them before posting.
http://www.uiowa.edu/~writingc/handouts/netiquette.htm
http://www.albion.com/bookNetiquette/0963702513p32.html
http://www.walthowe.com/navnet/faq/guidelines.html
http://en.wikipedia.org/wiki/FAQ

HTH
Jun 27 '08 #72
user923005 <dc*****@connx.comwrites:
On Apr 29, 11:38Â*am, Eligiusz Narutowicz<eligiuszdotn...@hotmail.com>
wrote:
>"Default User" <defaultuse...@yahoo.comwrites:
Eligiusz Narutowicz wrote:
>Most of answers are in the FAQ, but I dont know why someone answers
so. Why have a newsgroup if the people to help are asking this?
What is the reason for having a newsgroup Frequently Asked Question
list?
Brian

I think that is very obvious. I don't know your point? So you will only
point people to the FAQ?

If a well prepared and correct answer is found in the FAQ, wouldn't it
be a nice place to point people?
>Why do you post here then?

Brian posts here to be helpful. On those occasions when something is
not contained in the FAQ, he will post pertinant information regarding
the issue at hand.

Pretty much all usenet groups have FAQs and it is simple common
courtesy to read them before posting.
http://www.uiowa.edu/~writingc/handouts/netiquette.htm
http://www.albion.com/bookNetiquette/0963702513p32.html
http://www.walthowe.com/navnet/faq/guidelines.html
http://en.wikipedia.org/wiki/FAQ

HTH
I am sorry but I know what a FAQ is. But one can point nearly all people
to the FAQ. Nicholas was abrupt I thought in his "have you bothered to
read the faq" comment. It was just my opinions that this is not the
right answer. One can always nicely refer people to it, but people can
not always find what they need if they dont know what the problem
is. Just my 2c of course. I think it is rude and you may of course think
otherwise. I didnt see Brian posting too much help only telling people
off so sorry if I was wrong.
Jun 27 '08 #73
On Apr 29, 12:14*pm, Eligiusz Narutowicz<eligiuszdotn...@hotmail.com>
wrote:
user923005 <dcor...@connx.comwrites:
On Apr 29, 11:38*am, Eligiusz Narutowicz<eligiuszdotn...@hotmail.com>
wrote:
"Default User" <defaultuse...@yahoo.comwrites:
Eligiusz Narutowicz wrote:
Most of answers are in the FAQ, but I dont know why someone answers
so. Why have a newsgroup if the people to help are asking this?
What is the reason for having a newsgroup Frequently Asked Question
list?
Brian
I think that is very obvious. I don't know your point? So you will only
point people to the FAQ?
If a well prepared and correct answer is found in the FAQ, wouldn't it
be a nice place to point people?
Why do you post here then?
Brian posts here to be helpful. *On those occasions when something is
not contained in the FAQ, he will post pertinant information regarding
the issue at hand.
Pretty much all usenet groups have FAQs and it is simple common
courtesy to read them before posting.
http://www.uiowa.edu/~writingc/handouts/netiquette.htm
http://www.albion.com/bookNetiquette/0963702513p32.html
http://www.walthowe.com/navnet/faq/guidelines.html
http://en.wikipedia.org/wiki/FAQ
HTH

I am sorry but I know what a FAQ is. But one can point nearly all people
to the FAQ. Nicholas was abrupt I thought in his "have you bothered to
read the faq" comment.
One problem with printed media is that it is hard to judge intent and
feeling. Sometimes we read insult between the lines. That is simply
human nature. The fundamental rule of posting on the internet is:
1. Before posting to a USENET news group, grow a skin at least two
inches (five centimeters) thick.
It was just my opinions that this is not the
right answer. One can always nicely refer people to it, but people can
not always find what they need if they dont know what the problem
is.
Perhaps not. But it is a good idea to try that first.
Just my 2c of course. I think it is rude and you may of course think
otherwise.
It is never rude to refer people to the FAQ (unless it is done in this
way):
"READ THE FAQ YOU #@$%&*~ WHINGING TWIT!"
On the other hand, it is rude to post to a USENET news group without
reading the FAQ.
Some people don't know that, so we say things like:
"Read the FAQ, and you'll get whiter teeth and fresher breath!"
and:
"Why not read the FAQ? David Beckham read it and look at him now!"
I didnt see Brian posting too much help only telling people
off so sorry if I was wrong.
He has been posting here for quite a long time.
Jun 27 '08 #74
Eligiusz Narutowicz wrote:
"Default User" <de***********@yahoo.comwrites:
Eligiusz Narutowicz wrote:

Most of answers are in the FAQ, but I dont know why someone answers
so. Why have a newsgroup if the people to help are asking this?

What is the reason for having a newsgroup Frequently Asked Question
list?
I think that is very obvious.
Is it? What DO you think the purpose is?
I don't know your point?
My point is, the FAQs are there for a reason, and that is to answer the
common questions.
So you will
only point people to the FAQ? Why do you post here then?
Because not all situations are covered by the FAQs, of course.


Brian
Jun 27 '08 #75
Eligiusz Narutowicz<el*************@hotmail.comwrites:
[...]
I am sorry but I know what a FAQ is. But one can point nearly all people
to the FAQ. Nicholas was abrupt I thought in his "have you bothered to
read the faq" comment. It was just my opinions that this is not the
right answer. One can always nicely refer people to it, but people can
not always find what they need if they dont know what the problem
is. Just my 2c of course. I think it is rude and you may of course think
otherwise. I didnt see Brian posting too much help only telling people
off so sorry if I was wrong.
Many questions are answered in the FAQ, but not all are. Thus the FAQ
and the newsgroup nicely complement each other. As I'm sure you know.

To be precise, what Nick wrote (about 7 articles upthread from this
one) was not "have you bothered to read the faq", but "Have you read
the comp.lang.c FAQ?". It was a reasonable question in context, and I
saw nothing rude about it. It was in direct response to an article in
which a poster badly misstated the possible expansions of the NULL
macro; that article in turn was in direct response to another article,
which it quoted, in which two of the likely expansions of the NULL
macro had been stated correctly. IMHO Nick showed admirable restraint
given the circumstances.

More generally, it could be argued that flaming someone for posting
without reading the FAQ is rude. It could also be argued that posting
without reading the FAQ is rude. I will not state a position on
either of those questions. There are posters here who are, in my
opinion, repeatedly and unnecessarily rude. There are others who are,
in my opinion, falsely accused of being unnecessarily rude. I haven't
found discussing the issue to be a productive use of my time or of the
newsgroup's bandwidth.

My advice: try not to be offensive, and try not to be offended.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #76
On Tue, 29 Apr 2008 15:47:30 +0500, arnuld <No****@NoPain.comwrote:
>On Mon, 28 Apr 2008 13:31:18 +0100, Ben Bacarisse wrote:

> *buf = 0;
char *buf = NULL;
char *buf = "";

are all different enough to be unlikely alternatives to each other.

*buff = 0;

puts a zero at the place pointed by pointer.

char *buffer = NULL;
char *buffer = 0;
different in what context ? Both are same as of value they contain:

#include <stdio.h>
int main(void) {

char *buffer = 0;
char *buffer2 = NULL;

printf("%s\n%s\n", buffer, buffer2);
Obviously you wanted to do something else rather than invoke undefined
behavior here. Did you mean to use %p instead of %s? If so, it would
be a good to acquire the habit of casting the pointers to void* (even
though a char* would work without the cast).
>
return 0;
}

============ OUTPUT ===============
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
(null)
(null)
/home/arnuld/programs/C $

>Arnuld's choice to prefer *buf = '\0'; to my *buf = 0; is fine and
needs no correction.

:)

Remove del for email
Jun 27 '08 #77
On Tue, 29 Apr 2008 18:34:16 -0700, Barry Schwarz wrote:
>On Tue, 29 Apr 2008 15:47:30 +0500, arnuld <No****@NoPain.comwrote:
> char *buffer = 0;
char *buffer2 = NULL;

printf("%s\n%s\n", buffer, buffer2);
Obviously you wanted to do something else rather than invoke undefined
behavior here. Did you mean to use %p instead of %s? If so, it would
be a good to acquire the habit of casting the pointers to void* (even
though a char* would work without the cast).
I really meant %s because I am providing printf an array, whether empty or
full and you do see that its prints without any problem:
/home/arnuld/programs/C $ gcc -ansi -pedantic -Wall -Wextra test.c
/home/arnuld/programs/C $ ./a.out
(null)
(null)
/home/arnuld/programs/C $

BTW, I used %d, %lu, %s but what is %p ?
--
http://lispmachine.wordpress.com/
my email ID is at the above address

Jun 27 '08 #78
arnuld wrote:
>On Tue, 29 Apr 2008 18:34:16 -0700, Barry Schwarz wrote:
>>On Tue, 29 Apr 2008 15:47:30 +0500, arnuld <No****@NoPain.com>
wrote:
>> char *buffer = 0;
char *buffer2 = NULL;

printf("%s\n%s\n", buffer, buffer2);
>Obviously you wanted to do something else rather than invoke
undefined
behavior here. Did you mean to use %p instead of %s? If so, it
would be a good to acquire the habit of casting the pointers to void*
(even though a char* would work without the cast).

I really meant %s because I am providing printf an array, whether
empty or full and you do see that its prints without any problem:
No. That's GNU C specific behaviour. You are confused between
initialising a pointer and later assigning to it. This

char *buf = 0;

is a declaration with an initialisation. It declares buf to a char * and
sets it to 0, which in a pointer context is automatically converted to
a null pointer value. buf is, after this initialisation, a null
pointer, something that is never legal to deference. The %s format
specifier needs a valid pointer pointing to a string, or a single null
character. The behaviour is undefined if a null pointer is passed to
it. It so happens that the GNU C compiler prints (null), but some other
compiler could merely segfault or do something even worse.

This series of statements

char *buf = 0;
buf = "";

declare buf as a char * and first set it to NULL. Then the second
statement sets buf to point to the start of an empty string, which is
simply a single null character. It is equivalent to this code:

char nullc = '\0'
char *buf = 0;
buf = &nullc;

with the caveat that the storage pointed to by the example above is
modifiable while the one preceding it is not. That's because string
literals are not, (or should not be), modifiable in C. Another
functionally equivalent code is:

char *buf = NULL;
buf = malloc(1 * sizeof *buf);
/* check malloc return */
*buf = 0;

So, in summary assigning a zero value to a pointer in an initialisation
is very different from assigning to it after deferencing it.

Can you understand the difference between these?

1. char *buf = 0;

2. char *buf;
buf = NULL;

3. char *buf;
char c = '\0';
buf = &c;

4. char *buf = NULL;
buf = "";

Jun 27 '08 #79
arnuld wrote:

<snip>
BTW, I used %d, %lu, %s but what is %p ?
Conversion specifier to print a pointer value in a implementation
defined format. The corresponding argument should be cast to a void *
value for maximum portability.

The scanf family of functions also recognise %p and the corresponding
argument should be the address of a void *. The input must be in the
same format that printf outputs for a %p invocation.

Jun 27 '08 #80
On 29 Apr, 20:14, Eligiusz Narutowicz<eligiuszdotn...@hotmail.com>
wrote:
user923005 <dcor...@connx.comwrites:
On Apr 29, 11:38 am, Eligiusz Narutowicz<eligiuszdotn...@hotmail.com>
wrote:
"Default User" <defaultuse...@yahoo.comwrites:
Eligiusz Narutowicz wrote:
Most of answers are in the FAQ, but I dont know why someone answers
so. Why have a newsgroup if the people to help are asking this?
What is the reason for having a newsgroup Frequently Asked Question
list?
I think that is very obvious. I don't know your point? So you will only
point people to the FAQ?
If a well prepared and correct answer is found in the FAQ, wouldn't it
be a nice place to point people?
Why do you post here then?
Brian posts here to be helpful. On those occasions when something is
not contained in the FAQ, he will post pertinant information regarding
the issue at hand.
Pretty much all usenet groups have FAQs and it is simple common
courtesy to read them before posting.

I am sorry but I know what a FAQ is. But one can point nearly all people
to the FAQ. Nicholas [...]
I prefer Nick to Nicholas.
[...] was abrupt I thought in his "have you bothered to
read the faq" comment.
I didn't intend to be abrupt but succinct. Note you have misquoted
me. What I actually wrote was:
>since NULL can be either 0 or <void*then how come:
no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?
what you said I wrote borders on misrepresentation.
I corrected a (possible) misconception and then pointed him
at the FAQ. I did this because I believe NULL, 0, nil etc.
are confusing and the FAQ explains them well. Ben *may*
not have read these excellent explanations so I directed him
to them. What I understand of the issue is from reading the
FAQ. Why would I reproduce (badly) what has already been
done by a smarter person than myself (the FAQ writer)?

It was just my opinions that this is not the
right answer. One can always nicely refer people to it, but people can
not always find what they need if they dont know what the problem
is. Just my 2c of course. I think it is rude and you may of course think
otherwise. I didnt see Brian posting too much help only telling people
off so sorry if I was wrong
well rudeness is (partly) in the eye of the beholder.
--
Nick Keighley

I only see so far because I stand upon the shoulders of FAQ writers
Jun 27 '08 #81
Nick Keighley wrote:
On 29 Apr, 20:14, Eligiusz Narutowicz<eligiuszdotn...@hotmail.com>
wrote:
>user923005 <dcor...@connx.comwrites:
On Apr 29, 11:38 am, Eligiusz
Narutowicz<eligiuszdotn...@hotmail.comwrote:
"Default User" <defaultuse...@yahoo.comwrites:
Eligiusz Narutowicz wrote:

>Most of answers are in the FAQ, but I dont know why someone
answers so. Why have a newsgroup if the people to help are
asking this?
What is the reason for having a newsgroup Frequently Asked
Question list?
>I think that is very obvious. I don't know your point? So you will
only point people to the FAQ?
If a well prepared and correct answer is found in the FAQ, wouldn't
it be a nice place to point people?
>Why do you post here then?
Brian posts here to be helpful. On those occasions when something
is not contained in the FAQ, he will post pertinant information
regarding the issue at hand.
Pretty much all usenet groups have FAQs and it is simple common
courtesy to read them before posting.

I am sorry but I know what a FAQ is. But one can point nearly all
people to the FAQ. Nicholas [...]

I prefer Nick to Nicholas.
>[...] was abrupt I thought in his "have you bothered to
read the faq" comment.

I didn't intend to be abrupt but succinct. Note you have misquoted
me. What I actually wrote was:
>>since NULL can be either 0 or <void*then how come:
no! NULL can be defined as ((void*)0) (note the zero at the end).
Have you read the comp.lang.c FAQ?

what you said I wrote borders on misrepresentation.
I corrected a (possible) misconception and then pointed him
at the FAQ. I did this because I believe NULL, 0, nil etc.
are confusing and the FAQ explains them well. Ben *may*
not have read these excellent explanations so I directed him
to them.
Note that you actually responded to arnuld. I don't think either Ben of
this group would need to consult the FAQ!

<snip>

Jun 27 '08 #82
Nick Keighley <ni******************@hotmail.comwrites:
I corrected a (possible) misconception and then pointed him
at the FAQ. I did this because I believe NULL, 0, nil etc.
are confusing and the FAQ explains them well. Ben *may*
not have read these excellent explanations so I directed him
to them.
I presume you mean Arnuld not Ben. Your reply was to him (and, yes, I
have read the FAQ).

--
Ben.
Jun 27 '08 #83

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Chris Readle | last post by:
Ok, I've just recently finished a beginning C class and now I'm working through K&R2 (alongside the C99 standard) to *really* learn C. So anyway, I'm working on an exercise in chapter one which...
16
by: Josh Zenker | last post by:
This is my attempt at exercise 1-10 in K&R2. The code looks sloppy to me. Is there a more elegant way to do this? #include <stdio.h> /* copies input to output, printing */ /* series of...
2
by: arnuld | last post by:
there is a solution on "clc-wiki" for exercise 1.17 of K&R2: http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_1:Exercise_17 i see this uses pointers whereas K&R2 have not discussed pointers...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
4
by: arnuld | last post by:
as i said, i have restarted the book because i overlooked some material. i want to have some comments/views on this solution. it runs fine, BTW. ------------------ PROGRAMME -------------- /*...
16
by: arnuld | last post by:
i have created solution which compiles and runs without any error/ warning but it does not work. i am not able to understand why. i thought it is good to post my code here for correction before...
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
16
by: arnuld | last post by:
i am not able to make it work. it compiles without any error but does not work: what i WANTED: 1.) 1st we will take the input in to an array. (calling "getline" in "main") 2.) we will print...
88
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.