#Need help verifying my notes

110 messages · Page 1 of 1 (latest)

drowsy escarp
#

If a pointer is declared with the type of int * the compiler assumes that the memory location held in the variable is an integer. Regardless of what is contained within that memory address as operations are performed relative to its base type.

double x = 1.23, y;
int *p;

p = (int *) &x;
y = *p;

printf("The incorrect value of x is : %f", y);

Above is a simple example. The output of y is incorrect. That's because y think it's an integer because the pointer is an integer when it isn't.

--

I'm wondering about the validity of the final line. Because

Pointer operations are performed relative to
the base type of the pointer.

is what the book says and I don't quite understand it.

lofty roostBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

valid knoll
#

That book completely has no flippin' idea.

#

*p there is itself an invalid operation, there is no need to tell what happens after that (spoiler: ||anything can happen||).

#

Pointer arithmetic is defined based on array elements, not addresses as most people wrongly put it.

lament locust
valid knoll
#

@lament locust Actually the printed value can be 1.23.

lament locust
#

nevermind I didn't see the format is %f

#

it actually probably will be 1.23 lol

valid knoll
#

It's not because of that.

#

The value can be ur mom or whatever.

lament locust
#

it will probably print 1.23

valid knoll
#

Not "probably" by any means.

lament locust
#

wait i didnt see there is a variable y

#

anyways the book is right, this code would not work

#

im not sure why did the book bother showing nonsense though i dont remember any book writing out nonsense examples when i was learning

valid knoll
#

It is only correct up to that point of "it doesn't work", the explanation is completely off.

#

And there is no pointer arithmetic on p, so "Regardless of what is contained within that memory address" is completely out of the question.

lament locust
#

here is a more interesting question

#

does this work

#
#include <stdio.h>

int main () {
        double x = 1.23, y;
        int *p;

        p = (int *) &x;
        y = *p;

        printf("The incorrect value of x is %lf", *(double *)p);

        return 0;
}
valid knoll
#

No.

vagrant umbraBOT
#
Program Output
The incorrect value of x is 1.230000
lament locust
#

it is technically false since it was correct

valid knoll
#

In C world just because it appears to work doesn't mean it's "correct".

lament locust
#

it is correct though

valid knoll
#

It is not.

#

The code isn't valid to begin with.

lament locust
#

sure it is

drowsy escarp
lament locust
#

this is the point of casting, it's just usually not done for pointless exercises like this

valid knoll
lament locust
#

lol

drowsy escarp
valid knoll
#

Indeed.

lament locust
#

if this is invalid then all void* are invalid

drowsy escarp
#

If it helps clarification the book is trying to showcase pointer conversion

valid knoll
lament locust
#

you usually cast a void pointer before you access

#

if you want your program to compile

drowsy escarp
#

I'm going to take an SS of the few relevant pages

valid knoll
lament locust
#

yes that would be wrong

valid knoll
#

The C language does not allow that, explicitly.

lament locust
#

yes

#

in this case however, we are accessing through a compatible type

#

double*

valid knoll
#

You are not.

drowsy escarp
#

The book is trying to show me that I can't do what the code sample does

valid knoll
drowsy escarp
#

Can we start over?

lament locust
#

read the code

drowsy escarp
#

All this arguing is confusing me

lament locust
#

printf("The incorrect value of x is %lf", *(double *)p);

valid knoll
lament locust
#

its a double pointer

valid knoll
#

I don't care about that line.

lament locust
#

then youre missing the point

valid knoll
#

Your program is invalid before that point.

#

*p is invalid, everything after that is meaningless.

drowsy escarp
#

The note segment is based on these two pages

lament locust
#

ok

valid knoll
#

If in doubt, you can refer to the C language standard's definition of undefined behavior.

lament locust
#

I understand what you're saying

#

I'm going to revise this for you

drowsy escarp
# drowsy escarp

I'm just trying to check if my notes make sense based on what I've read

lament locust
#

because the y = *p was not the point of what I was asking

lament locust
#

i left it because i just copy pasted what the original was

#
#include <stdio.h>

int main () {
        double x = 1.23, y;
        int *p;

        p = (int *) &x;

        printf("The incorrect value of x is %lf", *(double *)p);

        return 0;
}
vagrant umbraBOT
#
Program Output
The incorrect value of x is 1.230000
valid knoll
#

OK that is now valid.

lament locust
#

yes

#

then we are on the same page

drowsy escarp
valid knoll
#

Because it's explicitly stated it's not allowed.

lament locust
#

it leads to a kraken spawning from the underworld by your computer desk and you die

#

that's what dennis and richie willed

drowsy escarp
valid knoll
#

In C world just because it appears to work doesn't mean it's "correct".

drowsy escarp
#

oh

#

well alright

#

It compiles but leads to unexpected behaviour

valid knoll
#

There is a whole class of programs being invalid after compilation, that is undefined behavior.

#

Though rigorously speaking UB can also happen during compilation.

lament locust
#

the thing about C is the compiler just wants you to be happy

#

its a people pleaser

drowsy escarp
#

So my code leads to undefined behavior?

valid knoll
#

Yes.

lament locust
#

but it doesnt know its hurting you by being too nice

drowsy escarp
#

So is the reasoning as to why it leads to undefined behavior correct?

Above is a simple example. The output of y is incorrect. That's because y think it's an integer because the pointer is an integer when it isn't.

valid knoll
#

The reasoning is accessing double through int* is not allowed, no more, no less.

#

The book is mistakenly describing the behavior of the particular language implementation they were using.

drowsy escarp
valid knoll
#

To that end another compiler may decide to completely get rid of your code and produce an empty program (this does happen).

drowsy escarp
#

Well that's my question and it is solved

#

Thank you both 🙏🏻

lofty roostBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

lament locust
#

this will probably prove it doesnt work

#
#include <stdio.h>

int main () {
        double x = 1.23, y;
        int *p;
        double *b;
        double z;

        p = (int *) &x;
        y = *p;

        b = &y;
        z = *b;

        printf("The incorrect value of x is %lf", z);

        return 0;
}
vagrant umbraBOT
#
Program Output
The incorrect value of x is 2061584302.000000
lament locust
#

yes

valid knoll