2015-05-01 91 views
0

編輯。是否有可能採用第二個函數並將源指針之後的最後一個元素作爲參數指向元素的指針?示例copy_ptrs(target3,source,source +5)指向一個元素?

我包含了一個我的整個程序的副本,但我想要做的是使用兩個函數,一個使用數組表示法,另一個使用指針表示法來簡單地複製初始值數組元素被用戶讀入。我的指針功能不起作用,因爲它不會打印數據的副本。我認爲我的指針聲明是錯誤的?我對指針的瞭解非常有限,但我認爲解決方案非常接近。任何幫助都會很棒。

#include <stdio.h> 
#define MAX 999 

void copy_arr(double ar[], double ar2[], int n); 
void copy_ptr(double arr[], double arr2[], int n); 

int main() 
{ 
    int i, num; 
    double source[MAX]; 
    double target1[MAX]; 
    double target2[MAX]; 


    printf("\nEnter number of elements to be read into the array: "); 
    scanf("%d", &num); 

    printf("\nEnter the values below (press enter after each entry)\n"); 

    for (i = 0; i < num; i++) 
    { 
      scanf("%lf", &source[i]); 
    } 

    //copy_arr(target1, source, num); 
    copy_ptr(target2, source, num); 


    printf("\n\nCopying Complete!\n"); 

    return 0; 
} 

void copy_arr(double target1[], double source[], int num) 
{ 
    int i; 

    for (i = 0; i < num; i++) 
    { 
      target1[i] = source[i]; 
    } 

    printf("\n***The first function uses array notation to copy the elements***\n"); 
    printf("===================================================================\n"); 

    for (i = 0; i < num; i++) 
    { 
     printf("\n    Array_Notation_Copy[%d] = %.2lf", i, target1[i]); 
    } 

} 
void copy_ptr(double target2[], double source[], int num) 
{ 
    int i; 
    double *p, *q; 

    p = source; 
    q = target2; 

    for (i = 0; i < num; i++) 
    { 
      *q = *p; 
     q++; 
     p++; 
    } 
    q = target 2 
    printf("\n\n***The second function uses pointer notation to copy the elements***\n"); 
    printf("===================================================================\n"); 

    for(i = 0; i < num; i++) 
    { 
      printf("\n    Pointer_Notation_Copy[%d] = %.2lf",i, *q++); 
    } 
} 

回答

1

您正在使用q打印陣列target2qfor循環的末尾不再指向它。 printf

q = target2; 

復位指針q到品脫陣列target2之前添加此行。

+0

賓果!感謝所有的幫助! –

1

你確實非常接近。

您剛剛在打印之前忘了重設qtarget2;所以在打印時,你試圖顯示超出數組末尾的元素。

對於你的問題的「編輯」部分,你可以循環是這樣的:

for (double *ptr = source ; ptr != pointer_after_last ; ++ptr) 

利用這一點,你將不得不ptr指向到其他各個之後的要素之一。如果您想跟蹤物品編號,只需添加一個在循環的每次運行中增加的變量。

您程序的其餘部分將完全相同。

+0

你能看看我再次遇到的問題嗎?我需要創建另一個功能,但完全卡住了。 –

0

在第二個函數中(在更新不應該完成的帖子之前,否則將會混淆),當數組被填充時,您正嘗試使用之前循環中已更改的指針輸出目標數組的元素在任何情況下在源陣列

for(i = 0; i < num; i++) 
{ 
     printf("\n    Pointer_Notation_Copy[%d] = %.2lf",i, *q++); 
                     ^^^^^ 
} 

然而的元件的功能是不正確rekative到分配的描述。您不應該使用輔助計數變量的循環。

正確的功能,可以像

void copy_ptr(double target2[], const double source[], int num) 
{ 
    const double *p = source; 
    double *q = target2; 

    for (; p != source + num; ++p, ++q) 
    { 
      *q = *p; 
    } 

    printf("\n\n***The second function uses pointer notation to copy the elements***\n"); 
    printf("===================================================================\n"); 

    int i = 0; 
    for (q = target2; q != target2 + num; ++q) 
    { 
      printf("\n    Pointer_Notation_Copy[%d] = %.2lf",i++, *q); 
    } 


} 
+0

由於'i'現在未定義,因此缺少打印元素編號 – Gianluca

+0

@Gianluca謝謝。我甚至沒有看到印刷品。:) –

相關問題