[英]Understanding addressing of strings in 2D arrays
#include<stdio.h>
void main()
{
char s[10][10];
int i;
for(i=0;i<4;i++)
scanf("%s",s[i]);
printf("%s",s);
printf("%s",s+1);
printf("%s",s[1]+1);
}
When I type the above line of code first printf
statement will print the first string and second printf
will print the second string since s[1]
is equivalent to s+1
. 当我键入上面的代码行时,第一个
printf
语句将打印第一个字符串,第二个printf
将打印第二个字符串,因为s[1]
相当于s+1
。 But the third printf
will print the second string starting from the second character. 但第三个
printf
将从第二个字符开始打印第二个字符串。
If s[1]
is equivalent to s+1
why s[1]+1
does not give the result of s+2
? 如果
s[1]
等于s+1
为什么s[1]+1
不给出s+2
的结果?
I do not get the idea of address calculation for 2D string array. 我不知道2D字符串数组的地址计算。
The way pointer arithmetic works, s[i]
is equal to *(s + i)
. 指针算法的工作方式,
s[i]
等于*(s + i)
。 So s[1]+1
is actually *(s + 1) + 1
, which is not the same as either s + 2
or *(s + 2)
. 所以
s[1]+1
实际上是*(s + 1) + 1
,它与s + 2
或*(s + 2)
。
So, let's talk about pointer arithmetic for a second. 那么,让我们谈谈指针算法一秒钟。 Given a pointer to any type
T
: 给定指向任何类型
T
的指针:
T *p;
the expression p+1
will evaluate to the address of the next object of type T
. 表达式
p+1
将计算为T
类型的下一个对象的地址。 So if T
is int
, then p+1
will give us the address of the next int
object after p
. 所以如果
T
是int
,那么p+1
将给出p
之后的下一个int
对象的地址。 If p
is 0x8000
and sizeof (int)
is 4, then p+1
evaluates to 0x8004
. 如果
p
为0x8000
且sizeof (int)
为4,则p+1
计算结果为0x8004
。
Where things get fun is if we're working with array expressions. 如果我们正在使用数组表达式,那么事情变得有趣。 Assume the following:
假设如下:
T a[N];
Except when it is the operand of the sizeof
or unary &
operators, an expression of type "N-element array of T
" will be converted ("decay") to an expression of type "pointer to T
" and its value will be the address of the first element of the array. 除了当它是的操作数
sizeof
或一元&
运营商,类型“的N元件阵列的表达 T
”将被转换(“衰变”),以类型的“指针到一个表达T
”,其值将是数组的第一个元素的地址。
So if we write 所以,如果我们写
int a[10];
int *p = a + 1;
the expression a
is converted from "10-element array of int
" to "pointer to int
", and the value of a
is the address of the first element (ie, &a[0]
). 表达式
a
从“10个元素的int
of int
”转换为“指向int
指针”, a
的值是第一个元素的地址(即&a[0]
)。 So the expression a + 1
gives us the address of the next integer object following a
, which just happens to be &a[1]
1 . 所以表达式
a + 1
为我们提供了下一个整数对象之后的地址a
,这恰好是&a[1]
1。
Now assume we're working with a 2-D array: 现在假设我们正在使用二维数组:
int a[2][3];
int (*p)[3] = a + 1;
The expression a
has type "2-element array of 3-element array of int
". 表达
a
具有类型“的3元素数组的2个元素的数组int
”。 In this case, a
"decays" to type "pointer to 3-element array of int
", or int (*)[3]
. 在这种情况下,
a
“衰减”来输入“指针3元素数组的int
”,或int (*)[3]
So a + 1
gives us the address of the next *3-element array of int
". Again, assuming a
starts at 0x8000
and sizeof (int)
is 4, then a + 1
evaluates to 0x800c
所以
a + 1
给我们的下一* 3元素数组的地址int
”。同样,假设a
在开始0x8000
和sizeof (int)
为4,则a + 1
的计算结果为0x800c
a[i]
is evaluated as *(a+i)
;
a[i]
被评估为*(a+i)
;
that is, we're offsetting i
elements from the address specified by a
and dereferencing the result.
i
元素从a
指定的地址偏移并取消引用结果。
Note that this treats a
as a pointer, not an array.
a
视为指针,而不是数组。
In the B language (from which C is derived), the array object a
would have been a pointer object that contained the address of the first element ( a[0]
).
a
将是一个包含第一个元素( a[0]
)地址的指针对象。
Ritchie changed that in C so that the array expression would be converted to a pointer expression as necessary.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.