Two-dimensional arrays with pointers

In the section below, someone can explain the results obtained during execution:

#include<stdio.h>
void main()
{
char a[5][10]={"one","two","three","four","five"};
char **str=a;
printf("%p ", &a[0]);
printf("\n%p ", &str[0]);
printf("\n%p ", &str[3]);
printf("\n%p ", &str[1][56]);
printf("\n%p ", &(*(*(str+4)+1)));
}

Below is the result:

0xbf7f6286 
0xbf7f6286 
0xbf7f6292 
0x38 
0x1 
  • & a [0] - address of the starting address of the array
  • & str [0] is the same

    Can someone explain how the address of str [3] is 0xbf7f6292. According to my understanding, & str [0] and & str [3] should differ by 30 bytes.

    Also, please someone explain how the conclusion is for the other two cases.

Thanks at Advance

+5
source share
5 answers

Very short answer :

Change this:

char **str;

For this:

char (*str)[10];

. str . int main() (void C). , undefined :

#include <stdio.h>
int main()
{
    char a[5][10]={"one","two","three","four","five"};
    char (*str)[10] = a;
    printf("%p ", &a[0]);
    printf("\n%p ", &str[0]);
    printf("\n%p ", &str[3]);
    printf("\n%p ", &str[1][56]);       // this is undefined behaviour
    printf("\n%p ", &(*(*(str+4)+1)));
    return 0;
}

, printf() &(*(...)), . , :

    printf("\n%p ", *(str+4)+1);

( )

0x7fff5fbff900 
0x7fff5fbff900 
0x7fff5fbff91e 
0x7fff5fbff942 
0x7fff5fbff929 

(, )

, . . a

char a[5][10]

    0   1   2   3   4   5   6   7   8   9 
  -----------------------------------------
0 | o | n | e | 0 |   |   |   |   |   |   |
  -----------------------------------------
1 | t | w | o | 0 |   |   |   |   |   |   |
  -----------------------------------------
2 | t | h | r | e | e | 0 |   |   |   |   |
  -----------------------------------------
3 | f | o | u | r | 0 |   |   |   |   |   |
  -----------------------------------------
4 | f | i | v | e | 0 |   |   |   |   |   |
  -----------------------------------------

, , &a[1], , &a[0] ( ). :

int main()
{
    char a[5][10] = { "one", "two", "three", "four", "five" };

    printf("&a    = %p\n", &a);
    printf("&a[0] = %p\n", &a[0]);
    printf("a[0]  = %p\n", a[0]);
    printf("&a[1] = %p\n", &a[1]);
    printf("a[1]  = %p\n", a[1]);
    return 0;
}

&a    = 0x7fff5fbff8e0
&a[0] = 0x7fff5fbff8e0
a[0]  = 0x7fff5fbff8e0
&a[1] = 0x7fff5fbff8ea
a[1]  = 0x7fff5fbff8ea

, a[1] 10 (0x0a hex) . ? , .


?

C ++ , void, . , . .

int *iptr = malloc(5*sizeof(int));

iptr , 5 . , , :

iptr[1] = 1;

:

*(iptr+1) = 1;

; 1 ( 0-). , * (, ). (iptr+1) ( int - 32 , , 64 ), ?

: - . , , , ( - int). / , " ". . :

#include <stdio.h>

typedef struct Data
{
    int ival;
    float fval;
    char buffer[100];
} Data;

int main()
{
    int ivals[10];
    int *iptr = ivals;
    char str[] = "Message";
    char *pchr = str;
    Data data[2];
    Data *dptr = data;

    printf("iptr    = %p\n", iptr);
    printf("iptr+1  = %p\n", iptr+1);

    printf("pchr    = %p\n", pchr);
    printf("pchr+1  = %p\n", pchr+1);

    printf("dptr    = %p\n", dptr);
    printf("dptr+1  = %p\n", dptr+1);

    return 0;
}

iptr    = 0x7fff5fbff900
iptr+1  = 0x7fff5fbff904
pchr    = 0x7fff5fbff8f0
pchr+1  = 0x7fff5fbff8f1
dptr    = 0x7fff5fbff810
dptr+1  = 0x7fff5fbff87c

