簡體   English   中英

C編程:將int *轉換為char *

[英]C Programming: convert int* to char*

我知道我無法將int *轉換為char *,但必須使用sprintf將int *復制到char *數組中。 我的問題是,我相信自己正確使用了sprintf,但收到此警告:警告:從不兼容的指針類型傳遞'sprintf'的參數1。 這是我正在嘗試做的精簡版本。

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 *,但發現那總是返回第一個字符。 我究竟做錯了什么? 有人可以向我解釋為什么會這樣嗎? 抱歉,我是指針的新手,對此我真的很掙扎...

您的代碼很好。 我測試了以下內容,它們給出了正確的結果,並且沒有編譯器警告,

#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*

在您的示例代碼中

ints_to_chars(int * carray

carray應該是char *而不是int *

您的簽名錯誤:

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

嘗試這個 :

    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);

順便說一下,這是使用多進程或多線程查找素數的一種手段。 ^-^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM