Understanding C String Arrays and Pointer Arithmetic: Why cpp[-1][-1] == cpp[0][0]

preview_player
Показать описание
Explore the intriguing behavior of pointer arithmetic in C with string arrays. Learn why `cpp[-1][-1]` is equivalent to `cpp[0][0]`.
---

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: confusing with c string array with point and negative indexes

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding C String Arrays and Pointer Arithmetic: Why cpp[-1][-1] == cpp[0][0]

When working with C, especially with arrays of strings, you might encounter some confusing situations regarding pointer arithmetic and negative indexing. One such puzzle arises in the code provided, where we are tasked with understanding how cpp[-1][-1] equates to cpp[0][0]. Let's break this down step-by-step.

Problem Overview

The confusion stems from the nature of pointer arithmetic in C. Here’s a snippet from the code that causes the confusion:

[[See Video to Reveal this Text or Code Snippet]]

After some increments of cpp, the question had arisen about how certain expressions like cpp[-1][-1] can lead to the same result as cpp[0][0].

Dissecting the Code

How cpp Works

Initialization:

cpp is initialized to point to cp[0] which corresponds to c[3] (i.e., "FIRST").

The pointers in cp are set as cp[0] pointing to "FIRST", cp[1] to "POINT", and so on.

Pointer Increment:

After performing + + cpp twice, cpp points to cp[2], which in turn points to c[1] (i.e., "NEW").

Understanding Negative Indexing

The expression cpp[-1][-1] involves a bit of pointer magic:

cpp[-1]:

This effectively becomes *(cpp - 1), which points to cp[1] (i.e., c + 2 or "POINT").

cpp[-1][-1]:

My cpp[-1] is now pointing at c[2]. By the rules of pointer arithmetic, accessing [-1] from this pointer gets you c[1], which indeed holds the string "NEW".

Evaluation of Expressions

cpp[0][0]:

Since cpp is now pointing to cp[2] (which is the pointer to c[1], "NEW"), cpp[0][0] will yield the same result, essentially performing the same dereferencing.

Conclusion

So both cpp[-1][-1] and cpp[0][0] lead to the string "NEW". The confusion often arises from the interplay between array indexing and pointer arithmetic, especially when negative indexes are involved.

Remember:

cpp[-1] and cpp[0] can seem counterintuitive, but they both refer to positions in the array of pointers which convey the same underlying data when dereferenced properly.

Final Thoughts

Understanding these nuances in pointer manipulation can significantly enhance your grasp of C programming, especially when dealing with arrays and strings. The key takeaway here is to always visualize how pointer arithmetic shifts the focus of your pointers, and how negative indices allow you to access prior elements in some contexts. Keep experimenting with string arrays and pointers to solidify this knowledge!
Рекомендации по теме
join shbcf.ru