Vaguely about iterating a string pointer in my lectures

This is a piece of code that I copied from my lectures.

/* 2D processing */
printf("\n");
for (i=0; i<3; i++)
    for (j=0; j<3; j++)
        printf("%d ", *(*(ar+i)+j) );

Since it aris a pointer that refers to the location of the address, *(ar+i)it actually refers to the contents of the address of the address ar+i, but I don’t understand how it will work with it *(ar+i)+j, it likes content + number.

one more thing

(1) char *ptr; ptr = "This is a string";
(2) char *ptr = "This is a string";

Why (1) can not be char *ptr; *ptr="this a string"when the separation of declaration and appointment?

Thank you in advance.

+4
source share
4 answers

ar ( int **ar;), *(a + i) *(*(a + i) + j) - .

C ++ , , "" , , , ar :

int **ar;     // (1) A pointer to a pointer
int *ar[3];   // (2) An array of pointers
int ar[3][3]; // (3) An array of arrays

ar + i , ar - , ( 2 3), ( 2) (3). *, , , .

j, - ( 3), . , recap, , ar :

ar                // (1) a pointer to a pointer,
                  // (2) a pointer to array,
                  // (3) an array of array

ar + i            // (1) and (2) address of a pointer
                  // (3) address of array

*(ar + i)         // (1) and (2) a pointer
                  // (3) an array

*(ar + i) + j     // address of an integer in any case

*(*(ar + i) + j)  // an integer in any case

, , . , *(x + i), , x , , ar int **ar;. , C ++ *(x + i) x[i] i[x].

, C.

char * s = "foo";

(char *s) = ("foo");  // Not legal C, just to show type declaration and
                      // initialization

, * s, , s.

,

char *s, t;

s char * t char.

, , , C, , .

int *(*f)(int *(*g)(int x));

f, g x .

int *(*f)(int *(*)(int));  // Same as above

f , , int int, int.

, C , : -D

+5

:

  • a[b] *(a+b).

( ), a b , - , .

*(*(ar + i) + j) *(ar[i] + j), ar[i][j].

, , ar, :

  • ar[i][j] , ar[i] , .
  • ar.
  • , ar

    • type** ar
    • type (*ar)[]
    • type ar[][]
    • type * ar[]

    type - .

    , .

, . .

.

- C.

  • : type * p;
  • : type * p = <init value>;

p = <new value>;

- .

, .

+2

1) *(*(ar+i)+j)=ar[i][j]; , ar 2D-. ar[i] ( *(ar+i)), i 0 no_of_rows-1, . ar[i][j] ( *(*(ar+i)+j) .

( 3D) .

2):

ptr="abc"; "abc", ptr.

+1

, ar .

int **ar;   

ar ar , int.
*ar ar , int.
*(*ar) , ( ar ), .. int.

(1) char * ptr; * ptr = "this string", ?

, * . , .

+1

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


All Articles