Pointer Arithmetic (Comparing the Pointers)

preview_player
Показать описание
C Programming: Pointer Arithmetic in C Programming.
Topic discussed:
1) Comparing two pointers using relational and equality operators.
2) The example problem of pointer comparison.
3) Homework problem on comparison of pointers.

Music:
Axol x Alex Skrindo - You [NCS Release]

#CProgrammingByNeso #CProgramming #Pointers #PointerArithmetic #PointersInC
Рекомендации по теме
Комментарии
Автор

The correct answer should be option (a) "45 7 4 1 1"

Explanation:

-*(p+3) will give the value at the memory location 4 elements ahead of p, which is a[5] = 45
-*(q-3) will give the value at the memory location 3 elements behind q, which is a[2] = 7
-q-p will give the difference in the number of elements between the two pointers which is (5-1) = 4
-p<q will give the result of the comparison between the memory addresses of the two pointers, is 1 (true) since the address of p is less than the address of q
-*p<*q will give the result of the comparison between the values pointed by the two pointers, which is 1 (true) since the value pointed by p is less than the value pointed by q.
.*p and *q are the values of the pointers which are 16 and 32 respectively.

mohamedmezid
Автор

p+3=45
q-3=7
q-p=1020-1004/4=4
p<q compare addresses
*p<*q compare values
correct answer (A)

hemalathayr
Автор

for Homework assignment, option 1 is the correct option according to me.

tathagatadas
Автор

you are the only one teacher in YouTube community that help student to learn languages in deep all basics is clear

MAYANKGUDADHECI-
Автор

Thank you for all your hard work.You have explained in the bestest way and as short as possible ❤️❤️
Regarding the answer it will be A.

rahuldorai
Автор

*(p+3) = 45
*(q-3)= 7
After this
Now return initial value to variable means *p =16 and *q = 45
q-p = indexof(q) - indexof(p) =4
Adress of p < q ? Yes so return 1
Is 16 < 45 ? Yes return 1

amirghorban
Автор

opyion a is correct.
p+3=45
q-3=7
q-p=1020-1004/4=4
p<q compare addresses
*p<*q compare values
correct answer (A)

rajivkumawat
Автор

Just to make things more clear in the above lecture in first example the neso guy in the statement
Printf("%d\n", p<=q); is comparing the address value and not the actual values in the array which are 4 and 6.if you also change the values then also the output will stay the same because the comparison is in between the address values and the address value of a[3] is less than the a[5].

umairalvi
Автор

Correct Answer is opt :A
If by chance anyone getting 47 7 4 1 0 then it's totally wrong because there's no doubt changing pointing position of pointer but remember we have never stored it in form of p and q and result of last printf is true i.e output will be 1 hence correct answer is opt:A
(ALL are openly invited if I am wrong anyhow)

amanpandey
Автор

Man, i was wondering when is the second part of this playlist to be available to us? I mean stack, queue, etc. I am having data structures course in Spring 2020, and it would be super New Year gift to all you followers from you if you gradually start publishing these videos. In c++ preferably, because my course is in c++))) anyways, just an attempt to shoutout to you on behalf of your supporters army) nail this long-awaited data structures and show us the level))

olzhasdossaibek
Автор

1. *(p+3) = *(&a[1+3 ]) = &a[4] = 45
2. *(q-3) = *(&a[5-3]) = &a[2] = 7
Let, address of a[0] = 1000 So,
3. q-p = 1020 - 1004 = 16 = 4*4(bytes) = 4
4. p<q = 1004 < 1020 = 1(true)
5. *p < *q = 7 < 32 = 1(true)

Output - (A) 45 7 4 1 1

the_ocean_eyes
Автор

Option A is the correct answer of the homework problem

abhimanyunag
Автор

Haven't used my compiler nor looked at the comment section. I'm confident now. The answer is A.

vudhanthineeraja
Автор

Because many say option A is correct: It is NOT! Copy the program, save it as a C file and compile it. You will see the output will be: 45 7 4 0 1. So option D is CORRECT!

Why? Here is the answer:

**Answer:**

Initially, `*p` points to 16 and `*q` points to 32.

The following are the various `printf()` statements:

1. `*(p + 3)` = `*(&a[1 + 3])` = `*(&a[4])` = 45
2. `*(q - 3)` = `*(&a[5 - 3])` = `*(&a[2])` = 7
3. `q - p` = 1020-1004/4=4
4. `q < p` = 1020 < 1004 = 0 (`false`)
5. `*p < *q` = 16 < 32 = 1 (`true`)

zubiarfan
Автор

man this stuff is making me get through my first semester, haha. You got some great videos!!

Surya-kpqv
Автор

We are de-referencing in the first two print statements but we know that de-referencing can never change his address but it can change the value stored in that address so his address will be same as the old addresses. thus right answer is no 1

Subratajana
Автор

I know most of the people already commented the ans but still I will comment 😂 Ans:a

Karthik-orfy
Автор

assuming adresses starting from 100 ie &a[0] is 100

*(p+3)===*(116)===45
*(q-3)===*(108)===7
q-p===(120-104)/4===4
p<q===104 < 120 ===-T===1
*p < *q===16 < 32===T===1

satyamgawade
Автор

A is the correct option....it helps to know much about pointers ...tq... keep

ganavin
Автор

sir, so pointer will store the address as initialized earlier but will point to different locations as mentioned.

nikhilanand
join shbcf.ru