【问题标题】:how to increment index of array of pointer values?如何增加指针值数组的索引?
【发布时间】:2014-12-09 05:41:00
【问题描述】:

初学者问题。我对指针有所了解,但是在尝试使用函数中的指针打印数组的值时遇到了很多麻烦。 特别是在这一行: printf("\n%d\t\t%d\t0x%X", i+1, *(arr+i) , arr);我的输出是一个表格,显示输入计数、用户输入的值以及存储的地址。地址看起来不错,但值打印垃圾。我认为在尝试使用索引 i 递增指针数组时语法 *(arr+i) 是正确的??

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


void function(int *arr[]);
main()
{
    int array[5];
    function(&array);
}


void function(int *arr[])
{
    int i, n;
    printf("How many elements? ");
    scanf("%d", &n);

    for(i=0; i<n; i++)
    {
        printf("Enter element %d: ", i);
        scanf("%d", arr);
        arr++;
    }

    printf("\nInput\t\tValue\tAddress");
    for(i=0; i<n; i++)
    {
    printf("\n%d\t\t%d\t0x%X", i+1, *(arr+i) , arr);
    arr++;
    }

}

【问题讨论】:

  • int *arr[] 等价于int **arr,一个指向(数组)指针的指针。编译器应该在调用时生成警告,因为&amp;array 的类型为int *

标签: c arrays pointers


【解决方案1】:

int *arr[] 是一个指针数组。

在向它们写入内容之前,您需要为这些指针分配内存。喜欢

arr[0] = malloc(sizeof(int));

所以你的代码应该是这样的:

    a[0] = malloc(sizeof(int) *n);
    for(i=0;i<n;i++)
    {
       scanf("%d",(a+i));
       printf("%d\n",*(a+i));
    }

在您的代码中,您似乎只是在读取数组的值并打印它们。你可以使用

int *arr = malloc(sizeof(int) *n);

那么做

for(i=0; i<n; i++)
{
    printf("Enter element %d: ", i);
    scanf("%d", &arr[i]);
}

【讨论】:

  • 你能给我解释一下 malloc 的使用吗?我在 for 循环之前输入该行,它给了我错误“预期 int ** 但参数类型为 int (*)[5]”
  • @steeele 你这里真的有指针数组吗?你不能只使用如图所示的单个指针吗?
  • 是的,它是我课堂练习的一部分。不仅如此,我真的很好奇我做错了什么,现在让我感到困惑。
【解决方案2】:

试试下面的

#include<stdio.h>

#define N 5

void function( int arr[] );

int main( void )
{
    int array[N];

    function( array );
}


void function( int arr[] )
{
    int i, n;
    int *p = arr;

    printf( "How many elements? " );
    scanf( "%d", &n );

    if ( N < n ) n = N;

    for ( i = 0; i < n; i++ )
    {
        printf( "Enter element %d: ", i );
        scanf( "%d", p++ );
    }

    printf( "\nInput\t\tValue\tAddress" );
    for ( i = 0; i < n; i++ )
    {
        printf( "\n%d\t\t%d\t%p", i+1, *( arr + i ) , arr + i );
    }

}

【讨论】:

    【解决方案3】:

    希望这会有所帮助。

    #include<stdio.h>
    #include <stdlib.h>
    
      #define N 5
      void function(int *);
      int main()
      {
         int array[N];
         function(&array);
         return 0;
      }
    
    
      void function(int *arr)
      {
          int i, n;
           printf("How many elements? ");
           scanf("%d", &n);
           fflush(stdin);
    
           if (n<=N) {
           for(i=0; i<n; i++)
           {
             printf("Enter element %d: ", i);
             scanf("%d", arr);
             fflush(stdin);
             arr++;
           }
    
        printf("\nInput\t\tValue\tAddress");
        for(i=0; i<n; i++)
        {
          printf("\n%d\t\t%d\t0x%X", i+1, *(arr+i) , arr);
          arr++;
        }
      }
    

    【讨论】:

      【解决方案4】:

      有几个问题:
      1.void function(int *arr[]); => 在此函数中,您声明的是一个指针数组,而不是指向数组的指针。请阅读int (*)arr[]int *arr[],它们的不同之处在于“( )”和“[ ]”的优先顺序。

      2. 如果你定义了一个像int arr[5]; 这样的数组,那么要传递数组的基地址,你必须像function(arr) 这样传递它,所以如果你调用像function(&amp;arr) 这样的函数,那么它没有任何意义。当您声明数组 int arr[5](比如说)时,arr 始终代表该数组的基地址。

      ==>> 接下来要使用指针数组(您正在使用的那个),您需要了解动态内存分配函数,如 malloc()、calloc()、realloc() 和 free()。

      所以我给你的建议是首先阅读一本完全是指针书的书中的指针。我最喜欢的是 Kenneth A. Reek 的“C 上的指针”

      不要依赖网站学习基础知识,买一本好书。使用 StackOverflow 作为解决严重问题的资源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-02
        • 2018-09-18
        • 1970-01-01
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多