0

Is This Statements Are Same And If Yes Then How?

Is Z = *(ptr1) * *(ptr2); Equal to Z = (*ptr1) * (*ptr2); And Z= *ptr1 * *ptr2 ;

24th Oct 2019, 5:31 AM
Mr. Nothing
Mr. Nothing - avatar
9 Réponses
+ 4
As I understand it, the use of parentheses is only necessary when we use pointer arithmetic, as in if we do `*(ptr1 + 2) * *(ptr2 + 5)` because here we are dereferencing from another address rather than the one actually be pointed to by <ptr1> or <ptr2>. (Edit) The value of <Z> are the same for the 3 assignments above. P.S. Specify language in Relevant Tags please.
24th Oct 2019, 5:54 AM
Ipang
+ 3
When a pointer is used with array like that, & (address-of operator) is not necessary, because when an array is assigned to a pointer (<ptr1>), array <x> here is decayed into an address of the first element in the array <x> itself. Read more about array decay in the response written by ~ swim ~ in the following thread: https://www.sololearn.com/Discuss/2043862/?ref=app Try this code (paste in main). int x[5] = {1,2,3,4,5}; printf("Array <x> => %x\n", x); printf("Address of array <x> => %x\n", &x); printf("Address of first element in array <x> => %x\n", &x[0]); int *ptr = x; // array <x> decays into address of // first element in it. printf("Pointer <ptr> points to address => %x\n", ptr);
24th Oct 2019, 8:29 AM
Ipang
+ 3
You're welcome Aman BTW you can edit your reply or your original post above if you want. Sometimes autocorrect can be annoying I know : )
24th Oct 2019, 1:26 PM
Ipang
+ 2
Thanks for adding language specification in tags 👍
24th Oct 2019, 9:02 AM
Ipang
+ 2
Hii Ipang thankyou very much for explain me.. 😊 my doubles are clear now.
24th Oct 2019, 11:27 AM
Mr. Nothing
Mr. Nothing - avatar
+ 2
Sorry *doubts
24th Oct 2019, 11:27 AM
Mr. Nothing
Mr. Nothing - avatar
+ 2
Next Time I'll take care of this. 😋. Thank you.
24th Oct 2019, 1:39 PM
Mr. Nothing
Mr. Nothing - avatar
+ 1
Ipang understand Int x[10]; int *ptr1 = x; is valid while x is array and size is 10 And int *ptr1 = &x; is invalid. Whyyyy????
24th Oct 2019, 6:53 AM
Mr. Nothing
Mr. Nothing - avatar
+ 1
As I know & operator indicates address of variable!!!!
24th Oct 2019, 6:54 AM
Mr. Nothing
Mr. Nothing - avatar