-3

I'm having a fascination in C language right now and I'm trying to crack the pointers mistery. I have expirience in Python, Ruby, but this things feel strange to me. I've stumbled upon this article:

https://www.geeksforgeeks.org/applications-of-pointers-in-c-cpp/

I do understand the basic concept of it but when it comes to real life implementation I'm a bit puzzled. I do understand dynamic memory allocation with the usage of a pointer, also passing arguments by reference from the link (swap example), but when it comes to accessing arrays I have a question - What is the point of a pointer here actually if arr[2] and *(arr + 2) are the same thing ? Is there a performance benefit ? or is it only a different way to write it ?

  • `arr[2]` is just an easier way to write `*(arr + 2)`. Both use pointers. In both cases, assuming that `arr` is an array, `arr` will [decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) to a pointer to its first element. In other words, writing `arr` is equivalent to writing `&arr[0]`. – Andreas Wenzel Aug 27 '22 at 19:55
  • In your most recent edit, you successfully addressed the stated reason for the closure of the question. However, despite this, in my opinion, your question should remain closed, because in its current state, the question is too broad and therefore should be closed as ["Needs more focus"](https://stackoverflow.com/help/closed-questions). Asking for "examples of pointer usage" is not a very specific question. Also, you should only ask one question, but you are asking several questions in the same question. – Andreas Wenzel Aug 27 '22 at 20:21
  • 1
    `"Is there a performance benefit ?"` No, there generally is no performance benefit between `arr[2]` and `*(arr + 2)`. The first one is just easier to read and write than the second one. – Andreas Wenzel Aug 27 '22 at 23:14
  • 1
    Asking several questions in one question is ok if the other questions don't really ask a new question, but rather clarify the scope of the first question. For example, your questions `"What is the point of a pointer here actually if arr[2] and *(arr + 2) are the same thing ? Is there a performance benefit ? or is it only a different way to write it ?"` are ok, even though they are formulated as three questions, because you are effectively only asking a single question. However, asking for "other example of pointer usage" makes the question too broad, in my opinion. – Andreas Wenzel Aug 27 '22 at 23:28
  • Asking for examples of something is, in itself, not a specific question, but rather an open-ended question, and therefore does not fit well with Stack Overflow's Q&A format. It is more suited for a discussion forum. Additionally, pointer usage in C is a very broad topic, so asking for examples for this is a very broad question. If you want a different kind of example of pointers, then I suggest that you take a look at the function [`qsort`](https://en.cppreference.com/w/c/algorithm/qsort) in the C standard library, which uses function pointers. – Andreas Wenzel Aug 27 '22 at 23:59
  • 1
    This question is probably a duplicate of the following question: [Practical difference between using array and pointer offset notation](https://stackoverflow.com/q/58160944/12149471) – Andreas Wenzel Aug 28 '22 at 08:12

2 Answers2

3

What is the point of a pointer here

In C, when a array name is used in an expression and is not the operand of the & or sizeof operator, then the array name is said to "decay" to a pointer to the first element of the array.

In simple terms this just means the array name will be implicitly converted into a pointer to the first element of the array. "decay" just refers to the loss of type and dimension information.

For example:

int x[2] = {2, 4};
int *y = x; 

In this example we declare and initialize the pointer y. When we reference the array name x it "decays" to a pointer to the first element. It also loses type information (no longer int [2]) and dimension information (2). In this case y points to the first element of x.

We can also reference the array through x[i] which is identical to *(x+i). Notice how the name of the array is being referenced in the 2nd expression, we can add i to x and go to the x[i]th element in the array.

programmer
  • 669
  • 3
  • 11
-2

I have not done C programming in a while but when i used i found this excelled Linked List problems from Standford’s free library that helped build my understanding - http://cslibrary.stanford.edu/105/LinkedListProblems.pdf

Linked lists use pointers and they are an excellent way to build your understanding of pointers. . Basically when you use an array the size needs to be declared upfront. This may not always be possible and that’s where pointers come in handy.

Manoj Guglani
  • 134
  • 1
  • 11