使用指针指向C中的一行

发布于 2025-02-12 01:27:34 字数 114 浏览 0 评论 0原文

如果我有数组a,如何将指针设置为第一行?

double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};

If I have array a, how would I set a pointer to the first row?

double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月牙弯弯 2025-02-19 01:27:34

您可以声明指向行的指针并将其初始化以用以下行指向第一行:

double (*p_first_row)[4] = &a[0];

由于阵列到驱动器decinter ,您还可以写:

double (*p_first_row)[4] = a;

括号是必要的,因为声明

double *p[4];

声明了一系列指针,而声明则

double (*p)[4];

声明了指向数组的指针。

You can declare a pointer to a row and initialize it to point to the first row with the following line:

double (*p_first_row)[4] = &a[0];

Due to array to pointer decay, you can also write:

double (*p_first_row)[4] = a;

The parentheses are necessary, because the declaration

double *p[4];

declares an array of pointers, whereas the declaration

double (*p)[4];

declares a pointer to an array.

原来分手还会想你 2025-02-19 01:27:34

就像您对任何指针所做的那样,它正常地将其解雇。
*(a + 0)给您矩阵的第一行,*(a + i)将为您提供i-th在2D数组中行。

示例代码以获取2D数组每一行中的第一个元素看起来像这样。

double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};

for (int i = 0; i < 2; i ++) {
  printf("%lf ", *(a + i)[0]);
}

输出:

1 5

Just dereference it normally as you would do to any pointer.
*(a + 0) gives you the first row of the matrix, and *(a + i) will give you the i-th row in the 2D array.

A sample code to get the first element in each row of your 2d array would look like this.

double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};

for (int i = 0; i < 2; i ++) {
  printf("%lf ", *(a + i)[0]);
}

Output:

1 5
爱,才寂寞 2025-02-19 01:27:34

如果您有一个多维数组,例如

T a[N1][N2][N3][N4];

t是某种类型的指示符,n1n2n3n4是一些积极的整数,然后将指针指向数组的第一个元素,只需将左侧的尺寸更改为星号,例如

T ( *p )[N2][N3][N4] = a;

在此声明中,数组设计师 a is隐式转换为指针转换为其第一个元素。

如果要获取一个指向i-th(0&lt; = i&lt; n1)元素的指针(这是类型t [n2]的数组] [n4])您可以写作

T ( *p )[N2][N3][N4] = a + i;

T ( *p )[N2][N3][N4] = a;
p += i;

这是一个演示程序。

#include <stdio.h>

int main( void )
{
    double a[2][4] = 
    {
        {1, 2, 3, 4}, 
        {5, 6, 7, 8}
    };

    for ( double ( *row )[4] = a; row != a + 2; ++row )
    {
        for ( double *p = *row; p != *row + 4; ++p )
        {
            printf( "%.1f ", *p );
        }

        putchar( '\n' );
    }
}

程序输出是

1.0 2.0 3.0 4.0 
5.0 6.0 7.0 8.0 

If you have a multi-dimensional array like for example

T a[N1][N2][N3][N4];

where T is some type specifier and N1, N2, N3, N4 are some positive integers then to make a pointer to the first element of the array just change the left most dimension to asterisk like

T ( *p )[N2][N3][N4] = a;

In this declaration the array designator a is implicitly converted to a pointer to its first element.

If you want to get a pointer to the i-th (0 <= i < N1) element of the array (that is an array of the type T[N2][N3][N4]) you can write

T ( *p )[N2][N3][N4] = a + i;

or

T ( *p )[N2][N3][N4] = a;
p += i;

Here is a demonstration program.

#include <stdio.h>

int main( void )
{
    double a[2][4] = 
    {
        {1, 2, 3, 4}, 
        {5, 6, 7, 8}
    };

    for ( double ( *row )[4] = a; row != a + 2; ++row )
    {
        for ( double *p = *row; p != *row + 4; ++p )
        {
            printf( "%.1f ", *p );
        }

        putchar( '\n' );
    }
}

The program output is

1.0 2.0 3.0 4.0 
5.0 6.0 7.0 8.0 
草莓酥 2025-02-19 01:27:34
  • C中的2D数组是数组数组。
  • double a [2] [4] =一个大小2的数组,每个类型double [4]的项目。
  • 指向此类数组的指针被声明为double(*p)[2] [4],并初始化/分配为p =&amp; a
  • 同样,指向类型double A [4]的指针被声明为double(*p)[4]
  • C中的任何数组,每当大多数表达式中使用时,都会“衰减”到指向其第一个元素的指针中。
  • 由于的第一项double a [2] [4]具有类型double [4],然后a将腐烂到指向此类的指针一项,double(*)[4],无论何时在表达式中使用。

因此,如果我们愿意,我们可以通过double a [2] [4]迭代:

double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};

for(double (*p)[4]=a; p<a+2; p++)
{
   printf("%lf %lf %lf %lf\n", (*p)[0],(*p)[1],(*p)[2],(*p)[3]);
}

这也等同于double(*p)[4] =&amp; a [0 ]


现在,假设我们应该写一些带有saner语法的表达式:

a[i][j]

任何a [i]表达式在定义上都是100%等于*(a+i)。因此,以上等同于*(*(A+I)+J)

这里使用两次指针算术:a+idouble( *)[4] [4]类型,用i * sizeof(double)增加地址的指针算术[4])字节。而+j部分是double *上的指针算术,而将地址用j * sizeof(double) bytes增加。

因此,a [1] [0] = (char *)A + 1 * sizeof(double [4]) + 0 * sizeof(double)

示例:

#include <stdio.h>

int main()
{
   double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
   printf("%lf ", a[1][0] );
   printf("%lf ", (char*)a + 1 * sizeof(double[4]) + 0 * sizeof(double) );
}
  • A 2D array in C is an array of arrays.
  • double a[2][4] = an array with size 2, each items of type double[4].
  • A pointer to such an array is declared as double (*p)[2][4] and initialized/assigned as p=&a.
  • Similarly, a pointer to an array of type double a[4] is declared as double (*p)[4].
  • Any array in C, whenever used in most expressions, "decays" into a pointer to its first element.
  • Since the first item of double a[2][4] has type double [4], then a will decay into a pointer to such an item, double (*)[4], whenever used in an expression.

Therefore we can iterate through the double a[2][4] like this, if we wish:

double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};

for(double (*p)[4]=a; p<a+2; p++)
{
   printf("%lf %lf %lf %lf\n", (*p)[0],(*p)[1],(*p)[2],(*p)[3]);
}

Which is also equivalent to double (*p)[4]=&a[0].


Now suppose that we write an expression some with saner syntax as we ought to:

a[i][j]

Any a[i] expression is by definition 100% equivalent to *(a+i). So the above is equivalent to *(*(a+i)+j).

Here pointer arithmetic is used twice: a+i is pointer arithmetic on a double(*)[4] type, increasing the address with i * sizeof(double[4]) bytes. Whereas the +j part is pointer arithmetic on a double*, increasing the address with j * sizeof(double) bytes.

Thus a[1][0] = (char*)a + 1 * sizeof(double[4]) + 0 * sizeof(double)

Example:

#include <stdio.h>

int main()
{
   double a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
   printf("%lf ", a[1][0] );
   printf("%lf ", (char*)a + 1 * sizeof(double[4]) + 0 * sizeof(double) );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文