【问题标题】:Print elements after place in array in c在c中放入数组后打印元素
【发布时间】:2016-03-23 08:21:22
【问题描述】:

所以我尝试在某个点或位置之后打印数组元素 但它给我打印了另外一些垃圾。代码需要打印7,8,9,5。 我确定问题出在这条线上:

for (arr=x+1;arr;arr++) 但我不明白该写什么来代替这一行。

请帮助我并使用 * 或 & 代替 [](使用指针)。谢谢!

#include <stdio.h>
#include <stdlib.h>

void printAfterX(int* arr, int n, int* x);

int main(void)
{
    int arr[] = { 4, 8, 6, 2, 1, 3, 5, 7, 8, 9, 5 };
    printAfterX(arr, 11, arr + 6);

    system("PAUSE");    
    return 0;
}

void printAfterX(int* arr, int n, int* x)
{
    if (n >= arr)
    {
        printf("not found");
    }
    else
    {
        for (arr=x+1; arr < arr+ n; arr++)
        {
            printf("%d  ",*arr);
        }
    }
}

【问题讨论】:

  • 函数printAfterX的第二个参数是什么?
  • 编译时启用所有警告并将警告视为错误。
  • @HassanNadeem 数组的大小
  • @saymaname if (n &gt;= arr) 没有任何意义,您正在将整数与数组进行比较。我你需要这个if (n+arr &gt;= x)
  • n 在您的代码中是 11arr &lt; arr + n 中的 is 情况永远不会是假的。

标签: c arrays pointers


【解决方案1】:

为什么需要混合使用指针和索引?你是对的,在for (arr=x+1; arr &lt; arr+n; arr++) 行中存在问题,因为arr &lt; arr + n 对于正n 总是正确的。所以,这个循环永远不会结束。

还不清楚if (n &gt;= arr) 是什么意思。在这里,您比较数组中的元素数量和该数组上的指针。这是没用的。看来你需要比较xarr

我建议您使用元素索引来简化您的代码。试试这个:

void printAfterX(int* arr, int n, int x)
{    
    if (x < 0 || x+1 >= n)
    {
        printf("not found");
    }
    else
    {
        for (int i = x+1; i < n; i++)
        {
            printf("%d  ", arr[i]);
        }    
    }
}

要调用此函数,您需要更改第三个参数:

printAfterX(arr, 11, 6);

另外最好避免使用system("pause") - 在这里阅读:system("pause"); - Why is it wrong?

更新:

好的,如果你需要使用指针,试试这个:

void printAfterX(int* arr, int n, int* x)
{

    if (x < arr || x+1 > arr+n)
    {
        printf("not found");
    }
    else
    {            
        for (x = x+1; x < arr + n; x++)
        {
            printf("%d  ", *x);
        }

    }
}

【讨论】:

  • 但我需要这个没有任何其他变量。只需 x,n,arr
【解决方案2】:

很明显arr &lt; arr + n 总是成立,除非它有溢出,所以循环不会在溢出之前停止。

此外,通过编写n &gt;= arr,您将intint * 进行比较,这绝对没有意义。要检查数组界限,您还应该检查它是否小于下限。

最后,我认为最好使用一个简单的while循环。

这里是精炼的代码:

#include <stdio.h>
#include <stdlib.h>

void printAfterX(int *arr, int n, int *x);

int main(void)
{
    int arr[] = { 4, 8, 6, 2, 1, 3, 5, 7, 8, 9, 5 };
    printAfterX(arr, 11, arr + 6);

    getchar();
    return 0;
}

void printAfterX(int *arr, int n, int *x)
{
    if (x < arr || x >= arr + n)
    {
        printf("No such element");
    }
    while(x < arr + n)
    {
        printf("%d  ", *x);
        x++;
    }
}

【讨论】:

    【解决方案3】:

    for (arr=x+1; arr &lt; arr+ n; arr++) 行是一个无限循环,因为arr &lt; arr+ n 始终为真。

    同样在if (n &gt;= arr) 行中,您将一个int 与一个指针进行比较,这是没有意义的。

    像这样修改你的函数:

    void printAfterX(int* arr, int n, int* x)
    {
    
        if (n <= x-arr) //checking it doesn't exceeds the size
        {
            printf("not found");
        }
        else
        {
            for (x++; x < arr+ n; x++)
            {
                printf("%d  ",*x);
            }
    
        }
    }
    

    【讨论】:

      【解决方案4】:

      您必须将元素的数量作为参数传递给函数才能知道何时到达数组的末尾。

      您也无法将int 类型的变量与int * 类型的变量进行比较。试试这个版本:

      #include <stdio.h>
      #include <stdlib.h>
      
      void printAfterX(int* arr, size_t size, size_t offset);
      
      int main(void)
      {
          int arr[] = { 4, 8, 6, 2, 1, 3, 5, 7, 8, 9, 5 };
          printAfterX(arr, 11, 7);
      
          system("PAUSE");
          return 0;
      }
      
      void printAfterX(int* arr, size_t size, size_t offset)
      {
          if (offset >= size)
          {
              printf("not found");
              return;
          }
          else
          {
              while(offset < size)
              {
                  printf("%d  ", *(arr + offset));
                  offset++;
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

        试试看:

        #include <stdio.h>
        #include <stdlib.h>
        
        void printAfterX(int[], int, int);
        
        int main(void)
        {    
            int arr[] = { 4, 8, 6, 2, 1, 3, 5, 7, 8, 9, 5 };
            printAfterX(arr, 11, 6);    
            system("PAUSE");       
            return 0;   
        }
        
        void printAfterX(int arr[], int n, int x)    
        {    
            if (n <= arr[x])   
            {   
                printf("not found");    
            }    
            else    
            {    
                for (i=x+1; i < n; i++)    
                {    
                    printf("%d  ",arr[i]);    
                }      
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多