2013-09-25 137 views
0

我知道我無法將int *轉換爲char *,但必須使用sprintf將int *複製到char *數組中。我的問題是,我相信我正確使用sprintf,但我收到此警告: 警告:從不兼容的指針類型傳遞參數1'sprintf'。下面是我正在嘗試做的一個精簡的簡化版本。C編程:將int *轉換爲char *

int* iarray = calloc(top + 1, sizeof(int)); 
char* carray = (char*)calloc(count,sizeof(char)); 
/* 
some code that adds stuff to the int*, it works fine....something like 
for(i=0; i<=count; i++) 
    iarray[i] = i; 
lets assume that iarray is something like 1234 
*/ 
for(i=0; i<=count; i++) 
{ 
    sprintf(carray, "%d", iarray[i]); 
    printf("%s", carray) //this is just to test, it only prints out the first number... 
} 

這一直讓我瘋狂......如果你想要更多我的代碼,請讓我知道。

謝謝!

編輯:這裏是更多的代碼。我跳過了基本的變量聲明,我可以添加那些,但我向你保證,所有這些都很好

這裏是做轉換功能:

void ints_to_chars(char* carray, int count, int* iarray) 
{ 
    //convert ints to char* 
    int i; 
    //char *char_array=(char*)calloc(count,sizeof(char)); 
    printf("ints to chars: char array-->"); 
    for(i=0; i<=count; i++) 
    { 
     sprintf(carray, "%d", iarray[i]); 
     printf("%s",carray); 
    } 
    return; 
} 

int main(int argc, char** argv) 
{ 
int i, j, count; 

if(i-1 == 0) 
        bottom = 2; 
       else 
        bottom = (int)atoi(argv[i-1])+1; 
       top = (int)atoi(argv[i]); 
       printf("child %d: bottom=%d, top=%d\n", getpid(), bottom, top); 

       prime_iarray = calloc(top + 1, sizeof(int)); 
       primenumbers(prime_iarray,bottom,top); 
       count = prime_iarray[0]; 

       /*printf("prime int array: \n"); 
       for(i=1; i<=count; i++) 
       { 
        printf("%d", prime_iarray[i]); 
       }*/ 

       //int* into char* 
       prime_carray = (char*)calloc(count,sizeof(char)); 
       ints_to_chars(prime_carray, count, prime_iarray); 
       printf("prime char array:\n"); 
       printf("%s",&prime_carray[0]); 

} 

編輯2:所以有人親切地指出了一個愚蠢的錯誤消除了我得到的警告。但是我的char *只打印出第一個字符....我最初是返回一個本地char *,但發現它總是隻返回第一個字符。我究竟做錯了什麼?有人可以向我解釋爲什麼會發生這種情況嗎?對不起,我是新來的指針,我真的很努力...

+2

向我們顯示您的真實代碼。 –

+2

顯示的代碼不會對警告負責。 –

+0

'top'和'count'是什麼?你想要char-array包含什麼? int的字符串表示形式還是int形式爲ASCII代碼的字符? –

回答

1

你的代碼是好的。我測試了其給出正確的結果,並沒有編譯器警告以下,

#include "stdio.h" 
#include "stdlib.h" 
int main() 
{ 

     int count = 20; 
     int top = 10; 
     int* iarray = calloc(top + 1, sizeof(int)); 
     char* carray = (char*)calloc(count,sizeof(char)); 
     int i; 
     int n; 

     iarray[0] = 9; 
     for(i=0; i<=count; i++) 
     { 
         n = sprintf(carray, "%d", iarray[i]); 
         printf("%s", carray); //this is just to test, it only prints out the first number... 
     } 
} 

在你ints_chars功能,你爲什麼聲明carrayint*

1

在您的示例代碼,你有

ints_to_chars(INT * CARRAY

CARRAY應該是一個char *不是INT *

+0

我的天啊......我簡直不敢相信。感謝您指出我愚蠢的錯誤.... – user1202994

1

你的簽名錯誤:

void ints_to_chars(int* carray, int count, int* iarray) 
        ^^ this should be char* 
0

試試這個:

char a[100]; 
    char b[4]; 
    a[0] = '\0'; 
    int i; 

    for(i=0; i<=count; i++) 
    { 

      sprintf(b ,"%d" , iarray[i]); 
      strcat(a , b); 
      strcat(a , " ");   

    } 
printf("%s", a); 

順便說一句,這是一個使用多進程或多線程來查找素數的分配。^-^