Predict the output of this C code snippet using pointer arithmetic and notation. Go!

preview_player
Показать описание
Test your understanding of pointer arithmetic and array access using pointer notation. Analyze the code, predict the output, and explain the logic behind each print statement in the comments.

Рекомендации по теме
Комментарии
Автор

Here’s the answer: The output of the code snippet demonstrates how pointer arithmetic works in C. The first print statement outputs 30 30 because ptr points to the third element of the array. The second print statement outputs 2 because ptr - arr calculates the index of ptr relative to the start of the array. The final print statement outputs 20 30 because ptr[-1] accesses the second element of the array, and arr[ptr - arr] accesses the third element.

GoogleDevelopers
Автор

Here's a hint: Consider how pointer arithmetic works in C and how it affects array access. Understanding the relationship between pointers and array indices is key to solving this puzzle!

GoogleDevelopers
Автор

C is not type safe,
when it comes pointer arithmetic thankfully it would work only if the two data types of the pointers are actually the same(datatype here being 'int'). Take a char and an int it would complain.
Output is,
30 30
2
20 30

Also it looks like questions asked in a company called Zoho during their interview process.

Ivy_Inquisitor
Автор

ptr[-1] busca um valor antes do que ptr aponta, deixando-o sem definição provavelmente seja isso

maisquediversao
Автор

C is still just about relevant. Why use it for teaching?

AdrianColley
Автор

C pointers are fun.
30, 30
8 (assuming int size is 4bytes)
20, segmentation fault

new_beginning
visit shbcf.ru