Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer questions

Reply
Thread Tools

pointer questions

 
 
tfelb
Guest
Posts: n/a
 
      10-17-2008
Hey group!

I have 2 questions. I saw functions with char *dst = (char *)src. In
that case if I remember what I've learned I assign (an) (the) address
of src to dst. Right?

But I can assign an address with the address operator & too? char *dst
= &src.

What's the difference between *dst = (char *)src and *dst = &src and
what's the recommended style?

2. The same thing with returning a pointer from a function. If i write
a version of strchr i can return a pointer like

return ptr OR
return (char *)ptr.

I would think in both cases I'll return a pointer(address)

Thanks for any help

Tom



 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      10-17-2008
tfelb wrote:
> Hey group!
>
> I have 2 questions. I saw functions with char *dst = (char *)src. In
> that case if I remember what I've learned I assign (an) (the) address
> of src to dst. Right?
>
> But I can assign an address with the address operator & too? char *dst
> = &src.
>
> What's the difference between *dst = (char *)src and *dst = &src and
> what's the recommended style?


It's not a minor style difference, it's a profound difference in
meaning. However, there's also some confusion in what you've written
between declarations and statements. Let me clear up that issue before
going any further. You wrote two very different things that look similar:

char *dst = (char*)src;

This is a declaration of a pointer to char named 'dst'. It is
initialized by converting src into a pointer to char. That pointer value
is stored in dst.

You also mentioned:

*dst = (char*)src;

This is an assignment statement. It takes a pointer named dst,
determines what that pointer points at, and then sets the thing that it
points at equal to (char*)src.

So, the declaration sets the value of 'dst'. The assignment statement
sets the value of the thing that dst points at. These are two very
different things.

