2014-11-23 101 views
-1

以下只是一個代碼的一部分。
t是測試用例編號,然後我有每個t的整數n。
我想打破整數數字和存儲在數組中,然後打印數組的每個元素。C - 爲什麼arr [0]顯示爲32767

輸入

1 
45 

預期輸出

5 
4 

實際輸出

32767 
0 

代碼

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

int main() { 
int t,n,n1,tmp,in,len,j; 
scanf("%d",&t); 
while(t--) 
    { 
    scanf("%d",&n); 
    int arr[]={}; 
    n1=n; 
    in=0; 
    len=0; 
    while(n1>0) 
     { 
     tmp=n1%10; 
     arr[in]=tmp; 
     len++; 
     n1=n1/10; 
     in++; 
     } 
    for(j=0;j<len;j++) 
     { 
     printf("%d\n",arr[j]); 
     } 
    } 
} 
+4

'int arr [] = {};'這裏,你認爲'arr'的大小是多少? – 2014-11-23 06:41:20

+0

我不知道,它取決於輸入,如果它是12345,那麼大小將會是5. – 2014-11-23 06:45:54

+7

但是C不能這樣工作。數組不會根據需要增長,'int arr [] = {};'是非法的。 – 2014-11-23 06:54:25

回答

1

問題在於你的int arr[]={};的定義,它創建了一個沒有存儲空間的空數組。除非動態分配,否則最好總是定義最大數組大小。解決這個問題(以及初始化所有值)會解決這個問題。

以下是糾正問題的一種方法。它定義了128的數組元素MAXVALUES的最大數量。它也增加了提示定向用戶數據請求,防止尾隨newline從第一次使用的scanf被讀取作爲'n'輸入:

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

#define MAXVALUES 128 

int main() { 

    int t = 0; 
    int n = 0; 
    int n1 = 0; 
    int tmp = 0; 
    int in = 0; 
    int len = 0; 
    int j = 0; 

    printf ("\n Enter the number of numbers to convert: "); 
    scanf ("%d%*c", &t); 

    while (t--) { 

     printf ("\n Enter the number 'n' : "); 
     scanf ("%d%*c", &n); 

     int arr[MAXVALUES] = {0}; 

     in = 0; 
     len = 0; 
     n1 = n; 

     while (n1 > 0) { 
      tmp = n1 % 10; 
      arr[in] = tmp; 
      len++; 
      n1 = n1/10; 
      in++; 
     } 

     for (j = 0; j < len; j++) { 
      printf ("%d\n", arr[j]); 
     } 
    } 

    return 0; 
} 

輸出:

$ ./bin/arrayval 

Enter the number of numbers to convert: 2 

Enter the number 'n' : 12345 
5 
4 
3 
2 
1 

Enter the number 'n' : 56789 
9 
8 
7 
6 
5 

根據中的數字動態分配

您可以動態分配arr,防止發生#define分配比需要更多的空間(這有點像這裏用大錘撲打一隻蒼蠅)。它只需要更多的工作。具體來說,在分配arr之前,需要知道n中有多少個數字,以便您可以分配不超過所需的內存。這裏,n中的位數由功能szitoa計算,然後分配arr。這是該類型解決方案的一種方法:

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

/* determine number of chars required for string of int i, 
does NOT include space for null char (like strlen) */ 
size_t 
szitoa (int val) 
{ 
    int it = 0; 
    int sz = (val > 0) ? 0 : 1; /* provide space of '-' */ 
    val = (val > 0) ? val : -val; /* absolute value */ 

    for (it = 1; it < INT_MAX; it*=10) { 
     sz++; 
     if (val >= it && val < (it*10)) 
      break; 
    } 

    return sz; 
} 