, iptr iptr+1 ; ( int ). char pchr pchr+1 . , Data dptr dptr+1 , 0x6C 108 . ( - , , ). , 4- (int float) char 100 .

, , C/++. . :

int ar[10];
int *iptr1 = ar+1;
int *iptr5 = ar+5;

, :

printf("%lu", iptr5 - iptr1);

: 4. , . ? . .

, , :

int ar[5];
int *iptr = ar;

iptr[1] = 1;

, :

*(iptr+1) = 1;

"" " , iptr, 1 * ( int ), 1 ."

: . , ,

1[iptr] = 1;

() . , , a, ( , ):

char **str = a; // Error: Incompatible pointer types: char ** and char[5][10]

, . . char ** - char. , , . .. , , a str.

char **str = (char **)(a); // Should NEVER do this, here for demonstration only.
char *s0 = str[0];  // what do you suppose this is?

:

int main()
{
    char a[5][10] = { "one", "two", "three", "four", "five" };
    char **str = (char **)a;
    char *s0 = str[0];
    char *s1 = str[1];

    printf("&a    = %p\n", &a);
    printf("&a[0] = %p\n", &a[0]);
    printf("a[0]  = %p\n", a[0]);
    printf("&a[1] = %p\n", &a[1]);
    printf("a[1]  = %p\n", a[1]);

    printf("str   = %p\n", str);
    printf("s0    = %p\n", s0);
    printf("s1    = %p\n", s1);

    return 0;
}

:

&a    = 0x7fff5fbff900
&a[0] = 0x7fff5fbff900
a[0]  = 0x7fff5fbff900
&a[1] = 0x7fff5fbff90a
a[1]  = 0x7fff5fbff90a
str   = 0x7fff5fbff900
s0    = 0x656e6f
s1    = 0x6f77740000

, str , , s0? , ASCII . ? ASCII :

0x65 : e
0x6e : n
0x6f : o

"" ( , , . :

0x6f : o
0x77 : w
0x74 : t

, "". , ?

.. , . , , . . :

str[1]

:

*(str+1)

. . str? , , char. :

str + 0

str + 1

a char*. 4 ( 32- ). , str[1] - .

, (, , , ).

str[3] 0xbf7f6292

: :

&str[3]

:

(str + 3)

, (str + 3) - , str, 3x, str , char *, . . printf, :

0xbf7f6286

, 4 (32- ). ...

0xbf7f6286 + (3 * 4)

....

0xbf7f6286 + 0x0C = 0xbf7f6292
+8

printf, , , two-d, d. , , [0] [0].

char ** str , , str [3], internaly (str + 3), , , 32- , 12 . , str - 2- , .

0xbf7f6286 str [0] 0xbf7f6292 str [3] diff 0xc, , .

printf ( "\n% p", & str [1] [56]); printf ( "\n% p", & (((str + 4) +1)));

, , - airthematic.

, .

+3
char a[5][10]={"one","two","three","four","five"};
char **str=a;

, , , .

"" .

char   a =  5;
char  *b = &a;
char **c = &b;

, - c

printf("\n%d",*c)

, 'b', , 'a'.

- c

printf("\n%d",**c)

'a' .

.

, - .

:

int array [5] = {1,2,3,4,5};
printf("\n%p",array);
printf("\n%p",&array);
printf("\n%p",&array[0]);

.

,

char **str=a;

** str , , [name array = a].

, .

question .

+1

str + 3, , str + (3 * sizeof (char **)). , 'char **' 4 , str 12 .

0

2D-, ( "for" ) .

, ( ) , , '&' .

Since you set the array to str, you actually set the str pointer to the address pointed to by the array "a".

When you use the [int n] access operator, you actually tell your computer to look like ā€œnā€ address slots, where the array (or pointer) actually points. Thus, why is 16 more than the top two.

The last two look like Hex, I'm not quite sure about the last two output lines.

0
source

All Articles