简体   繁体   English

C数组指针算法

[英]C array pointer arithmetic

I am trying to rewrite my code that takes the user input array, goes to the function and adds zeros between each number and saves it to array 2. My source code works just fine 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. 我正在尝试重写使用用户输入数组的代码,转到该函数并在每个数字之间添加零,然后将其保存到数组2。我的源代码工作得很好,但在尝试使其使用时遇到了麻烦仅用于函数访问每个数组元素的指针算法,不能是子脚本。 What can you tell me about my code or suggestions on how to do this? 关于我的代码或有关此操作的建议,您能告诉我什么?

Source code: 源代码:

#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; 
  }
}

My arithmetic: 我的算术:

   #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; 
      }
    }

You need to dereference your pointer. 您需要取消引用指针。

a2+j = a1+p;

Doesn't have any effect. 没有任何效果。 This is saying "set the address a2 + j to the address a1 + p," which you can't do. 这就是说“不能将地址a2 + j设置为地址a1 + p”。 To set the value stored at a2+j to the value stored at a1+p, you need to do this: 要将存储在a2 + j的值设置为存储在a1 + p的值,您需要执行以下操作:

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

You need to enclose your pointer arithmetic inside parentheses, and use the dereference operator on it. 您需要将指针算术放在括号内,并在其上使用解引用运算符。

a1 + p , though, isn't the address you want. 但是, a1 + p并不是您想要的地址。 p is storing the actual address, not just the offset, so you probably just want p . p正在存储实际地址,而不仅仅是偏移量,因此您可能只需要p

Problem 1: 问题一:

Here in your code : 在您的代码中:

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 
}
  • I think here you need to scan elements into a1 array as it's size is n and you are scanning n elements... 我认为这里您需要将元素扫描到a1数组中,因为它的大小为n并且您正在扫描n元素...
  • but you are scanning elements into a2 and further in the insert0() function you are rewriting the elements of a2 with help of a1 which is uninitialized... 但是您正在将元素扫描到a2并且进一步在insert0()函数中,您将借助于未初始化的a1来重写a2的元素。

solution: 解:

scan elements into a1 将元素扫描到a1

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

Problem 2: 问题2:

  • 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 ie, 注意 :要注意的另一点是,对于a2[](2*n)-1元素就足够了,因为您在数字之间仅分配了零,即

example : 例如:

1,1,1 // 3 elements

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

Solution : 解决方案:

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

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

and finally.... 最后...

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. 但是我很难做到这一点,以便它仅使用指针算法来访问每个数组元素的函数,它不能是子脚本。

Yes, sub-scripting can be used! 是的,可以使用子脚本!

this way : 这条路 :

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];
    }
}

So your code would be : 因此您的代码将是:

  #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];
    }
}

input : 输入:

3
1 2 3

output : 输出:

1
0
2
0
3

This is a reply which is not intended to be a demo in efficiency of using pointer arithmetic, but rather to show how array indexing (subscripting) translates into pointer arithmetic, applied to your code. 这不是一个旨在提高使用指针算术效率的演示,而是说明如何将数组索引(下标)转换为适用于您的代码的指针算术。 Please, take a look at this SO post containing valuable information from the C standard. 请看一下这篇SO帖子,其中包含来自C标准的宝贵信息。

Your code also has few logical bugs, reading into and writing from wrong arrays, etc… You can easily find them yourself. 您的代码还包含一些逻辑错误,无法读取和写入错误的数组,等等。。。您可以轻松地自己找到它们。 Here's a working fix: 这是一个可行的解决方案:

#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; 
}

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

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