puzzling error

  • Thread starter Bill Cunningham
  • Start date
S

Seebs

char p[] = "some string";

makes p an array whose contents you're not permitted to modify.

No it doesn't. That's an array whose contents are modifiable, with
an initializer.

Note... I'm actually not totally sure. I'd look it up if I were going
to write code which depended on this. I was having an interesting
conversation with a friend recently. One of the things you face when
programming, especially if you have to use a number of languages, is
that almost nobody can actually remember everything you need to know to
program effectively and reliably.

The secret to success in programming is not to know all this stuff; it's
to cultivate the awareness of when you don't quite know something, compared
to when you're quite sure. Ten years ago, I'd probably have just said
you were wrong. Now I know enough to spot even the faint hint of "wait,
is that right?" floating around.

Being sure of everything makes for horrible code. Being pretty sure most of
the time, but alert to the possibility that you might be misremembering
things, makes for much better programming.

-s
 
K

Keith Thompson

Paul N said:
    char p[] = "some string";

makes p an array whose contents you're not permitted to modify.

Are you sure about that?

No, I'm completely wrong about that.

In fact, p is a 12-element array whose elements may be modified; it's
initialized to a *copy* of the non-modifiable array specified by the
string literal.

On the other hand, this:

char *p = "some string";

makes p point to the first element of an array that you're not
permitted to modify, which is why it's better written as:

const char *p = "some string";

so the compiler will enforce it for you. (The array itself is
anonymous.)

Thank you for catching my stupid mistake -- and please point out the
similar errors I will inevitably have made in this followup.
 
K

Keith Thompson

Seebs said:
char p[] = "some string";

makes p an array whose contents you're not permitted to modify.

No it doesn't. That's an array whose contents are modifiable, with
an initializer.

Acknowledged elsethread, thanks.

[...]
 
A

Angel

char p[] = "some string";

makes p an array whose contents you're not permitted to modify.

No it doesn't. That's an array whose contents are modifiable, with
an initializer.

Note... I'm actually not totally sure. I'd look it up if I were going
to write code which depended on this. I was having an interesting
conversation with a friend recently. One of the things you face when
programming, especially if you have to use a number of languages, is
that almost nobody can actually remember everything you need to know to
program effectively and reliably.

I've tested on GNU/Linux with this short program; the original poster's
program with all the bugs fixed:

#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>

int main(void)
{
char s[] = "abcdef";

puts(strfry(s));
}

As it is now, the program works flawlessly. With the s[] replaced with
*s, it segfaults. So looks to me like you are entirely correct on this
one.


Oh, and I agree with your thoughts on multiple languages. I've meddled
in Basic, Pascal, C, C++, Java, Perl, PHP, Inform, PL/SQL and several
other languages, well known or obscure. Everytime I pic one up again
after a while, I have to sit and think for a moment, "Uh, how did this
one go again?", and I tend to make a lot of mistakes until I get the
hang of it again.
 
B

Bill Cunningham

Ben said:
If you want to find out why, ask in comp.unix.programmer. The answer
is likely to be highly system specific and is not really a C question.
Ok. Maybe a compiler issue?

Bill
 
J

Joachim Schmitz

Bill said:
No I put the define in the middle of the two include directives.

If that didn't help, this would indicate that stdio.h includes string.h, and
the 'include header guard' of string.h prevents it's content from being seen
the 2nd time thru.
The '-D_GNU_SOURCE' is equivalent to a #define at the start of your source

Bye, Jojo
 
K

Keith Thompson

Joachim Schmitz said:
If that didn't help, this would indicate that stdio.h includes string.h, and
the 'include header guard' of string.h prevents it's content from being seen
the 2nd time thru.
The '-D_GNU_SOURCE' is equivalent to a #define at the start of your source

I don't think a conforming stdio.h *can* include string.h.

I suggest that guessing about the consequences of what *might*
be in Bill's source file is a waste of time. If he wants help,
he can post the source (rather than vaguely describing it).

There might (on certain systems) be things in <stdio.h> that are
affected by _GNU_SOURCE. If you're going to #define _GNU_SOURCE,
you should do so at the very top of the source file, before any
#include directives. If you're not doing that, and you're having
problems, moving the #define to the top of the file should be the
first thing you try.
 
B

Bill Cunningham

Kenneth said:
I've checked the man pages of several glibc-based systems and they
all mention the #define.

"man strfry" returns:

==========
STRFRY(3) Linux Programmer's Manual STRFRY(3)


NAME
strfry - randomize a string

SYNOPSIS
#include <string.h>

char *strfry(char *string);

DESCRIPTION
The strfry() function randomizes the contents of string by
using rand(3) to randomly swap characters in the string.
The result is an anagram of string.

RETURN VALUE
The strfry() functions returns a pointer to the randomized
string.

CONFORMING TO
The strfry() function is unique to the Linux C Library and
GNU C Library.

SEE ALSO
memfrob(3)

[...]

GNU April 12, 1993 1

==========

"uname --release" returns "2.4.31-01.grsec.uslecsmp".

My man page has the same date but the kernel is 2.4.20-7. That is the
original kernel that came with the system I have put an updated kernel
2.4.37 into the system too. My linus version is RH 9.0. Long before fedora.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top