473,898 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer questions

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

Oct 17 '08 #1
8 1871
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.8) 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.
Oct 17 '08 #2
On 17 Okt., 15:01, James Kuyper <jameskuy...@ve rizon.netwrote:
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.8) 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(convers ion) 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.
Oct 17 '08 #3
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(convers ion) 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.
Oct 17 '08 #4
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?
Oct 17 '08 #5
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

Oct 17 '08 #6
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.
Oct 17 '08 #7
On Fri, 17 Oct 2008 04:34:48 -0700 (PDT), tfelb <to*******@gmai l.com>
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
Oct 18 '08 #8
tfelb wrote:
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?
No. Assuming that 'src' is an object of pointer type, what is assigned
to 'dst', is not the address of 'src', it is the _value_ stored in
'src'. 'src' is a pointer, and the value stored in 'src' is an address
of something 'src' points to (or possibly a null-pointer value). It is
not the address of 'src' itself.
But I can assign an address with the address operator & too? char *dst
= &src.
Yes. But in this case you get the address of 'src' itself.
What's the difference between *dst = (char *)src and *dst = &src
See above.
and what's the recommended style?
It is not a matter of style, since the two are not even remotely the same.

Exception to the above: In one particular case when 'src' is an array,
the apparent behavior might be identical in both assignments, due to
certain specific properties of arrays in C. Since you didn't specify
what 'src' is, it is impossible to say whether this is relevant to your
question, the way you intended it.
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 same? I don't see anything even remotely the same in this case. In
your previous question you used the '&' operator. There's no '&' in this
question, which makes is a completely different issue.
I would think in both cases I'll return a pointer(address )
You you do return the value of 'ptr'. In the first case you don't cast
it. In the second case you do. Without context it is impossibel to say
whether you need the cast or not and what's the effect of the case.
Clarify your question. Otherwise, it makes no sense.

--
Best regards,
Andrey Tarasevich
Oct 18 '08 #9

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

Similar topics

11
3052
by: Vivi Orunitia | last post by:
Hi all, I tried looking this up in the sgi docs but it didn't provide any concrete answer to what I'm looking for. Basically, is there any difference between using ::iterator for a container vs using ::pointer? I did a quick experiment and replaced all the ::iterator in my code with ::pointer and it seems to work the same. What I'm think is on the surface they're the same but perhaps there're some subtle differences beneath the...
5
1941
by: DamonChong | last post by:
Hi, I am still struggling to master C++ and is trying to understand how to achieve passing arguments using pointers. I got some questions that I like to post to the experts here, hope you can help to clarify my doubts. I'm using g++ version 3.3.4. I created 3 classes as below for testing some concepts. The questions are written as comments in Bclass.h file. Thank you for your time and tips! ------------runtime errors------------
3
2367
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
3
7670
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
9
6598
by: Ben Midgley | last post by:
Please have a look at the code below My problem is the error message "Nonportable pointer conversion", so far as I can tell there is no pointer conversion occuring here so I am assuming it is something implicit. The error is associated with the assignment { &MenuItemList }, when delcaring the mit_ZoneSelectMenu structure. I am using borland bcc32 the command line compiler which seems pretty good, I just dont understand this...
4
3642
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
4
2827
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
14
18226
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun to work in C# and am asking for suggestions/comments of how to implement a similar data sharing technique in C#. A C++ smart pointer can be used to share common information. For example, assume information managed by objects I1, I2, I3,...
1
7343
by: ladesidude | last post by:
Hi, I have this program in C, and have commented each line as per my understanding. At the end, I have some questions which I havent been able to understand. Any help would be appreciated. #include <stdio.h> #define Size1 (4) #define Size2 (3)
0
9993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11256
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10946
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10481
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9658
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8034
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5877
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4295
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3303
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.