使用指针从 C 中的函数调用数组元素

Using Pointers in to call array elements from a function in C

我正在尝试编写移动和切换数组前三个元素的代码。 main函数获取数组长度和元素,然后显示函数调用的结果。函数 roll 从数组 a1 中获取元素,移动前三个元素,然后将它们存储到数组 a2 中。我必须使用指针下标并且不能在函数中有[]。我必须使用这个原型:

void roll(int *a1, int n, int *a2)

这是我编写的代码。我一定没有正确调用函数,或者函数没有正确地将元素分配到数组 a2 中,因为我只得到 0 输出。

#include <stdio.h>

void roll(int *a1, int n, int *a2)
{
    int *p, *q;
    q = a2;

    for(p = a1; p < a1 + n; p++)
            *p = *(p+2);
            *(p+1) = *p;
            *(p+2) = *(p+1);
            *q = *p;
}

int main(void)
{
    int i, x;

    printf("Enter the length of the array:\n");
            scanf("%d", &x);
    int a[x];
    int b[x];
    //      int new_arr[x];

    printf("Enter the elements of the array:\n");
            for(i = 0; i < x; i++)
                    scanf("%d", &a[i]);

    roll(a, x, b);
    printf("The output array is:");
    //      for(i = 0; i < x; i++);
            printf("%d", *b);

    return 0;
}

所以,我还没有编译这个,但是:

for(p = a1; p < a1 + n; p++)
    *p = *(p+2);
    *(p+1) = *p;
    *(p+2) = *(p+1);
    *q = *p;

只会 运行 第一行 *p = *(p+2) 通过 for () 循环。

你需要在里面放上牙套:

for(p = a1; p < a1 + n; p++)
{
    *p = *(p+2);
    *(p+1) = *p;
    *(p+2) = *(p+1);
    *q = *p;
}

有点不清楚你的意思:

Function 'roll' takes elements from array a1, moves the first three elements, and then stores them to array a2.

对数组的传统 Roll 操作会将 nth 元素与首先(历史起源是计算器堆栈上的值滚动到当前行)。如果是这种情况,您可以使用一个简单的:

void roll (int *a1, int n, int *a2)
{
    int tmp = *a1;          /* save the 1st element of a1 */

    *a2++ = *(a1++ + --n);  /* store the nth element of a1 as a2[0] */
    while (--n)             /* for the next n-2 elements */
        *a2++ = *a1++;      /* assign to a2 in order */
    *a2 = tmp;              /* assign the 1st of a1 as the nth of a2 */
}

你可以用任何你喜欢的方式索引它,简单地递减 'n' 是最简单的方法。将 a1 的第一个元素保存为 tmp 允许您直接递增 a1a2(您的 roll 函数接收指针的副本)。

总而言之,您可以这样做:

#include <stdio.h>

void roll (int *a1, int n, int *a2)
{
    int tmp = *a1;          /* save the 1st element of a1 */

    *a2++ = *(a1++ + --n);  /* store the nth element of a1 as a2[0] */
    while (--n)             /* for the next n-2 elements */
        *a2++ = *a1++;      /* assign to a2 in order */
    *a2 = tmp;              /* assign the 1st of a1 as the nth of a2 */
}

int main(void)
{
    int i, x;

    printf ("Enter the length of the array: ");
    if (scanf("%d", &x) != 1) {
        fprintf (stderr, "error: invalid input.\n");
        return 1;
    }
    int a[x], b[x], *p = b;

    printf ("Enter the elements\n");
    for (i = 0; i < x; i++) {
        printf ("  a[%2d]: ", i);
        if (scanf ("%d", &a[i]) != 1) {
            fprintf (stderr, "error: invalid input.\n");
            return 1;
        }
        b[i] = a[i];    /* initialize b */
    }

    roll (a, x, b);

    printf("The output array is:");
    while (x--)
        printf (" %2d", *p++);
    putchar ('\n');

    return 0;
}

(注意: 如果您的目的只是将元素 1 和 3 从 a1 交换到 a2,则 'n' 不提供函数中的目的除了允许检查 n > 2 以确保您不会尝试访问范围外的数组,但这本身似乎值得怀疑)

例子Use/Output

$ ./bin/array_roll
Enter the length of the array: 5
Enter the elements
  a[ 0]: 1
  a[ 1]: 2
  a[ 2]: 3
  a[ 3]: 4
  a[ 4]: 5
The output array is:  5  2  3  4  1

检查一下,如果您还有其他问题,请告诉我。另外,如果您所说的 Roll 与我描述的传统 roll 不同,请添加一行。

如果您只是想对 a2a1 的前 3 个元素进行操作(注意:b 将需要完全初始化,如上所述),你可以简单地使用:

roll (a, 3, b);

示例w/roll (a, 3, b)

$ ./bin/array_roll
Enter the length of the array: 5
Enter the elements
  a[ 0]: 1
  a[ 1]: 2
  a[ 2]: 3
  a[ 3]: 4
  a[ 4]: 5
The output array is:  3  2  1  4  5

示例滚动元件 3 向下,将剩余部分向上推

在您的注释定义 "roll" 之后,只需稍作调整即可将当前的 roll 函数更改为 roll3 函数将 a1 中的第 3 个元素滚动到 a2 的开头,然后按顺序用 a1 的剩余元素填充 a2 的剩余部分。这里的转折不是循环 --n 而是循环 for (int i = 0; i < n-1; i++) 没有帮助,因此您可以测试滚动索引并跳过从 a1a2 的复制,例如(if (i == 2) {/* skip */}).

你可以这样做:

void roll3 (int *a1, int n, int *a2)
{
    *a2++ = *(a1 + 2);      /* store the 3rd element of a1 as a2[0] */
    n--;                    /* decrement n (we have filled 1-element) */
    for (int i = 0; i < n; i++, a2++) {  /* loop n-1 times, increment a2 */
        if (i == 2)         /* if we reach rolled index, increment a1 */
            a1++;
        *a2 = *a1++;        /* assign elements */
    }
}

呼叫为:

    roll3 (a, x, b);

带有 roll3 函数的示例

$ ./bin/array_roll
Enter the length of the array: 5
Enter the elements
  a[ 0]: 1
  a[ 1]: 2
  a[ 2]: 3
  a[ 3]: 4
  a[ 4]: 5
The output array is:  3  1  2  4  5

检查一下,如果您有任何问题,请告诉我。至此,处理指针increment/decrement和循环填充应该开始深入了。如果没有,最好的学习方法是拿出2号铅笔和纸片,画a1a2 然后在分配每个元素时逐步执行循环绘制线,以查看哪个元素要去哪里以及如何去。

检查这是否是您想要的。

#include <stdio.h>

void roll(int *a1, int n, int *a2)
{
int *p, *q;
q = a2;
p = a1;

        int temp=*p;
        *p = *(p+2);
        *(p+2)=*(p+1);
        *(p+1)=temp;

        for(int i=0;i<n;i++)
        {
            *(a2+i)=*(p+i);

        }


}

int main(void)
{
int i, x;

printf("Enter the length of the array:\n");
        scanf("%d", &x);
int a[x];
int b[x];
  //      int new_arr[x];
printf("Enter the elements of the array:\n");
        for(i = 0; i < x; i++)
               {
                   scanf("%d", &a[i]);
                    b[i]=a[i];
               }
roll(a, x, b);

  for(i = 0; i < x; i++)
        printf("%d\n", *(b+i));

return 0;

}

致所有帮助过的人……我让它工作了……问题出在我的电话上……我有一个额外的“;”在从 运行 停止它的 for 循环之后...非常感谢大家!