试图改变一个短程序来使用指针

时间:2015-03-13 07:35:01

标签: c pointers

本周我刚开始学习指针,而且我在实验室工作中遇到了麻烦。我给了程序,并且必须改变它以使用旧的和新的变量的指针。 (代码链接在这篇文章的底部)

我试过了,但我很困惑并遇到了几个问题:

1)我的值未被输入正确的索引。

2)当我尝试添加第3个值时出现分段错误。我知道当我尝试访问我无法访问的内存时会发生这种情况,所以我的指针在某处出错了。

非常感谢任何形式的指导!

原始工作代码:

#include <stdio.h>
#define ARSZ 10

typedef unsigned int UINT;

void showData(int [], UINT, UINT, int, int);

int main(void){

   int array[ARSZ] = {0};                 
   UINT old = ARSZ-1, new = ARSZ-1;        
   int runningTot = 0, maxAveLen, curAveLen = 1, newVal, numCtr=0;

   printf("Enter the maximum number of values to average over: ");
   scanf("%d", &maxAveLen);

   while(1){

      runningTot -= array[old];            // subtract value pointed to by old
      old += ((++numCtr) >= maxAveLen);    // increment old if maxAveLen inputs
      old %= ARSZ;                         // wraps old to 0 if array sz exceeded

      printf("Enter new value to array:"); // get new value to add to array
      scanf("%d", &newVal);       

      new = (++new)%ARSZ;                  // increment new reset to 0 if >= ARSZ
      runningTot += (array[new] = newVal); // add new Value at [new] location

      // Determine the number of values to average over i.e. 1,2,3,4,4,4,4,4...
      curAveLen = (numCtr >= maxAveLen)?maxAveLen:numCtr;
      showData(array, old, new, runningTot, curAveLen);

   }
}

// Display the current data
void showData(int ar[], UINT O, UINT N, int RT, int aveLen){ 

      for (int i=0; i < ARSZ; i++)         // Print out the array contents    
         printf("%d\t", ar[i]);

         printf("\nOld = %u, \tNew = %u, \tTotal = %d, \tAve = %5.2f\n\n", 
     O, N, RT, (float)RT/aveLen);

} 

我的破码:

#include <stdio.h>
#define ARSZ 10

typedef unsigned int UINT;

void showData(int [], UINT*, UINT*, int, int);

int main(void){

   int array[ARSZ] = {0};                  
   UINT *old = &array[ARSZ-1], *new = &array[ARSZ-1];      
   int runningTot = 0, maxAveLen, curAveLen = 1, newVal, numCtr=0;

   printf("Enter the maximum number of values to average over: ");
   scanf("%d", &maxAveLen);

   while(1){

      runningTot -= array[*old];            
      *old += ((++numCtr) >= maxAveLen);    
      *old %= ARSZ;                         

      printf("Enter new value to array:"); 
      scanf("%d", &newVal);       

      *new = *(++new)%ARSZ;                  // Error might be occurring here
      runningTot += (array[*new] = newVal); 

      curAveLen = (numCtr >= maxAveLen)?maxAveLen:numCtr;
      showData(array, old, new, runningTot, curAveLen);

   }
}

// Display the current data
void showData(int ar[], UINT *O, UINT *N, int RT, int aveLen){ 

      for (int i=0; i < ARSZ; i++)            
         printf("%d\t", ar[i]);
      printf("\nOld = %u, \tNew = %u, \tTotal = %d, \tAve = %5.2f\n\n",
     *O, *N, RT, (float)RT/aveLen);

} 

1 个答案:

答案 0 :(得分:1)

这些不等同

UINT *old = &array[ARSZ-1], *new = &array[ARSZ-1]; // ...but only to initialize pointers

UINT old = ARSZ-1, new = ARSZ-1; // ...but only to initialize pointers

你不能指向一个常量。你可以做的是为&#34; old&#34;分配内存。和&#34;新&#34;使用malloc()或通过为其分配地址 另一个有效的UINT。然后你可以分配一个常数。

这也是错的: *new = *(++new)%ARSZ; // increment new reset to 0 if >= ARSZ  您正在递增指针本身而不是指针内的值。试试这个:

*new = (++*new)%ARSZ;