【问题标题】:C Programming: convert int* to char*C 编程:将 int* 转换为 char*
【发布时间】:2013-10-01 03:14:32
【问题描述】:

我知道我无法将 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* 但发现它总是只返回第一个字符。我究竟做错了什么?有人可以向我解释为什么会这样吗?对不起,我是指针新手,我真的很挣扎......

【问题讨论】:

  • 向我们展示您的真实代码。
  • 显示的代码看起来不对警告负责。
  • 什么是topcount?你希望 char 数组包含什么? int 的字符串表示形式还是具有 int ASCII 码的字符?
  • 在您的 ints_chars 函数中,为什么将 carray 声明为 int*?
  • 我希望 char* 具有 int* 的字符串表示形式。所以如果 int* 是 1234,我希望 char* 也是“1234”

标签: c pointers char int


【解决方案1】:

在您的示例代码中

ints_to_chars(int* 数组

carray 应该是 char* 而不是 int*

【讨论】:

  • 天哪……我简直不敢相信。感谢您指出我的愚蠢错误....
【解决方案2】:

你的签名有误:

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

【讨论】:

    【解决方案3】:

    你的代码很好。我测试了以下给出正确的结果并且没有编译器警告,

    #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 函数中,为什么将carray 声明为int*

    【讨论】:

      【解决方案4】:

      试试这个:

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

      顺便说一下,这是一个使用多进程或多线程查找素数的分配。 ^-^

      【讨论】:

        猜你喜欢
        • 2012-06-01
        • 2020-04-15
        • 2015-12-18
        • 2015-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多