Problem with strcat, strcpy,sprintf

M

Mark McIntyre

Not if you understand what a "string" is. (You're not restricted to
explaining just one thing at a time.)

I've noticed when teaching undergrads (and new staff) that the
teach-several-things-at-once approach is fraught with peril... :)
 
K

Keith Thompson

Mark McIntyre said:
I've noticed when teaching undergrads (and new staff) that the
teach-several-things-at-once approach is fraught with peril... :)

Perhaps. Is the students-don't-know-what-strings-are-so-pretend-
their-misconceptions-are-correct approach fraught with less peril?
 
M

Mark McIntyre

Perhaps. Is the students-don't-know-what-strings-are-so-pretend-
their-misconceptions-are-correct approach fraught with less peril?

Pejorative, rigged question and therefore not worthy of an answer. :)

Ignoring your question, I'd say its much less fraught with peril to
start with fundamentals so that by the time the students get to such
complexities as the = operator, they already don't think strings are
PODs.
 
K

Keith Thompson

Mark McIntyre said:
Pejorative, rigged question and therefore not worthy of an answer. :)

I see the smiley, but the question was based directly on what you
wrote upthread. It may have been a bit pejorative, but it was not
rigged, at least not deliberately.

Mark:
] >> Remember that in C, = is not the copy operator, its the assignment
] >> operator. For pointer types, this sets the pointers to point to the
] >> same place. It does /not/ copy the contents.
] >[...]
Keith:
] >I don't think I'd phrase it that way.
Mark:
] Yeah, yours is perhaps more correct phrasing, but IMO that would have
] totally confused anyone who thought of "string" as an actual type.
] Since this is a very common newby view, I deliberately chose different
] wording.

It certainly appeared to me that you were assuming certain
misconceptions about what strings are and deliberately not correcting
them for the sake of an explanation of what assignment does. For
example, I found your use of the word "contents" misleading; the
content of a pointer is an address, not the thing it points to.
Saying that "=" is not the copy operator" is not a simplification;
it's just incorrect. It might be a convenient fiction that can help
someone understand that in
char *a = "hello";
char *b;
b = a;
the assignment doesn't copy the string -- but it will inevitably lead
to confusion later on.

There are times when it's appropriate to give not-quite-correct
explanations early on, to be refined later. I don't think this is one
of those cases. The subtle relationship between arrays and pointers
is so central to the way C works that understanding it is an absolute
prerequesite to having a real understanding of the language.
Ignoring your question, I'd say its much less fraught with peril to
start with fundamentals so that by the time the students get to such
complexities as the = operator, they already don't think strings are
PODs.

(POD being Plain Old Data, I presume; I think that's mostly a C++
term, so some here might not be familiar with it.)

I agree with you, but that's not the situation here. The question is
how to explain pointer assignment to someone who *already*
misunderstands C strings. You can give a quick and misleading
explanation that lets the newbie *think* he understands how a
particular program works, or you can give a longer and more correct
explanation, or at least a pointer to one (such as, "read section 6 of
the C FAQ").
 
G

Greg Comeau

Perhaps. Is the students-don't-know-what-strings-are-so-pretend-
their-misconceptions-are-correct approach fraught with less peril?

I find teaching to misconceptions misconcepted and so always teach
the truth. The "small lies" (and big ones) just never make any sense
to me when I hear others saying that they teach that way.
 

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

Similar Threads

strcpy/strcat 2
Problem with sprintf 1
sprintf 15
[newbie] strcpy, strtok and strcat problem... 16
strcpy question 15
Partial string loss with sprintf/strcat 9
strcpy and strcat problem 23
strcat problem 8

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top