Basic help needed for pointers (double indirection)

I asked a while ago about an account that I don’t remember how to manipulate basic pointers, and someone gave me a really good demo

eg

char *ptr = "hello" (hello = a char array)

so now * ptr points to h

ptr++= moves ptr to point to the next element to get its value i do * ptr and it gives me e

ok so far I hope: D, but now I need to manipulate char **ptr, and I was wondering how I do it in such a way that deceives the effects of the 2d array?

some basic tips would be highly appreciated, since I need to perform the assignment that has **ptrto simulate a 2d array and not knowing how this happens in the first place, this means that I can’t even solve it on paper (for example, how to do it you cast ** ptr how you get the values ​​[x] [y] etc.)

thank

+3
source share
3 answers

You can index a pointer in the same way that you can index an array if all addresses are configured correctly.

Assuming the following declaration:

char **ptr;

Here are the types of different expressions:

       Expression Type Equivalent expressions (yield same value)     
       ---------- ---- ------------------------------------ -----
              ptr        char **   &ptr[0]
             *ptr        char *    ptr[0] 
         *(ptr+i)        char *    ptr[i]; &ptr[i][0]
            **ptr        char      ptr[0][0]
      *(*(ptr+i))        char      ptr[i][0]; *ptr[i]
    *(*(ptr+i)+j)        char      ptr[i][j]

:

  • ptr , (2- char)
  • ptr[i] i-
  • ptr[i][j] - j- i-
  • ptr++ ++ptr ptr,
  • (*ptr)++ ++(*ptr) *ptr,

, , malloc.

char **ptr = {"foo", "bar", "bletch"}; // using aggregate initializer on 
                                       // non-aggregate type; bad juju,
                                       // a.k.a undefined behavior

char **ptr;          // ptr has not been initialized to point anywhere 
ptr[0] = "foo";      // dereferencing ptr via subscript invokes undefined
ptr[1] = "bar";      // behavior
ptr[2] = "bletch";

, , , malloc - :

char **ptr = malloc(sizeof *ptr * N);
if (ptr)
{
  ptr[0] = "foo";    // ptr[i] gets address of
  ptr[1] = "bar";    // string literal
  ptr[2] = "bletch";
  ...
}

char **ptr = malloc(sizeof *ptr * N);
if (ptr)
{
  size_t i;
  for (i = 0; i < N; i++)
  {
    ptr[i] = malloc(sizeof *ptr[i] * M); // strictly speaking, the sizeof
    if (ptr[i])                          // is not necessary here
    {
      //initialize ptr[i]
    }
  }
}
+8

- . :

// Declare our double-indirection pointer.
char** ptr;
// And initialize it:
char s1[] = "hello";
char s2[] = "world";
ptr = malloc(sizeof(char*) * 2);
ptr[0] = s1;
ptr[1] = s2;
// ptr now points to a pointer that points to 'h'.
char* ptr2 = *ptr;
// ptr2 points to 'h'.
char* ptr3 = *(ptr + 1);
// ptr3 points to "w".
char c = **ptr; // could be written as *(*ptr)
// c = 'h'.
char c2 = *(*(ptr + 1));
// c2 = 'w'.
char c3 = *(*(ptr) + 1);
// c3 = 'e'.
+4

You can use them as a normal normal two-dimensional array. (Since it is effective that they are)

char** ptr = {"lorem", "ipsum", "dolor"};
char* s1 = ptr[0]; //Points to the beginning of "lorem"
char* s2 = ptr[1]; //Points to the beginning of "ipsum"
char c = ptr[2][4]; //Contains 'r'

This is due to the fact that:

int *a;
//...
int i = a[6];
int j = *(a + 6); //Same as previous line!

Greetings

Amit Ron -

+1
source

Source: https://habr.com/ru/post/1736959/


All Articles