C_78 Problems on Pointers | Important question with answer

preview_player
Показать описание
In this lecture we will discuss some coding exercises on pointers.

*********************************************
Connect & Contact Me:

Telegram Group Link: Jenny's Lectures
*******************************************

More Playlists:

Tags:
pointers in c, what is pointer, introduction to pointers in c

#coding #strings #jennyslectures #cprogramming #clanguage
Рекомендации по теме
Комментарии
Автор

In the statement printf("%d %d %d\n", (*p)++, *p++, *++p);, it might seem that the operations should be evaluated from right to left due to operator precedence or associativity, but that's not how function argument evaluation works in C.

Understanding the Concept:
Operator Associativity vs. Function Argument Evaluation:

Operator Associativity refers to how operators of the same precedence are grouped in the absence of parentheses (e.g., a + b + c is left-to-right because + is left-associative).

However, function arguments in C are not evaluated based on the associativity of the operators within the arguments. Instead, the order of evaluation of function arguments is unspecified in the C standard.

Unspecified Evaluation Order in C: The C standard does not define the exact order in which function arguments are evaluated (i.e., whether arguments are evaluated left-to-right, right-to-left, or in any particular order). This means that depending on the compiler, the order can vary. Most modern compilers tend to evaluate arguments from right to left, but it’s not guaranteed, and the standard doesn’t mandate it.

Side Effects with Increment Operators: In this case, the post-increment and pre-increment operators (++) introduce side effects, which change the value of p (the pointer) during argument evaluation. This makes it tricky to predict the exact output without knowing how the compiler evaluates the function arguments.

Example Breakdown:
Let’s analyze what happens in the statement printf("%d %d %d\n", (*p)++, *p++, *++p);.

(*p)++:
This returns the value that p points to (dereferenced), then increments the value at p.
*p++:
This returns the current value at p, then increments the pointer p to point to the next location.
*++p:
This first increments the pointer p, then returns the value at the new location.
Now, based on how the compiler decides to evaluate the arguments, the exact order of pointer increments and dereferencing will differ.

Why Associativity Doesn’t Apply:
Operator associativity applies when you have multiple operators of the same precedence in a single expression (like a + b + c).
Function argument evaluation is not governed by associativity rules but by the fact that the C standard leaves the order of evaluation unspecified. Therefore, associativity doesn't determine how arguments like (*p)++, *p++, and *++p are evaluated.

Conclusion:
The evaluation order of the arguments in printf is unspecified, meaning the compiler can choose the order in which these arguments are processed. This is why associativity (whether right-to-left or left-to-right) does not govern the behaviour here. The key takeaway is to avoid relying on the evaluation order when using side-effect-producing expressions like ++ in function arguments, as the result may vary across compilers.

jaijaijaijai
Автор

wow. Studying become more interesting after such problems....especially for beginners

DharmeshT-jq
Автор

I think that pointer topic is so tough...but when I saw your lecture it so simple ..for me, thank you mam...really that you given problem are make practice

visvasprabhakar
Автор

Jenny the Robot. Happy Independence Day Mam.

shivendra
Автор

thanks mam for helping us specially in present era..it is a very famous maxim that people help u, u r lucky if people don't help u its very common.i have seen 1st 20 video in a day ..all are quite interesting and understandable. I have learnt more.thsnks

sohaibirshad
Автор

Your way of teaching is unique. Love and respect from Pakistan❤

hammadhamyuain
Автор

Thank you mam for a wonderful explanation . Mam I have one that is we are going to get the output of star p as zero in that case why the value got changed in place of -1 to 0 . In the first code problem.

YtMindMatrix
Автор

how to understand the assossiactivity at 2:48 that it is right to left ??

adityanair
Автор

Thanks very much for the well-detailed video. It clear my conceptions on pointer.

oyebamijimustapha
Автор

1. p = 12, 11, 11
2. p = 13
3. p = 13
4. p = 14
5. q = 67
6. p = 54
7. p = -2 Though I got -2 as my answer to this, when I checked in on my computer it was giving multiple outputs, so each time I run the code, it varies. Why??

chinweokwugiftchiedozie
Автор

Your way of explanation is always awesome, very enjoyable, interesting and easy to absorb. Thank you so much!!! Keep up the good work☺

manirulmolla
Автор

8:56 why are we geeting -3 for the last one when i dry i get -2

kashishrocks
Автор

Ma'am it's a request please make a course on Java also.
U teach amazingly
Thank you ma'am

shivammishra
Автор

At 11:00, In printf("%d\n", *(p++-2)-1) , my calculation is coming -2, but the output is showing -3. Plz explain it.

SuprovatBhattacharjee
Автор

🌟🌟Mam, when will you upload Object Oriented Programming (OOPs)videos mam, waiting alot to see 🌟🌟

jegadeeswarapandian
Автор

Nice t-shirt, nice hair, introducing you self is professional,
Thinking imagination,
Respected,

SravanKumar-ucqt
Автор

4:38 mam but *p is in bracket so first it should have been solve plz reply mam

ajjubhai
Автор

Thank you ma'am for such wonderful explanation....

Vishal-zwhn
Автор

i got (-3) on the last one any explainations why i got that ? i tried the code on the white board not the one on the computer.

Userx-zm
Автор

ma'am but precedence of post increment /post decrement is greater than pre increment/decrement but you said ++p would be solved first(when second time printf function is used) plz ma'am try to solve my doubt🙏🙏🙏🙏
If someone else is having answer for this then help me out

elijahmikaelson