473,799 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why (type*)pointer isn't equal to *(type**)pointe r?

Why (type*)pointer isn't equal to *(type**)pointe r,

In the code snippet, it shows that:
(int *) == (int **) ,
(int *) != (*(int **)) .

Does type-casting change the address? or doesn't type-casting do
anything?

1 int main(void)
2 {
3 char ch;
4 int *p;
5 int *p2;
6 int *p3;
7
8 ch = 'c';
9 p = (int *)(&ch);
10 p2 = *(int **)(&ch);
11 p3 = (int **)(&ch);
12
13 printf("%c", *p);
14 printf("%c", *p2); /* error in de-reference */
15 printf("%c", *p3);
16
17 return 0;
18 }
19
~
~

Thank you

lovecreatesbeau ty

Jan 14 '06 #1
10 2286
Ian
lovecreatesbeau ty wrote:
Why (type*)pointer isn't equal to *(type**)pointe r,

In the code snippet, it shows that:
(int *) == (int **) ,
(int *) != (*(int **)) .

Does type-casting change the address? or doesn't type-casting do
anything?

1 int main(void)
2 {
3 char ch;
4 int *p;
5 int *p2;
6 int *p3;
7
8 ch = 'c';
9 p = (int *)(&ch);
10 p2 = *(int **)(&ch);


Your are casting to a pointer to pointer to int and dereferencing the
result, which will be 'c' and garbage.

Ian
Jan 14 '06 #2
lovecreatesbeau ty wrote:
Why (type*)pointer isn't equal to *(type**)pointe r,


Well, the main reason is that the first does not dereference the
pointer, but the second does. You can't possibly expect two
expressions to have the same value if one dereferences a pointer
and the other one does not.

- Logan
Jan 14 '06 #3
Yes, I can abstract the first byte among the 4 bytes of the int. This
kind of type-casting is normal and occurs frequently, I think.

Ian wrote:

Your are casting to a pointer to pointer to int and dereferencing the
result, which will be 'c' and garbage.

Ian


Jan 14 '06 #4
Thank you.

But I think (type**)pointer is a double pointer, *(type**)pointe r is a
single pointer after de-reference (type**)pointer and is equal to
(type*)pointer, right? I'm so confused on this.

Logan Shaw wrote:
lovecreatesbeau ty wrote:
Why (type*)pointer isn't equal to *(type**)pointe r,


Well, the main reason is that the first does not dereference the
pointer, but the second does. You can't possibly expect two
expressions to have the same value if one dereferences a pointer
and the other one does not.

- Logan


Jan 14 '06 #5
lovecreatesbeau ty said:
Why (type*)pointer isn't equal to *(type**)pointe r,

In the code snippet, it shows that:
In the code snippet, the behaviour is undefined, because you call a variadic
function without a valid prototype in scope. Fix that by adding:

#include <stdio.h>
(int *) == (int **) ,
(int *) != (*(int **)) .

Does type-casting change the address? or doesn't type-casting do
anything?
Casting is an explicit conversion from one type to another. Whether this is
meaningful depends on the situation. So let's look at the situation.
1 int main(void)
2 {
3 char ch;
4 int *p;
5 int *p2;
6 int *p3;
7
8 ch = 'c';
9 p = (int *)(&ch);
The Standard (I'm actually using a C89 draft here - caveat lector) says: "A
pointer to an object or incomplete type may be converted to a pointer to a
different object type or a different incomplete type. The resulting pointer
might not be valid if it is improperly aligned for the type pointed to. It
is guaranteed, however, that a pointer to an object of a given alignment
may be converted to a pointer to an object of the same alignment or a less
strict alignment and back again; the result shall compare equal to the
original pointer."

In this case, the pointer &ch is converted to int *, but there is no
guarantee that a char is aligned in a manner proper to int. So the pointer
is (at least potentially) invalid, and the code is therefore not portable.
10 p2 = *(int **)(&ch);
The same applies, so again this is not portable C.
11 p3 = (int **)(&ch);


And again.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jan 14 '06 #6
On 13 Jan 2006 23:15:06 -0800, in comp.lang.c , "lovecreatesbea uty"
<lo************ ***@gmail.com> wrote:
Why (type*)pointer isn't equal to *(type**)pointe r,
it is, if you do it right. In this case however, you're converting
between different types of pointer which may not work since different
pointer types may be stored differently (alignment, size, bitpattern
etc)..
1 int main(void)
don't put line numbers in code snippets, I don't plan to retype your
code to test any suggestions I may have. And you may have already had
to, so you may have introduced errors en route.
2 {
3 char ch;
4 int *p; 9 p = (int *)(&ch);
This casts a char* into an int*.
int* and char* are rather different pointer types. Converting one into
the other may not work but often will.
5 int *p2;
10 p2 = *(int **)(&ch);
This casts a char* into an int**, then dereferences it.
int** and char* are definitely different pointer types, converting one
into the other is almost certain to do strange things.
13 printf("%c", *p);
14 printf("%c", *p2); /* error in de-reference */


you won't get a compile error here provided you #include stdio.h

The result however may be garbage, or crash your application.
Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 14 '06 #7
lovecreatesbeau ty wrote:
Thank you.

But I think (type**)pointer is a double pointer, *(type**)pointe r is a
single pointer after de-reference (type**)pointer and is equal to
(type*)pointer, right? I'm so confused on this.


Ah, I think I may understand the confusion now. Typecasting does not
change what a pointer points to. Ultimately, a pointer is just an
address (at least on most machines) -- something that could be loaded
into an address register. Casting it to a different type does not
change the address. In other words, casting does not change the
value of the pointer. It just changes data structures that the
compiler uses (at compile time) what it should expect to find at
that address.

So, (type**)pointer has a type that indicates it's a pointer to a pointer.
But its value remains the same as without the typecast. The compiler
doesn't do any extra work to make sure the value is consistent with the
address. Only the "&" and "*" operators do that. (Well, and the []
operator.)

- Logan
Jan 14 '06 #8
Mark McIntyre <ma**********@s pamcop.net> writes:
On 13 Jan 2006 23:15:06 -0800, in comp.lang.c , "lovecreatesbea uty"
<lo************ ***@gmail.com> wrote:
Why (type*)pointer isn't equal to *(type**)pointe r,
it is, if you do it right. In this case however, you're converting
between different types of pointer which may not work since different
pointer types may be stored differently (alignment, size, bitpattern
etc)..


For certain values of "right". Apart from type compatibility, one
expression dereferences the pointer and the other one doesn't. They
can be equal only if "pointer" happens to point to itself.
1 int main(void)


don't put line numbers in code snippets, I don't plan to retype your
code to test any suggestions I may have. And you may have already had
to, so you may have introduced errors en route.


Actually, it looks like a copy-and-paste from a vi editor session with
line numbering enabled (":set number"); the trailing '~' characters
are a clue. But yes, line numbers in posted code snippets are a bad
idea. If you want to refer to lines by number, add a comment:

int main(void) /* line 1 */
2 {
3 char ch;
4 int *p;

9 p = (int *)(&ch);


This casts a char* into an int*.
int* and char* are rather different pointer types. Converting one into
the other may not work but often will.


It could cause alignment problems (int typically has stricter
alignment requirements than char). The problem might not show up in
this case because the compiler is likely to align ch on a word
boundary, but it's a bad idea in general unless you're *sure*
the alignment will be ok.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 14 '06 #9
On Sat, 14 Jan 2006 20:11:44 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.or g> wrote:
Mark McIntyre <ma**********@s pamcop.net> writes:
On 13 Jan 2006 23:15:06 -0800, in comp.lang.c , "lovecreatesbea uty"
<lo************ ***@gmail.com> wrote:

don't put line numbers in code snippets, I don't plan to retype your
code to test any suggestions I may have. And you may have already had
to, so you may have introduced errors en route.
Actually, it looks like a copy-and-paste from a vi editor session with
line numbering enabled (":set number");


hadn't thought of that.
the trailing '~' characters are a clue.


didn't see any in my version of the post, maybe Agent hid them or
something.
Mark McIntyre
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 14 '06 #10

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

Similar topics

4
9314
by: William Payne | last post by:
Hello! If a cast from a void pointer to a pointer to a struct fails at runtime, is NULL the result? I.E, is this code meaningful: struct my_struct_t foo* = (struct my_struct_t*)void_pointer; // void_pointer is of type void* if(foo == NULL) { printf("Error!\n"); }
204
13139
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
8
2237
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
42
5349
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
10
7989
by: Allen | last post by:
I want to package an address to byte buffer. So I need to cast it to integer value. How to cast it? const char * ptr = 0x0013e328; int value = <cast(ptr); // use reinterpret_cast?
17
4806
by: prakashraovaddina | last post by:
Hi ppl, can some one say if the following pointer initialization is legal or no. int *intPtr = 5; While debugging i still see that the pointer is given some address. However, the initialization value is not found there. But, the following string initialization,
25
3502
by: SRR | last post by:
Consider the following code: #include <stdio.h> #include <string.h> struct test{ char a; } funTest( void ); int main( void ) {
0
1179
musicman0929
by: musicman0929 | last post by:
Im creating a SQL server 05 DB with an image data type. trying to add pointer path info to the column. how do i do that? also, how do i specify a default image extension for files linked to this column?
10
3447
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
0
9550
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10495
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...
0
10269
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9085
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
7573
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
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2942
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.