int main() { 

    int t = 0; 
    int n = 0; 
    int n1 = 0; 
    int tmp = 0; 
    int in = 0; 
    int len = 0; 
    int j = 0; 

    printf ("\n Enter the number of numbers to covert: "); 
    scanf ("%d%*c", &t); 

    while (t--) { 

     printf ("\n Enter the number 'n' : "); 
     scanf ("%d%*c", &n); 

     /* dynamically allocate arr and validate */ 
     int *arr = calloc (szitoa (n), sizeof (int)); 
     if (!arr) { 
      fprintf (stderr, "error: arr allocation failed\n"); 
      exit (EXIT_FAILURE); 
     } 

     in = 0; 
     len = 0; 
     n1 = n; 

     while (n1 > 0) { 
      tmp = n1 % 10; 
      arr[in] = tmp; 
      len++; 
      n1 = n1/10; 
      in++; 
     } 

     for (j = 0; j < len; j++) { 
      printf ("%d\n", arr[j]); 
     } 

     if (arr) free (arr);   /* free memory allocated to arr */ 
    } 

    return 0; 
} 
+1

'int arr [] = {};'在標準C中是非法的;大括號包含的初始化器列表必須至少包含一個初始化器。 (gcc有一個擴展,它聲明瞭一個零大小的數組,雖然我很努力地看到這個擴展的好處) – 2014-11-23 07:16:10

+0

是的,它會工作。但是arr [128]將佔用128個元素的空間,但我實際上只使用了2個。我可能使用5或7或10,所以我還有其他任何方法來避免爲這麼多未使用的數組元素佔用空間。我在某處使用malloc或其他東西閱讀。 – 2014-11-23 07:18:41

+1

@MattMcNabb它完全讓我的頭腦發現'gcc'沒有警告地接受它:'gcc -Wall -Wextra -o bin/arrayval arrayval.c'試試吧。 – 2014-11-23 07:18:43

0

這應該爲你工作:

#include <stdio.h> 

int main() { 

    int number, numberCount, tmp, count; 

    printf("Please enter a number:\n>"); 
    scanf("%d", &number); 

    tmp = number; 

    for(numberCount = 0; tmp > 0; numberCount++) 
     tmp /= 10; 

    int numbers[numberCount]; 

    for(count = (numberCount-1); number > 0; count--) { 
     numbers[count] = number % 10; 
     number /= 10; 
    } 

    for(count = 0; count < numberCount; count++) 
     printf("%d digit is: %d\n", count+1, numbers[count]); 


    return 0; 

} 

輸入:

45 

輸出:

1 digit is: 4 
2 digit is: 5 
+0

謝謝,但正如我已經提到這只是我的代碼的一部分。我想將數值4和5放在數組中,所以我需要在我的代碼中進行改進,而不是完全不同的新代碼。希望你能理解。 :) – 2014-11-23 07:04:19

+0

@ user3388005你的數組數值爲4和5!如果仍然不能使用它,也許你可以使用或看到這個概念:D – Rizier123 2014-11-23 07:05:30

+0

但是你使用了forloop 2次,但我使用whileloop 1次。我不想增加時間複雜性。 – 2014-11-23 07:13:43

0

您正在嘗試寫更多的整數數組的比您爲分配的空間。這裏該代碼:

while(n1>0) 
{ 
    tmp=n1%10; 
    arr[in]=tmp; // You initialize the array space to in, which is ambiguous. 
    len++; 
    n1=n1/10; 
    in++; // You increment the array after already initializing space which you can't do in this manner. 

} 

如果您希望在擴大的基礎上,從它也是曖昧的用戶輸入的數量的陣列來完成 - 你可能會考慮使用動態內存。

+0

好的,實際上我基本上是PERL編碼器,並試圖切換到C.在PERL中,數組處理非常簡單。你能否提供使用動態內存的例子。任何鏈接或教程。 – 2014-11-23 07:08:15

+0

正如你可能已經知道的那樣,你可以使用@DavidC.Rankins的迴應來理解如何在你的特定情況下使用calloc。你也可以在這裏找到更多的資料 - [動態內存] http://www.programiz.com/c-programming/c-dynamic-memory-allocation 這將幫助你瞭解如何增加陣列的大小'來獲得保存整型數據類型所需的存儲量。 – KillaBytes 2014-11-23 18:48:01