0

For the below program using, single pointer,

#include <stdio.h>

int main()
{
    int b[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int *p = b;
    printf("\nElement - %d", *(p+3));
    printf("\nElement - %d", *(p+4));
}

2D array b is being accessed successfully using single pointer p.

I have also been through this answer.

Question:

1)

Did not feel the necessity of pointer to pointer for b. Is p suppose to be pointer to pointer, when working with 2Darray b?

2)

When do we require p to be pointer to pointer, when working with b?

3)

Is n level pointer required for n dimensional array?

Community
  • 1
  • 1
overexchange
  • 15,768
  • 30
  • 152
  • 347
  • 1
    If you need additional clarification beyond the duplicate question, your question needs to be more specific than the original. – Robert Harvey Oct 18 '16 at 16:18
  • @RobertHarvey I don't think this is a duplicate question because the code in this question doesn't even have pointers to pointers. `p` has the type `int*` which is a normal integer pointer. – Derek 朕會功夫 Oct 18 '16 at 16:34
  • I suggest using the phrase "pointer to pointer". The term "double pointer" refers to type `double*`. – Keith Thompson Oct 18 '16 at 19:30
  • @RobertHarvey Can you please reopen the query? Let me know, if something is wrong with the question, after edit – overexchange Oct 19 '16 at 02:24
  • @KeithThompson Update the query. Am stuck with this query, please help me – overexchange Oct 19 '16 at 02:52
  • `int *p = b;` is a constraint violation. Check your compiler error/warning messages. The behaviour of the program is undefined and meaningless. – M.M Oct 19 '16 at 03:08
  • @M.M Correct, compiler gives warning - `temp.c:4:12: warning:initialization from incompatible pointer type [-Wincompatible-pointer-types] int *p =b;`, but I need to understand, why compiler is bothering about 2D array `b` pointed by `p`? Because things are working with `*p`. – overexchange Oct 19 '16 at 03:12
  • Things are not working. See [What is undefined behaviour](http://stackoverflow.com/a/4105123/1505939). C has a type system. – M.M Oct 19 '16 at 03:14
  • @M.M When I say, things are working, `*p` stood the purpose of accessing elements in 2 array. Why do I need `**p`? From type system aspect, why compiler asks for `**p`? – overexchange Oct 19 '16 at 03:20
  • You don't need **p and the compiler does not ask for **p. The type of `b` (after decay) is `int (*)[3]` which is not compatible with `int *`. – M.M Oct 19 '16 at 03:21
  • 1
    Read section 6 of the [comp.lang.c FAQ](https://www.c-faq.com). – Keith Thompson Oct 19 '16 at 03:24
  • @M.M OK, if I dont need `**p`, then my query(above) further asks the necessity of `**p` to access 2D array? Keeping compiler type system warnings aside – overexchange Oct 19 '16 at 03:29
  • There is no necessity of **p. You need to do some basic reading about arrays (e.g. the link supplied by Keith) – M.M Oct 19 '16 at 03:29
  • @M.M I did not get the answer for above query after reading section 6 [here](http://c-faq.com/aryptr/index.html). – overexchange Oct 19 '16 at 04:44
  • 1
    You asked 3 questions. The answer to the first and third is "No". The second is explained by the duplicate link. – M.M Oct 19 '16 at 05:21
  • @M.M Answer to second is *Use ** when you want to preserve (OR retain change in) the Memory-Allocation or Assignment even outside of a function call.* Is that correct? – overexchange Oct 19 '16 at 05:25
  • 1
    That text relates to when you want a function to allocate or resize a 1-D array (and you don't want to use the function return value to return a pointer to the array) – M.M Oct 19 '16 at 05:34

0 Answers0