design inquiry

I

Ian Collins

Lew Pitcher wrote:

[snip]
cp used to point at the beginning of "stuff". Now, it points to the
character after the last significant character of the filename which
was copied into "stuff". This character /should be/ the '\0'
terminating the copied string.

OK why the * before cp in this examples?

You sure are milking this one, aren't you?
Wow I had no idea.

It shows.
 
L

Lew Pitcher

Lew Pitcher wrote:

[snip]
cp used to point at the beginning of "stuff". Now, it points to the
character after the last significant character of the filename which
was copied into "stuff". This character /should be/ the '\0'
terminating the copied string.

OK why the * before cp in this examples?

cp is a pointer to char.

In order to store a char at the place cp points to, you must "dereference"
the pointer. That's what the * does: it converts a pointer-to-char into the
thing pointed to.

The * allows you to store the '\0' in the char that cp points to.

Now, note that cp is /post-incremented/. The expression
cp++
does two things:
1) it evaluates to the value of cp, and
2) it increments cp *after* that value is evaluated

The statement
*cp++ = '\0';
is equivalent to
*(cp++) = '\0';
which (in turn) is equivalent to
*cp = '\0'; cp = cp + 1;
but, without the sequence point between the two expressions.


[snip]
Wow I had no idea.

Bill
WHere'd you find this from what version of slackware source?

I took the function from the Slackware 12.2 tftp-hpa source package
tftp/tftp.c. The header is part of /usr/include/arpa/tftp.h, which comes
from the Slackware 12.0 glibc-2.7-i486-19_slack12.2.tgz package.

However, both the header and the function are identical to the versions that
come with Slackware 13.1.
 
B

Bill Cunningham

Lew Pitcher wrote:

[snip]
Oops. Let's not propogate Bill's typo.
The original statement was...

*cp++ = '\0';

Not really. This gets down into a part of C that has escaped me. I
understand creating a pointer and using one.

char *cp;
cp=malloc(); for example.

But when does one want to use *cp instead of cp as a pointer? I hope I'm
clear.

Bill
 
K

Keith Thompson

Bill Cunningham said:
Lew Pitcher wrote:
[snip]
Oops. Let's not propogate Bill's typo.
The original statement was...

*cp++ = '\0';

Not really. This gets down into a part of C that has escaped me. I
understand creating a pointer and using one.

char *cp;
cp=malloc(); for example.

You know that malloc requires an argument specifying the number of bytes
to allocate, don't you?

But no, you really don't understand "creating a pointer and
using one". The above creates a pointer and assigns a value to it.
It doesn't use it.
But when does one want to use *cp instead of cp as a pointer? I hope I'm
clear.

This is very fundamental stuff. If you're still having such
difficulty with it after all these years, then I suggest that
dealing with tftp headers is beyond you for the foreseeable future.

You have a long history of starting on some ambitious project,
asking questions about very low-level aspects of the language
(imagine an amateur carpenter trying to build a house asking how
nails work), and then moving on to some other ambitious project
without any sign of having finished the previous one.

You're trying to do something with tftp headers. *What* are you
trying to do with them? If your goal is to write a program,
what exactly will that program do?

But ok, to answer your specific question:

Your question "But when does one want to use *cp instead of cp as
a pointer?" really doesn't make much sense.

cp is a pointer. Specifically, it's a pointer to char.

*cp is the char object that cp points to. *cp is not a pointer,
and cannot be used as one.

Again: cp is a pointer, *cp is not a pointer.
 
B

Bill Cunningham

Keith said:
You know that malloc requires an argument specifying the number of
bytes to allocate, don't you?

Yes yes of course. I just used malloc() as an example. If I remember
right without looking at anything malloc takes a size_t argument and returns
the generic pointer.
But no, you really don't understand "creating a pointer and
using one". The above creates a pointer and assigns a value to it.
It doesn't use it.


This is very fundamental stuff. If you're still having such
difficulty with it after all these years, then I suggest that
dealing with tftp headers is beyond you for the foreseeable future.

You have a long history of starting on some ambitious project,
asking questions about very low-level aspects of the language
(imagine an amateur carpenter trying to build a house asking how
nails work), and then moving on to some other ambitious project
without any sign of having finished the previous one.

That's the word for me amateur. Or hobbyist would be a better term. As
you can see I'm far from a Comp Scientist.
You're trying to do something with tftp headers. *What* are you
trying to do with them? If your goal is to write a program,
what exactly will that program do?

Tftp client.
But ok, to answer your specific question:

Your question "But when does one want to use *cp instead of cp as
a pointer?" really doesn't make much sense.

cp is a pointer. Specifically, it's a pointer to char.

*cp is the char object that cp points to. *cp is not a pointer,
and cannot be used as one.

Hum ok.
 
B

Bill Cunningham

Ok let me look at this again. I have a question about a couple points
here.

cp=(char*)&(tp->th_stuff);

Why exactly is the part of this code right of the ampersand in
parenthesis. I understand the char* cast. And what exactly is the object *cp
?

Bill
 
S

Stefan Ram

Bill Cunningham said:
cp=(char*)&(tp->th_stuff);
Why exactly is the part of this code right of the ampersand in
parenthesis.

These parenthesEs are not necessary.
And what exactly is the object *cp

The object that is pointed to by cp.
 
M

Morris Keesan

cp=(char*)&(tp->th_stuff);

Why exactly is the part of this code right of the ampersand in
parenthesis.

To make it clear to the reader that the & operator is taking the address
of the entire expression (tp->th_stuff). Without the parentheses, the
reader has to know the relative precedence of the & and -> operators, in
order to tell whether &tp->th_stuff is equivalent to the above, or whether
this is supposed to mean (&tp)->th_stuff. Even though the latter is a
nonsensical reading, and the parentheses are not strictly necessary,
sometimes redundant parentheses improve readability.
 
B

Bill Cunningham

Morris said:
To make it clear to the reader that the & operator is taking the
address of the entire expression (tp->th_stuff). Without the
parentheses, the reader has to know the relative precedence of the &
and -> operators, in order to tell whether &tp->th_stuff is
equivalent to the above, or whether this is supposed to mean
(&tp)->th_stuff. Even though the latter is a nonsensical reading,
and the parentheses are not strictly necessary, sometimes redundant
parentheses improve readability.

Oh Ok I see.

Bill
 
T

Tom St Denis

    And oh yes your top posting.

B

Maybe he just thought your incomplete/nonsensical ramblings were your
NNTP signature since it's fairly common of you to include in NNTP
posts. It's not technically rude to top post over signatures (though
usually you'd just clip them).

Tom
 

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

No members online now.

Forum statistics

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

Latest Threads

Top