简体   繁体   English

在C语言中将指针与Array一起使用

[英]Using pointers with Array in C

What would be the output of the following program? 以下程序的输出是什么?

int main(void) {
    int n[3][3] = {
        2, 4, 3,
        6, 8, 5,
        3, 5, 1
    };
    int i, *ptr;
    ptr = n;
    for (i = 0; i <= 8; i++)
        printf("\n%d", *(ptr + i));
}

Here what does n means wrt to two dimensional array? 在这里, n对二维数组意味着什么? And what ptr will have? 还有什么ptr? I am having lot of confusion using pointers with Array. 使用数组指针时,我感到很困惑。

Output is coming as 4 everytime. 每次输出为4。 I am trying to understand why it is printing 4 everytime? 我想了解为什么每次都打印4个?

Any explanation will help me a lot. 任何解释都会对我有很大帮助。

ptr is a pointer that "points" to the first element of the 'n' matrix. ptr是“指向” n矩阵的第一个元素的指针。 You make it doing: 您可以做到:

ptr = n[0];

You can't do: 您不能:

ptr = n;

because 'n' is an "array" of "arrays" (the first array store 2, 4 and 3, the second 6, 8 and 5, and so on...) 因为'n'是“数组”的“数组”(第一个数组存储2、4和3,第二个数组存储6、8和5,依此类推...)

When you declare the 'n' two-dimensional array, the elements aree stored in memory in lineal order (one integer allocation after another) and thats the reason why you can make 'n' point to one element (the first element in this case) and then make it point to the next element (by adding 1 to the pointer, this is called "pointer arithmetic"). 当您声明“ n”个二维数组时,这些元素按线性顺序存储在内存中(一个整数分配接一个整数分配),这就是为什么可以使“ n”指向一个元素(在这种情况下为第一个元素)的原因),然后使其指向下一个元素(通过在指针上加1,这称为“指针算术”)。

This is the little correction to make the program works in the right way: 这是使程序以正确的方式工作的微小修正:

#include <stdio.h>

int main(void) {
    int n[3][3] = {
        2, 4, 3,
        6, 8, 5,
        3, 5, 1
    };
    int i, *ptr;
    ptr = n[0];
    for (i = 0; i <= 8; i++)
        printf("\n%d", *(ptr + i));
}

The output wil be: 输出将是:

2 4 3 6 8 5 3 5 2 4 3 6 8 5 3 5

Output: 输出:

2 4 3 6 8 5 3 5 1

print all elements in arry orderly. 按顺序打印所有元素。 array is contiguous, ptr=n; 数组是连续的,ptr = n; alloted address of n; n的分配地址;

n gives address of first row or address of first element. n给出第一行的地址或第一元素的地址。

printf("%d %d %d",n,&n[0],&n[0][0]);

gives same address. 给出相同的地址。

arr[i] is same as *(arr+i) arr [i]与*(arr + i)相同

  n
  n[0] or(n+0)
  &n[0][0]
   |
   |
   v
   2    4    3    6   8   5      3  5  1 
|              |             |             |
|[0][0]        |             |             |
|--row 0-------| row 1       |----row 2----|            

each time when you do ptr+i its doing pointer arithmetic adds sizeof(int) to ptr results in next element in memory. 每次执行ptr + i时,其执行指针运算都会将sizeof(int)添加到ptr结果中的内存中的下一个元素中。

ptr=n; will assign the starting address of the 2D array n to the pointer variable ptr . 2D array n的起始地址分配给指针变量ptr In order to avoid the warning assign ptr = &n[0][0]; 为了避免警告,分配ptr = &n[0][0]; . Even though both n and &n[0][0] gives you the same address their types are different. 即使n和&n [0] [0]都为您提供相同的地址,但它们的类型却不同。 n is the starting address of the 2D array n and &n[0][0] is the address of the first element of the array which is n[0][0] . n2D array n的起始地址, &n[0][0]是数组的第一个元素n[0][0]

The following modified program will tell you the difference, 以下经过修改的程序将告诉您不同之处,

int main()
{
    int n[3][3] = {2, 4, 3, 6, 8, 5, 3, 5, 1};
    int i, *ptr1, (*ptr2)[3];
    ptr1 = &n[0][0];
    ptr2=n;
    printf("\n%p",ptr1);
    printf("\n%p",ptr2);
    printf("\n%p",++ptr1);
    printf("\n%p",++ptr2);
}

Initially both ptr1 and ptr2 will print the same address. 最初, ptr1ptr2都将打印相同的地址。 But after incrementing ptr1 and ptr2 will point to different addresses as their types are different. 但是,在递增之后, ptr1ptr2将指向不同的地址,因为它们的类型不同。

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

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