0

If I have this

 int (*p)[2];

and try to do

 *(*(p))=5;

it will throw segFault. it should.

But if I have

  char (*p)[2];
  *(*(p))=5; //works
  *(*(p+1))=7; //works

Can some one explain why pointer to 2d char array treated differently than pointer to 2 d int array in C

user786
  • 3,902
  • 4
  • 40
  • 72
  • 1
    Dereferencing `NULL` (if `p` is allocated globally) or using value of non-initialized non-static local variable invokes *undefined behavior*. – MikeCAT May 01 '21 at 08:27
  • 1
    Dereferencing an invalid pointer leads to *undefined behavior*. It might cause a crash, it might seem to work, or it might set your cat on fire. Just don't do it. – Some programmer dude May 01 '21 at 08:28
  • @Someprogrammerdude `Dereferencing an invalid pointer leads to undefined behavior.` does `Dereferencing` means `assigning` to ...? – user786 May 01 '21 at 08:30
  • No, it's the use of the dereference operator `*`. The variable `p` is a pointer. You never explicitly make it point anywhere which means it will eaither be a null pointer, or have an indeterminate value. Neither are valid pointers. – Some programmer dude May 01 '21 at 08:33
  • @Someprogrammerdude if I want to dynamically create array (extending size) or `pointer to 2 d array` then the only right thing is with realoc? is this correct but thats too inefficient – user786 May 01 '21 at 08:37
  • What is the actual problem you need to solve? Why do you think it can be solved with a pointer to an array of two integers? What are you going to do with this pointer? And what "efficiency" requirements do you have? What measurements and profiling have you made to determine that what you're doing is one of the top two (or possibly three) bottlenecks in your program? Please post a new question about the *actual* problem you need help with instead, as this is too much of an [XY problem](https://en.wikipedia.org/wiki/XY_problem). – Some programmer dude May 01 '21 at 08:41
  • @Someprogrammerdude I am creating a simple program in `C` that simply find the largest contagiously same gap sub-array I like to do it in single loop. so for that I either use `realoc` but I dont want to use realoc since someone told me in real work server applications we dont use realoc was that in correct? – user786 May 01 '21 at 08:46
  • If you need to reallocate memory, then you don't really have any other choice. There are ways to minimize the number of reallocations (like always allocate more than is needed at the moment), but some times there's no way to avoid it. – Some programmer dude May 01 '21 at 11:15
  • On an unrelated note, for any pointer `p` and index `i`, the expression `*(p + i) ` is *exactly* equal to `p[i] `. That means `*(*(p)) ` is the same as `p[0][0]`. The latter is easier to read and understand. – Some programmer dude May 01 '21 at 11:18

1 Answers1

2

Unfortunately, works (apparently) is one of the possible outcomes of an ill-formed program. The pointer has no memory assigned to it, dereferencing it is undefined behavior.

It can also make the compiler shoot you down, as discussed in this Q&A.

As for the works part, here you can see it doesn't, (139 return code is a segmentation fault btw).

anastaciu
  • 23,467
  • 7
  • 28
  • 53