简体   繁体   English

了解2D数组中字符串的寻址

[英]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 . 所以如果Tint ,那么p+1将给出p之后的下一个int对象的地址。 If p is 0x8000 and sizeof (int) is 4, then p+1 evaluates to 0x8004 . 如果p0x8000sizeof (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在开始0x8000sizeof (int)为4,则a + 1的计算结果为0x800c


1. The expression a[i] is evaluated as *(a+i) ; 1.表达式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] ). 在B语言中(从中派生出C语言),数组对象a将是一个包含第一个元素( a[0] )地址的指针对象。 Ritchie changed that in C so that the array expression would be converted to a pointer expression as necessary. Ritchie在C中对此进行了更改,以便根据需要将数组表达式转换为指针表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM