Problem while Implementing C programming concepts | CTechnotips

Hello Friends,

During my teaching, I was trying to explain arrays and negative indices used with arrays in C Programming Languages.

I have come across conceptual problem, which, I am unable to understand and justify. Though my program is simple, it gives varied outputs during its execution.

Please consider simple program shown below.
 

 
Few lines are commented, and so, program works and gives answer as per my prediction.
 
Next screen shows a line de-commented, and it’s the point from where program goes out of my prediction. I don’t understand the reason.


Below screen contains more lines de-commented, which adds more difficulties to my understanding.


I appreciate all those who read my blog and find it interesting. Please knock out your brains and suggest me conceptual solution, which would help me understanding memory manipulation for this example.

2 comments:

Unknown said...

According to me here,
b[0]=10,b[-1]=20,b[-2]=30.
now we remember that array start its index with 0 only. so the value of minimum index will become 0 index for array.
so that b[-2]=30 will become b[0]. b[-1]=20 will become b[1]. b[0]=10 will become b[2].

Kaushal Patel said...

Dear Friend
Here is not Array Problem but Memory Problem is given. Here b is Pointer [not array] and
b[0] means address of pointer itself and
b[-1] means address of pointer itself is subtracted by 2 byte because pointer have 2 byte. etc
Suppose b have memory address like DS:0344 (Note : b and b[0] is equal)then
b[-1] have memory address DS:0342 (it's subtracted by 2 bytes),
b[-2] have memory address DS:0340. etc ...

Please Check Carefully and reply me the solution.
thank you dear Friend.

Post a Comment