![](/img/trans.png)
[英]How to reverse a array of chars and then store the reversed array of char to another array?
[英]How to reverse an array and store the values of the reversed array in a new array
我的程序需要執行以下操作:
niz[] = { 2, 4, 5, 7, 4, 8, 3 }
需要變成niz[] = { 3, 8, 4, 7, 5, 4, 2 }
printf
內的 printf 的情況下完成,該 function 反轉數組並將值存儲到新數組中。 此外,需要預定義數組及其大小(因此無需用戶輸入)。main
中打印但是,不是反轉數組並將其存儲到新數組中並返回它。 該程序始終打印以下數字:
6356668
那么有人在我的代碼中看到了問題嗎?
int koko(int *array, int *array2, int c, int d) {
for (c = 6; c > -1; c--, d++) {
array2[d] = array[c];
}
return array2;
}
int main() {
int niz[] = { 2, 4, 5, 7, 4, 8, 3 };
int niz2[7];
int a, b, c;
c = koko(niz, niz2, a, b);
printf("%d", c);
}
那么有人在我的代碼中看到了問題嗎?
是的:
d
接收一個未初始化的參數c
,因為您在使用它之前從未在 function 的 scope 中初始化它,它會調用未定義的行為。
koko
的返回類型不正確,因為您試圖返回array2
,實際上niz2
,您需要int*
返回類型,而不是int
。
此外,您不需要這樣做,因為作為 function 的參數傳遞的數組會衰減到指向它的第一個元素的指針,即使參數不是明確的指針,在 ZC1C415268E17A94 的 scope 中對其所做的更改也是永久的.
根據您的代碼,您可以執行以下操作:
#include <stdio.h>
void koko(int *array, int *array2, size_t size) // pass the size of the array
{ // size_t more appropriate for object sizes
for (int j = 0; size > 0; size--, j++) // initialize j, you can use size as iterator
{
array2[j] = array[size - 1]; // changes made to array2 are permanent
}
}
int main()
{
int niz[] = {2, 4, 5, 7, 4, 8, 3};
const size_t SIZE = sizeof(niz) / sizeof(niz[0]); // determine the size of the array
int niz2[SIZE]; // same size as non reversed array
koko(niz, niz2, SIZE);
for (size_t i = 0; i < SIZE; i++) // test print the reversed array
printf("%d", niz2[i]);
}
因為你使用新數組作為koko
function的參數,所以不需要返回。 如果要返回它,則應將返回的類型從int
更改為int *
。
在koko
function 中, d
必須始終從0
開始,因此您無需將其聲明為此 function 的參數。 讓我們在這個 function 中將其聲明為局部變量。
c
是for
循環的迭代器之一,所以讓我們在這個 function 中聲明它,初始值等於size - 1
( size
是數組的大小或該數組中的元素數)。
順便說一句,這個 function 變成如下:
void koko(int *array,int *array2,int size)
{
int d = 0;
int c;
for(c = size - 1;c>-1;c--,d++)
{
array2[d]=array[c];
}
}
要在 main 中使用此 function,您只需提供兩個 arrays 和數組大小(在本例中為7
):
koko(niz,niz2,7);
完整的測試代碼:
#include <stdio.h>
void koko(int *array,int *array2,int size)
{
int d = 0;
int c;
for(c = size - 1;c>-1;c--,d++)
{
array2[d]=array[c];
}
}
int main()
{
int niz[]={2, 4, 5, 7, 4, 8, 3};
int niz2[7];
koko(niz,niz2,7);
for(int i = 0; i <7; i++)
printf("%d ",niz2[i]);
}
output:
3 8 4 7 5 4 2
您不需要將 4 arguments 傳遞給koko
,只需 arrays 和元素數量就足夠了。 也不要與-1
比較,將循環寫為在0
處停止的向下循環,這樣您就可以使用無符號索引類型,例如size_t
。 更好地使用索引從 0 到排除length
的經典循環,並將源元素存儲到適當的目標元素。
你得到一個沒有意義的數字 output 的原因是你 printf 的返回值koko
,它被錯誤輸入為int
而你返回一個指向目標數組的指針,無論如何你不能以這種方式打印一個數組,你可以使用循環遍歷數組元素。
這是修改后的版本:
#include <stdio.h>
void koko(const int *array, int *array2, size_t length) {
for (size_t i = 0; i < length; i++) {
array2[length - 1 - i] = array[i];
}
}
int main() {
int niz[] = { 2, 4, 5, 7, 4, 8, 3 };
size_t length = sizeof(niz) / sizeof(niz[0]);
int niz2[length];
koko(niz, niz2, length);
for (size_t i = 0; i < length; i++) {
printf("%d ", niz2[i]);
}
printf("\n");
return 0;
}
function的返回類型不正確。
int koko(int *array,int *array2,int c,int d)
{
for(c=6;c>-1;c--,d++)
{
array2[d]=array[c];
}
return array2;
}
返回的表達式具有int *
類型,而返回類型是int
。
而這個電話
c=koko(niz,niz2,a,b);
沒有意義,因為變量a
和b
未初始化。
也是 function printf
的單次調用
printf("%d", c);
與輸出整個結果數組沒有任何共同之處。
看來您需要的是以下內容。
#include <stdio.h>
#include <stdlib.h>
int * reverse( const int *a, size_t n )
{
int *result = malloc( n * sizeof( int ) );
if ( result != NULL )
{
for ( size_t i = 0; i < n; i++ )
{
result[i] = a[n - i - 1];
}
}
return result;
}
int main(void)
{
int a[] = { 2, 4, 5, 7, 4, 8, 3 };
const size_t N = sizeof( a ) / sizeof( *a );
int *b = reverse( a, N );
if ( b != NULL )
{
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", b[i] );
}
putchar( '\n' );
}
free( b );
return 0;
}
程序 output 是
3 8 4 7 5 4 2
如果你想以相反的順序將一個數組復制到另一個已經存在的數組中,那么對應的 function 可以如下面的演示程序所示。
#include <stdio.h>
void reverse_copy( const int *a, size_t n, int *b )
{
const int *p = a + n;
while ( p-- != a )
{
*b++ = *p;
}
}
int main(void)
{
enum { N = 7 };
int a[N] = { 2, 4, 5, 7, 4, 8, 3 };
int b[N];
reverse_copy( a, N, b );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", b[i] );
}
putchar( '\n' );
return 0;
}
程序 output 將與上圖相同。
3 8 4 7 5 4 2
在 function 中不引入任何額外的變量(實際上是多余的),它可以這樣定義
void reverse_copy( const int *a, size_t n, int *b )
{
while ( n-- )
{
*b++ = a[n];
}
}
正如您在 function 內部看到的,僅使用了它的參數。
順便說一句,遞歸 function 可以如下所示。:)
#include <stdio.h>
void reverse_copy( const int *a, size_t n, int *b )
{
if ( n )
{
*b = a[n-1];
reverse_copy( a, n - 1, b + 1 );
}
}
int main(void)
{
enum { N = 7 };
int a[N] = { 2, 4, 5, 7, 4, 8, 3 };
int b[N];
reverse_copy( a, N, b );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", b[i] );
}
putchar( '\n' );
return 0;
}
它的 output 再次是
3 8 4 7 5 4 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.