#Simple doubt in C
1 messages · Page 1 of 1 (latest)
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.
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
Obligatory nitpick that *p=&a isn't a statement in the first place, it's an expression. *p=&a; would be a statement 
BOOOOOOOOM!!!!
so, then that just means the answer to his question is yes
.... im assuming your reason for asking this question is that you have a program that uses it and the program doesnt compile or doesnt work.
........ can we see the program
.. or a test question on an exam
💀
Well, I didn't write any program, it's just a doubt rised in my mind while studying. Also when I asked chatgpt about this , it said it is invalid..... .....
Oh
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?????
So either chatgpt is very imperceptive
Or it's very perceptive
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>;
Hey @simple thorn can u explain my second doubt if u don't mind.......
So basically, *p has to be a pointer to whatever type a is since we're assigning to it &a
.... that means p is a pointer to pointer to whatever type a is
so,
Ysss
;compile ```c
int a;
int *q;
int **p;
p = &q;
*p = &a;
No output.
So here in this codee........
P holds the address of q. So the last statement means q stores the address of the variable 'a'
Yup
But chatgpt said something else let me read it wait a sec
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);
69
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
-
p is a pointer variable that stores the address of another variable.
-
*p is used to dereference the pointer, meaning it gives you access to the value stored at the memory address that p points to.
-
&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.
So, *p = &a is basically saying "Make the pointer pointed to by p point to a.
So, this means that either you told chatgpt that p is a pointer to int, or chatgpt pulled that assumption out of nowhere and is wrong.
Could be either
Yss I told p is pointer int type...
Now I c.......
Oh
So basically answer to my question is it's a legal statement....
Expression*
But yes
There's no way p is a pointer to int though.
It could be a pointer to pointer to int.
!solved
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
;compile
```
int *p,a=10;
*p=&a;
```
@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â„¢
PS that block contains backticks ` preceeded by backslashes, that won't work, as backslash is to make them visible instead of using their assigned meaning, so for code block those just have to be three backticks
Yeahhh I was tryna using the bot😅 failed....