正如我的標題所暗示的,我是一個初學者,正在玩一些陣列。盡管我盡力而為,但我無法正確更改陣列中的值?如您所見,陣列中只有最后 5 位數字正確,但前 3 位數字不正確?為什么會這樣?我將在下面發布我的代碼,以便你們所有人都能明白我的意思:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ARRAY 8
void input_array(char anumber[MAX_ARRAY])
{
printf("\n\nPlease insert new data to the 1st array with value 5: ");
fgets(&anumber[0], MAX_ARRAY, stdin);
long ret = strtol(&anumber[0], NULL, 10); // Converts char to int
printf("Converting char anumber = %d to int ret = %d\n", anumber[0], ret);
printf("\n(Array after): ");
for (int i = 0; i < MAX_ARRAY; i)
{
printf("(%d) ", anumber[i]);
}
}
int main(void)
{
char arr[MAX_ARRAY] = {5, 8, 2, 9, 1, 7, 4, 3};
printf("(Array before): ");
for (int i = 0; i < MAX_ARRAY; i)
{
printf("(%d) ", arr[i]);
}
input_array(arr); // Function that lets the user change value of "5" inside the array
return 0;
}
如果我作為用戶輸入值“3”,則此代碼的輸出是:
(Array before): (5) (8) (2) (9) (1) (7) (4) (3)
Please insert new data to the 1st array with value 5: 3
Converting char anumber = 51 to int ret = 3
(Array after): (51) (10) (0) (9) (1) (7) (4) (3)
uj5u.com熱心網友回復:
開場白:
線
long ret = strtol(&anumber[0], NULL, 10); // Converts char to int
是錯的。尤其是評論// Converts char to int是錯誤的。該功能strtol將不轉換單個字符到int。相反,它會將字串(即字符序列)轉換為long.
要將單個數字字符轉換為int,您可以改為撰寫以下行:
int number = anumber[0] - '0';
這是可能的,因為ISO C要求的字符集存盤的數字0來9順序。
您的程式中發生的事情實際上如下:
您將陣列的 8 個元素初始化為以下值:
(5) (8) (2) (9) (1) (7) (4) (3)
之后,您使用 輸入"3\n"來自用戶的字串fgets。對應的ASCII碼是51為數字'3'和10為換行符'\n'。用這兩個值覆寫陣列的前兩個元素,用字串的終止空字符覆寫第三個元素。
這就是為什么陣列在呼叫后具有以下值fgets:
(51) (10) (0) (9) (1) (7) (4) (3)
該函式fgets覆寫前 3 個元素,但保留其余元素不變。
解決問題的正確方法如下:
您似乎只想覆寫陣列的第一個元素。因此,您不應將陣列用作fgets函式呼叫的直接目標,因為該函式將始終寫入一個以上的位元組(正確使用該函式時)。
解決問題的一種非常簡單的方法如下:
void overwrite_first_element_with_input( char arr[MAX_ARRAY] )
{
//prompt user for input
printf( "Please enter new data for the first element of the array: " );
//write input to first element
if ( scanf( "%hhd", &arr[0] ) != 1 )
{
printf( "input error!\n" );
exit( EXIT_FAILURE );
}
}
完整的程式看起來像這樣(有一些小的改進):
#include <stdio.h>
#include <stdlib.h>
#define MAX_ARRAY 8
void overwrite_first_element_with_input( char arr[MAX_ARRAY] )
{
//prompt user for input
printf( "Please enter new data for the first element of the array: " );
//write input to first element
if ( scanf( "%hhd", &arr[0] ) != 1 )
{
printf( "input error!\n" );
exit( EXIT_FAILURE );
}
}
void print_array( const char arr[MAX_ARRAY], const char *tag )
{
printf( "%s", tag );
for ( int i = 0; i < MAX_ARRAY; i )
{
printf( "(%d) ", arr[i] );
}
printf( "\n" );
}
int main( void )
{
char arr[MAX_ARRAY] = {5, 8, 2, 9, 1, 7, 4, 3};
print_array( arr, "(Array before): " );
overwrite_first_element_with_input( arr );
print_array( arr, "(Array after): " );
return 0;
}
以下是該程式的一些示例輸出:
(Array before): (5) (8) (2) (9) (1) (7) (4) (3)
Please enter new data for the first element of the array: 20
(Array after): (20) (8) (2) (9) (1) (7) (4) (3)
但是,我不想鼓勵您使用andscanf代替,因為它有很多缺點。如果你想用and解決問題,我會推薦以下代碼:fgetsstrtolscanffgetsstrtol
void overwrite_first_element_with_input ( char arr[MAX_ARRAY] )
{
char line[100], *p;
long ret;
//prompt user for input
printf( "Please enter new data for the first element of the array: " );
//attempt to read one line of input and verify success
if (
//verify that fgets was successful
fgets( line, sizeof line, stdin ) == NULL
||
//verify that input buffer was large enough to store entire line
strchr( line, '\n' ) == NULL
)
{
printf( "input error!\n" );
exit( EXIT_FAILURE );
}
//attempt to convert input to number
ret = strtol( line, &p, 10 );
if ( p == line )
{
printf( "conversion failure!\n" );
exit( EXIT_FAILURE );
}
//write the successfully converted number to the array
arr[0] = ret;
}
請注意,您必須添加#include <string.h>上述代碼才能作業。
uj5u.com熱心網友回復:
你好,我看到你試圖將你的數字陣列保存在 char 中,所以它可能會導致這種錯誤。定義兩個變數用于字符,另一個用于數字。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/366061.html