(char*)src and &src mean two very different things. In this context, src
is probably a pointer object, whose value is a pointer that points at
some other object (if it's a void*, it might point at itself, but this
is easier to explain if we assume that it doesn't). (char*)src converts
that pointer value into a pointer to char, which will point to the first
byte of whatever object src points at. In contrast, &src creates a
pointer value, which points at src itself, not at the object that src
points at. You're earlier declaration implies that src is a pointer
type, let's call it a T*. Then &src has the type T**. That's not a type
that can be used to initialize the value of dst.


> 2. The same thing with returning a pointer from a function. If i write
> a version of strchr i can return a pointer like
>
> return ptr OR
> return (char *)ptr.


The key thing you need to understand about casts is that they are almost
never necessary. Most safe conversions occur implicitly, without you
having to make them explicit by using a cast. Using a cast can hide a
type error, so they should be avoided except when they are actually
necessary.

For most operators, when the two operands need to be the same type, the
"usual arithmetic conversions" (6.3.1. happen automatically, to make
them have the same type. When you assign a value to an object, if the
value is not of the same type as the object, it is usually implicitly
converted to the type of the object. The same is true in the definition
of an object, if it is explicitly initialized. It also happens to when
arguments are passed to a function, if that function is declared with a
prototype. Getting back to your question, the implicit conversions also
occur when a function returns a value.

You need a cast if there is no implicit conversion allowed from the
source type to the destination type. You also need a cast if the
implicit conversion is allowed, but would produce a different result
than the conversion that you want to perform. However, these are the
only two cases where a cast is needed, and you should always be
suspicious of the possibility that any cast you read, or are thinking of
writing, might be either unnecessary, or possibly even an error.
 
Reply With Quote
 
 
 
 
tfelb
Guest
Posts: n/a
 
      10-17-2008
On 17 Okt., 15:01, James Kuyper <(E-Mail Removed)> wrote:
> tfelb wrote:
> > Hey group!

>
> > I have 2 questions. I saw *functions with char *dst = (char *)src. In
> > that case if I remember what I've learned I assign (an) (the) address
> > of src to dst. Right?

>
> > But I can assign an address with the address operator & too? char *dst
> > = &src.

>
> > What's the difference between *dst = (char *)src and *dst = &src and
> > what's the recommended style?

>
> It's not a minor style difference, it's a profound difference in
> meaning. However, there's also some confusion in what you've written
> between declarations and statements. Let me clear up that issue before
> going any further. You wrote two very different things that look similar:
>
> * * * * char *dst = (char*)src;
>
> This is a declaration of a pointer to char named 'dst'. It is
> initialized by converting src into a pointer to char. That pointer value
> is stored in dst.
>
> You also mentioned:
>
> * * * * *dst = (char*)src;
>
> This is an assignment statement. It takes a pointer named dst,
> determines what that pointer points at, and then sets the thing that it
> points at equal to (char*)src.
>
> So, the declaration sets the value of 'dst'. The assignment statement
> sets the value of the thing that dst points at. These are two very
> different things.
>
> (char*)src and &src mean two very different things. In this context, src
> is probably a pointer object, whose value is a pointer that points at
> some other object (if it's a void*, it might point at itself, but this
> is easier to explain if we assume that it doesn't). (char*)src converts
> that pointer value into a pointer to char, which will point to the first
> byte of whatever object src points at. In contrast, &src creates a
> pointer value, which points at src itself, not at the object that src
> points at. You're earlier declaration implies that src is a pointer
> type, *let's call it a T*. Then &src has the type T**. That's not a type
> that can be used to initialize the value of dst.
>
> > 2. The same thing with returning a pointer from a function. If i write
> > a version of strchr i can return a pointer like

>
> > return ptr OR
> > return (char *)ptr.

>
> The key thing you need to understand about casts is that they are almost
> never necessary. Most safe conversions occur implicitly, without you
> having to make them explicit by using a cast. Using a cast can hide a
> type error, so they should be avoided except when they are actually
> necessary.
>
> For most operators, when the two operands need to be the same type, the
> "usual arithmetic conversions" (6.3.1. happen automatically, to make
> them have the same type. When you assign a value to an object, if the
> value is not of the same type as the object, it is usually implicitly
> converted to the type of the object. The same is true in the definition
> of an object, if it is explicitly initialized. It also happens to when
> arguments are passed to a function, if that function is declared with a
> prototype. Getting *back to your question, the implicit conversions also
> occur when a function returns a value.
>
> You need a cast if there is no implicit conversion allowed from the
> source type to the destination type. You also need a cast if the
> implicit conversion is allowed, but would produce a different result
> than the conversion that you want to perform. However, these are the
> only two cases where a cast is needed, and you should always be
> suspicious of the possibility that any cast you read, or are thinking of
> writing, might be either unnecessary, or possibly even an error.


Thanks for the replies, I think I understand you. If i would say that
in easy words (I'm not an expert )*dst = (char *)src is simply a
casting(conversion) if 2 pointer variables have different types (for
example the conversion from a void * pointer to a char *).

*dst = &src is the intializing process of dst but only if src has the
type char *. If not then I'll need the conversion.

Tom F.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-17-2008
tfelb wrote:
....
> Thanks for the replies, I think I understand you. If i would say that
> in easy words (I'm not an expert )*dst = (char *)src is simply a
> casting(conversion) if 2 pointer variables have different types (for
> example the conversion from a void * pointer to a char *).


You need to understand the difference between conversion and casting. In
this context, conversion is the creation of a value of one type from a
value that is of a different type. Casting is the process of explicitly
ordering that a conversion must occur, in this case by using the cast
(char*). Conversion can also occur without a cast; in fact, you're
usually better off letting it happen implicitly, if it can happen
implicitly. If, for instance, src is a void* pointer, then the following
definition:

char *dst = src;

will convert src implicitly to char*, no cast is needed.

You're incorrect in saying that 2 pointer variable are converted by the
statement you refer to. Only the value of one variable is converted by
that statement - the value of src.

> *dst = &src is the intializing process of dst but only if src has the
> type char *. If not then I'll need the conversion.


No. What you've written is an assignment statement, which has no effect
whatsoever on the value of src. It only affects the value of the object
that src points at. In this context, "*src" means "the object that src
points at". To create a definition with an initializer for dst, it needs
to start out as a declaration for dst:

char *dst = &src;

In the definition of dst, "*dst" does NOT mean "the object that dst
points at". Rather, the * is part of "char *", which defines the type of
dst. Also, the "=" that appears in this definition is part of the
initialization syntax; it doesn't have the same meaning that it would
have in an assignment statement. A definition with an initializer is
just a shorthand for a declaration followed by an assignment statement:

char *dst;
dst = &src;

Notice the absence of a '*' in the assignment statement. Please also
note that neither the initializer nor the assignment statement would be
valid unless src has the type 'char'. If src had type char*, as you
suggest, then &src would have type char**, which it not permitted as an
initializer for dst.
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      10-17-2008
tfelb wrote:
> Hey group!
>
> I have 2 questions. I saw functions with char *dst = (char *)src. In
> that case if I remember what I've learned I assign (an) (the) address
> of src to dst. Right?
>
> But I can assign an address with the address operator & too? char *dst
> = &src.
>
> What's the difference between *dst = (char *)src and *dst = &src and
> what's the recommended style?
>
> 2. The same thing with returning a pointer from a function. If i write
> a version of strchr i can return a pointer like
>
> return ptr OR
> return (char *)ptr.
>
> I would think in both cases I'll return a pointer(address)
>
> Thanks for any help
>
> Tom
>
>
>


What is the declaration of dst and src?
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-17-2008
James Kuyper wrote:

> char *dst = &src;
>
> In the definition of dst, "*dst" does NOT mean "the object that dst
> points at". Rather, the * is part of "char *", which defines the type of
> dst. Also, the "=" that appears in this definition is part of the
> initialization syntax; it doesn't have the same meaning that it would
> have in an assignment statement. A definition with an initializer is
> just a shorthand for a declaration followed by an assignment statement:
>
> char *dst;
> dst = &src;


Nitpick or not ?-- there are two ways I can think of that this isn't true.

(a) consider `const int spoo = 17;`, which isn't usefully replaced by
`const int spoo; spoo = 17;` since the assignment violates a
constraint.

(b) consider `int flarn = 42;` outside a function, where the assignment
statement `flarn = 42;` isn't allowed even though assignments to
`flarn` are permitted.

--
'Don't be afraid: /Electra City/
there will be minimal destruction.' - Panic Room

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-17-2008
Chris Dollin wrote:
> James Kuyper wrote:

....
>> have in an assignment statement. A definition with an initializer is
>> just a shorthand for a declaration followed by an assignment statement:
>>
>> char *dst;
>> dst = &src;

>
> Nitpick or not ?-- there are two ways I can think of that this isn't true.
>
> (a) consider `const int spoo = 17;`, which isn't usefully replaced by
> `const int spoo; spoo = 17;` since the assignment violates a
> constraint.
>
> (b) consider `int flarn = 42;` outside a function, where the assignment
> statement `flarn = 42;` isn't allowed even though assignments to
> `flarn` are permitted.


That's perfectly true; and I thought about discussing those
complications, but decided that he wasn't ready for them yet.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-17-2008
Eric Sosman <(E-Mail Removed)> writes:
> tfelb wrote:
>> Hey group!
>> I have 2 questions. I saw functions with char *dst = (char *)src. In
>> that case if I remember what I've learned I assign (an) (the) address
>> of src to dst. Right?


[...]

> Here's an important thing to keep in mind: A C pointer
> is not merely an address, it's an address *and* a type.
> If I hand you a naked address that might be represented on
> some machine by the value 0x12345678 and ask you what value
> is stored at that memory location, you do not have enough
> information to answer me. Am I asking about the char at
> that location, or about the int, or the double, or the
> struct tm, or the what? You need to know what kind of a
> value I'm asking about in order to know how many bytes to
> examine and what significance to attach to them. In C, a
> pointer's declared type carries this additional information;
> the address alone is not enough.


Correct -- *if* you're using the term "address" to mean a raw machine
address. But in fact the C standard uses the term "address" to mean,
basically, a pointer value. For example, given the declaration
"int n;", the expression "&n" yields *the address of* n; that
"address" includes both the location in the machine's memory and the
fact that it's the address of an object of type int. (The latter
information is part of the expression's type; it needn't actually be
stored.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-17-2008
On Fri, 17 Oct 2008 04:34:48 -0700 (PDT), tfelb <(E-Mail Removed)>
wrote:

>Hey group!
>
>I have 2 questions. I saw functions with char *dst = (char *)src. In
>that case if I remember what I've learned I assign (an) (the) address
>of src to dst. Right?


Only in a special case. It would always be true if the code was
char *dst = (char *)&src;
Note the added '&'.

For this code:

If src is an array, before the cast operator is applied the
expression src is converted to the address of the first element of the
array with type pointer to element type. Then your statement is true.

If src is a scalar object, the value contained in the object
(variable) src is converted to a char* value. (If src is an object
pointer the conversion is well defined. If it is an integer object,
it is implementation defined. I don't think it is defined for other
types such as function pointer or non-array aggregates.) This
converted value is stored in the char* named dst.

>
>But I can assign an address with the address operator & too? char *dst
>= &src.


Only if src is an object of type char.

>
>What's the difference between *dst = (char *)src and *dst = &src and
>what's the recommended style?


First, what you have written is different here. Your previous code
contained object definitions. This code contains executable
statements. I am going to assume you intended to continue in the same
vein and there is an invisible "char" in front of both expressions.

When src is a scalar object, the first expression works on the value
contained in src while the second works on the address of src itself.

If src is not an object of type char, the second is a constraint
violation requiring a diagnostic. While any address can be converted
to a char*, only the address of a char can be done so without a cast.

>
>2. The same thing with returning a pointer from a function. If i write
>a version of strchr i can return a pointer like
>
>return ptr OR
>return (char *)ptr.
>
>I would think in both cases I'll return a pointer(address)


There is no type pointer. There are types pointer to char, pointer to
int, etc. If your function is defined as returning a char*, then the
value of ptr is returned AS IF BY ASSIGNMENT. This means the
restrictions mentioned above apply. If ptr has type char*, the two
return statements are obviously equivalent. If ptr is a different
type of pointer, then only the second is legal.

--
Remove del for email
 
Reply With Quote
 
tfelb
Guest
Posts: n/a
 
      10-17-2008
On 17 Okt., 16:52, Keith Thompson <(E-Mail Removed)> wrote:
> Eric Sosman <(E-Mail Removed)> writes:
> > tfelb wrote:
> >> Hey group!
> >> I have 2 questions. I saw *functions with char *dst = (char *)src. In
> >> that case if I remember what I've learned I assign (an) (the) address
> >> of src to dst. Right?

>
> [...]
>
> > * * Here's an important thing to keep in mind: A C pointer
> > is not merely an address, it's an address *and* a type.
> > If I hand you a naked address that might be represented on
> > some machine by the value 0x12345678 and ask you what value
> > is stored at that memory location, you do not have enough
> > information to answer me. *Am I asking about the char at
> > that location, or about the int, or the double, or the
> > struct tm, or the what? *You need to know what kind of a
> > value I'm asking about in order to know how many bytes to
> > examine and what significance to attach to them. *In C, a
> > pointer's declared type carries this additional information;
> > the address alone is not enough.

>
> Correct -- *if* you're using the term "address" to mean a raw machine
> address. *But in fact the C standard uses the term "address" to mean,
> basically, a pointer value. *For example, given the declaration
> "int n;", the expression "&n" yields *the address of* n; that
> "address" includes both the location in the machine's memory and the
> fact that it's the address of an object of type int. *(The latter
> information is part of the expression's type; it needn't actually be
> stored.)
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) *<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"


Ok. I'll think about these areas. Thank you all!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments