C数组指针运算

C array pointer arithmetic

我正在尝试重写我的代码,它接受用户输入数组,转到函数并在每个数字之间添加零并将其保存到数组 2。我的源代码工作正常但我在尝试制作时遇到了问题它使用指针运算只是为了函数访问每个数组元素,它不能是子脚本。 关于我的代码或如何执行此操作的建议,您能告诉我什么?

源代码:

#include <stdio.h>

void insert0(int n, int a1[], int a2[]);

int main(void) {

  int i;     
  int n;

  printf("Please enter the length of the input array: ");   
    scanf("%d", &n);

  int a[n];   
  int b[2*n];  

  printf("Enter %d numbers for the array: ", n);   
    for (i = 0; i < n; i++){     
      scanf("%d", &a[i]);
    }

  insert0(n, a,  b);

  printf("Output array:");   
    for (i = 0; i < 2*n; i++){
      printf(" %d", b[i]);   
        printf("\n");
    }
    return 0; 
}

void insert0(int n, int a[], int b[]) {

  int i, j = 0; 

  for(i = 0; i < n; i++, j+=2){    
    b[j]= a[i];    
      b[j+1] = 0; 
  }
}

我的算法:

   #include <stdio.h>

    void insert0(int n, int *a1, int *a2);

    int main(void) {

      int i;     
      int n;

      printf("Please enter the length of the input array: ");   
        scanf("%d", &n);

      int a1[n];   
      int a2[2*n];  

      printf("Enter %d numbers for the array: ", n);   
        for (i = 0; i < n; i++){     
          scanf("%d", &a2[i]);
        }

//not sure if this is how you call it, I've seen it called with arr
      insert0(n, a1, a2); 

      printf("Output array:");   
        for (i = 0; i < 2*n; i++){
          printf(" %d", a2[i]);   
            printf("\n");
        }
        return 0; 
    }

    void insert0(int n, int *a1, int *a2) {

      int *p;
      int j = 0;

        // I think I translated this properly     
        for(p = a1; p < a1+n; p++, j+=2){
          a2+j = a1+p;  
          //unsure how to get a2[j+1] to still work here with pointer  
            a2(j+1) = 0; 
      }
    }

您需要取消引用您的指针。

a2+j = a1+p;

没有任何效果。这是说 "set the address a2 + j to the address a1 + p," 你做不到的。要将存储在a2+j的值设置为存储在a1+p的值,需要这样做:

*(a2+j) = *(a1+p)

您需要将指针算法括在括号内,并对其使用解引用运算符。

但是,

a1 + p 并不是您想要的地址。 p 正在存储实际地址,而不仅仅是偏移量,因此您可能只需要 p.

问题一:

在您的代码中:

int a1[n];   
int a2[2*n];  

printf("Enter %d numbers for the array: ", n);   

for (i = 0; i < n; i++)
{     
    scanf("%d", &a2[i]); //maybe you are using wrong array to input elements 
}
  • 我认为您需要将元素扫描到 a1 数组中,因为它的大小是 n 并且您正在扫描 n 个元素...
  • 但是您正在将元素扫描到 a2 中,并且在 insert0() 函数中您正在借助未初始化的 a1 重写 a2 的元素。 .

解决方法:

scan elements into a1

for (i = 0; i < n; i++)
{     
    scanf("%d", &a1[i]); //scanning into `a1`
}

问题二:

  • Note : another point to note is that for a2[], (2*n)-1 elements are enough as you are assigning only zeroes in between the numbers i.e,

示例:

1,1,1 // 3 elements

1,0,1,0,1 // (2*3)-1=5 elements after inserting

解法:

    printf("Please enter the length of the input array: ");
    scanf("%d", &n);

    int a1[n];
    int a2[(2*n)-1];  

最后……

but I am having trouble trying to make it so that it uses pointer arithmetic just for the function to visit each array element, it cannot be sub scripting.

可以,可以使用子脚本!

这样 :

void insert0(int n, int *a1, int *a2)
{

    int j = 0;

    for(j=0;j<(2*n)-1; j++)
    {
        if(j%2!=0)
            a2[j]=0;
        else
            a2[j]=a1[j/2];
    }
}

所以您的代码将是:

  #include <stdio.h>

  void insert0(int n, int *a1, int *a2);

  int main(void) 
  {

    int i;
    int n;

    printf("Please enter the length of the input array: ");
    scanf("%d", &n);

    int a1[n];
    int a2[(2*n)-1];

    printf("Enter %d numbers for the array: ", n);
    for (i = 0; i < n; i++)
    {
      scanf("%d", &a1[i]);
    }

    insert0(n, a1, a2);

    printf("Output array:");
    for (i = 0; i < 2*n-1; i++)
    {
        printf(" %d", a2[i]);
        printf("\n");
    }
    return 0;
}

void insert0(int n, int *a1, int *a2)
{

    int j = 0;

    for(j=0;j<(2*n)-1; j++)
    {
        if(j%2!=0)
            a2[j]=0;
        else
            a2[j]=a1[j/2];
    }
}

输入:

3
1 2 3

输出:

1
0
2
0
3

此回复并非旨在演示指针算法的使用效率,而是展示数组索引(下标)如何转化为指针算法,并应用于您的代码。请查看 this SO post,其中包含来自 C 标准的有价值的信息。

你的代码也有一些逻辑错误,读入和写入错误的数组等等......你自己可以很容易地找到它们。这是一个有效的修复:

#include <stdio.h>

void insert0(int n, int *a1, int *a2) {
  int p;    //just a normal int for counter
  int j = 0;

    for(p = 0; p < n; p++, j+=2){
      *(a2+j) = *(a1+p);   //values pointed at are getting assigned
                           //equivalent to a2[j] = a1[p]
      if(p < n - 1 )       //we insert only between numbers
        *(a2 + j + 1) = 0; //pointer pointing at memory into which 0 is copied incremented by 1
    }
 }


int main(void) {
  int i;     
  int n;

  printf("Please enter the length of the input array: ");   
    scanf("%d", &n);

  int a1[n];   
  int a2[2*n];  //one element too long, should be a2[n+n-1];

  printf("Enter %d numbers for the array: ", n);   //reading into smaller array
    for (i = 0; i < n; i++){     
      scanf("%d", &a1[i]);
    }

  insert0 (n, a1, a2); //this is OK

  printf("Output array: ");   
  for (i = 0; i < 2*n - 1; i++){
     printf(" %d", a2[i]);              //printing from the bigger, zero-padded array
  }
  printf("\n");
  return 0; 
}