C语言 使用指针显示数组内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6814533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:13:47  来源:igfitidea点击:

using pointers to display content of array

cpointers

提问by samprat

I am stuck about how to use pointers to display array. I can easily do this with array using for loop but I am interested in knowing how to use via pointers and I am stuck how to calculate starting and ending point of an array.

我对如何使用指针显示数组感到困惑。我可以使用 for 循环轻松地使用数组执行此操作,但我有兴趣知道如何通过指针使用,并且我被困在如何计算数组的起点和终点。

Below is the sample program

下面是示例程序

 void printArray(int *ptr);            
{         
    //for statement to print values using array             
    for( ptr!=NULL; ptr++) // i know this doesn't work         
    printf("%d", *ptr);        
}         

int main()    
{    
    int array[6] = {2,4,6,8,10};     
    printArray(array);    
    return 0;     
}

采纳答案by Praetorian

The checking for NULL trick only works for NULL terminated strings. For a numeric array you'll have to pass in the size too.

检查 NULL 技巧仅适用于 NULL 终止的字符串。对于数字数组,您也必须传入大小。

void printArray(int *ptr, size_t length);            
{         
    //for statement to print values using array             
    size_t i = 0;
    for( ; i < length; ++i )      
    printf("%d", ptr[i]);        
}   

 void printString(const char *ptr);            
{         
    //for statement to print values using array             
    for( ; *ptr!='
//Marker value.
for(;*ptr != -1; ++ptr)
    printf("%d", *ptr);  

//Length parameter
for(int i = 0; i < length; ++i)
    printf("%d", *(ptr+i));
'; ++ptr) printf("%c", *ptr); } int main() { int array[6] = {2,4,6,8,10}; const char* str = "Hello World!"; printArray(array, 6); printString(str); return 0; }

回答by NPE

You have several options:

您有多种选择:

  • You could pass the size of your array into the function.
  • You could have a special "sentinel" value (e.g. -1) as the last element of your array; if you do this, you must ensure that this value cannot appear as part of the array proper.
  • 您可以将数组的大小传递给函数。
  • 你可以有一个特殊的“哨兵”值(例如-1)作为数组的最后一个元素;如果你这样做,你必须确保这个值不能作为数组的一部分出现。

回答by Anders Abel

When an array is passed as a parameter to a function, it is decayed into a pointer to the first element of the array, loosing the information about the length of the array. To handle the array in the receiving function (printArray) requires a way to know the length of the array. This can be done in two ways:

当数组作为参数传递给函数时,它会衰减为指向数组第一个元素的指针,从而丢失有关数组长度的信息。要在接收函数 ( printArray) 中处理数组,需要知道数组长度的方法。这可以通过两种方式完成:

  • A special termination marker used for the last element. For strings this is NULL. In your example it could be -1, if that value will never occur in the real data.
  • Passing a length parameter to printArray.
  • 用于最后一个元素的特殊终止标记。对于字符串,这是NULL. 在您的示例中-1,如果该值永远不会出现在实际数据中,则可能是。
  • 将长度参数传递给printArray.

This would give the following forstatements:

这将给出以下for语句:

void printArray(int *ptr, size_t size)
{
    int *const end = ptr + size;
    while( ptr < end ) {
        printf("%d", *ptr++);        
    }
}

int main()    
{    
    int array[6] = {2,4,6,8,10};     
    printArray(array, sizeof(array) / sizeof(array[0]) );    
    return 0;     
}

回答by unkulunkulu

The function needs to know the size of the array. There are two common approaches:

该函数需要知道数组的大小。有两种常见的方法:

  1. Pass the actual size to the function, e.g.

    void printArray(int *ptr);            
    {         
        //for statment to print values using array             
        for( *ptr != 0; ptr++) // i know this doesn't work         
        printf("%d", *ptr);        
    }         
    
    int main()    
    {    
        int array[6] = {2,4,6,8,10, NULL};     
        printArray(array);    
        return 0;     
    }
    
  2. Explicitly provide a sentinel 0 (or other appropriate) element for the array:

    void printArray(int *ptr, size_t size)
    {
        int *const end = ptr + size;
        while( ptr < end ) {
            printf("%d", *ptr++);        
        }
    }
    
    int main()    
    {    
        int array[6] = {2,4,6,8,10};     
        printArray(array, sizeof(array) / sizeof(array[0]) );    
        return 0;     
    }
    
  1. 将实际大小传递给函数,例如

    void printArray(int *ptr);            
    {         
        //for statment to print values using array             
        for( *ptr != 0; ptr++) // i know this doesn't work         
        printf("%d", *ptr);        
    }         
    
    int main()    
    {    
        int array[6] = {2,4,6,8,10, NULL};     
        printArray(array);    
        return 0;     
    }
    
  2. 为数组显式提供哨兵 0(或其他适当的)元素:

     void printArray(int arr[]);            
    {         
       int *ptr;
    
       for(ptr = &arr[0]; ptr <= &arr[5]; ptr++)
       {
           if(ptr != null)       
                printf("%d", *ptr);
    
          ptr++;   // incrementing pointer twice, as there are ‘int' values in array which
                   //are of size 2 bytes, so we need to increment it twice..
       }       
    }         
    
     int main()    
    {    
        int array[6] = {2,4,6,8,10};     
        printArray(array);    
        return 0;     
    }
    

回答by Surjit Joshi

Here is the answer buddy (Non-tested)...

这是朋友的答案(未测试)...

##代码##