#Simple doubt in C

1 messages · Page 1 of 1 (latest)

scenic thunder
#

Is *p=&a an invalid statement in c??

storm parrotBOT
#

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.

serene hatch
#

the syntax is correct, the question would be about p and a types

#

as p would have to be a pointer to a pointer to a type of a

cyan cove
#

Obligatory nitpick that *p=&a isn't a statement in the first place, it's an expression. *p=&a; would be a statement ackchyually

simple thorn
#

so, then that just means the answer to his question is yes

simple thorn
#

........ can we see the program

languid hawk
#

.. or a test question on an exam

simple thorn
#

💀

scenic thunder
simple thorn
#

Oh

scenic thunder
#

Another doubt is that, *p points to the value at the address it holding. So does that mean *p=&a; the variable 'a' store the address of itself?????

simple thorn
#

So either chatgpt is very imperceptive

simple thorn
#

And you have experienced the AI equivalent of HolyBlackCat explaining to you that it isn't a statement because there's no semicolon and a statement is <expression>;

scenic thunder
#

Hey @simple thorn can u explain my second doubt if u don't mind.......

simple thorn
#

.... that means p is a pointer to pointer to whatever type a is

#

so,

scenic thunder
#

Ysss

simple thorn
#

;compile ```c
int a;
int *q;
int **p;

p = &q;
*p = &a;

slow meteorBOT
#
Compilation successful

No output.

simple thorn
#

.... q is now pointing to a

#

We caused it to point to a indirectly via p

scenic thunder
#

So here in this codee........

#

P holds the address of q. So the last statement means q stores the address of the variable 'a'

simple thorn
#

Yup

scenic thunder
#

But chatgpt said something else let me read it wait a sec

simple thorn
#

Well ChatGPT can go die in a ditch I guess

#

It points to a

#

No doubt about it

#

;compile ```c
int a;
int *q;
int **p;

p = &q;
*p = &a;

//Does q point to a? Let's find out!
*q = 69;
printf("%d\n", a);

slow meteorBOT
#
Program Output
69
scenic thunder
#

I will just screenshot it.....wait

#

Waittt

#

No, *p = &a does not mean that 'a' will store its own address. In fact, this expression is incorrect in C and will either cause a compiler error or undefined behavior. Let me explain why.


Key Concepts to Understand

  1. p is a pointer variable that stores the address of another variable.

  2. *p is used to dereference the pointer, meaning it gives you access to the value stored at the memory address that p points to.

  3. &a is the address of the variable a.


Why *p = &a is Wrong

*p expects a value of type int (because p is a pointer to an int).

But &a is an address (type int *), not an int value.

So, assigning &a to *p is type-incompatible.

simple thorn
#

So, *p = &a is basically saying "Make the pointer pointed to by p point to a.

simple thorn
#

Could be either

scenic thunder
#

Now I c.......

simple thorn
#

Oh

scenic thunder
#

So basically answer to my question is it's a legal statement....

simple thorn
#

Expression*

#

But yes

#

There's no way p is a pointer to int though.

#

It could be a pointer to pointer to int.

scenic thunder
#

Wait I didn't understand.....

#

Ok ok now I understand....

#

Thankuuuu

slow meteorBOT
#

Hello! I can compile code for you. To compile code, use the ;compile command. type ;help compile for more information.

If you are unfamiliar with Markdown, codeblocks can be created by formatting your message as the following.
```
<code>
```

scenic thunder
#

!solved

storm parrotBOT
#

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

scenic thunder
#

;compile
```
int *p,a=10;
*p=&a;
```

slow meteorBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

serene hatch
#

@scenic thunder, if you want to learn something, you start by banning yourself from chatgpt and other kind of language models, also using chatgpt as argument "it told me something else" is irrelevant in any technical discussion with actual at least semi-intelligent beings :P

#

buy a bookâ„¢

serene hatch
scenic thunder