0

I understand that I cannot convert an int* into a char* but must copy the int* into a char* array using sprintf. My issue is that I believe I am using sprintf correctly but I am getting this warning: warning: passing argument 1 of ‘sprintf’ from incompatible pointer type. Here is a condensed and simplified version of what I am trying to do.

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...
}

This has been driving me nuts... If you want more of my code then please let me know.

Thanks!

EDIT: here is more code. I skipped the basic variable declarations, I can add those in but I assure you all those are fine

Here is a function to do the conversion:

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

}

EDIT 2: So someone kindly pointed out a silly error that removed the warning I was getting. But my char* only prints out the first character.... I initially was returning a local char* but found out that that will always only return the first character. What am I doing wrong? Can someone explain to me why this is happening? I'm sorry, I'm new to pointers and I'm really struggling with this...

5
  • 2
    show us your real code. Commented Sep 25, 2013 at 13:38
  • 2
    Shown code does not looks responsible for Warning. Commented Sep 25, 2013 at 13:39
  • What is top and count? What do you want the char-array to contain? The String-representation of the int's or the characters which have the int as ASCII-code? Commented Sep 25, 2013 at 13:41
  • In your ints_chars function, why do you declare carray to be int*? Commented Sep 25, 2013 at 13:53
  • I want char* to have the string representation of the int*. so if int* is 1234, I want char* to also be "1234" Commented Sep 25, 2013 at 13:55

4 Answers 4

1

In your example code you have

ints_to_chars(int* carray

carray should be a char* not a int*

Sign up to request clarification or add additional context in comments.

1 Comment

Oh my god...... I can't believe this. Thanks for pointing out my silly mistake....
1

You have the wrong signature:

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

Comments

1

you code is fine. I tested the following which gives the correct result and no compiler warnings,

#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...
        }
}

In your ints_chars function, why do you declare carray to be int*?

Comments

0

try this :

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

by the way, this is an assigment to find prime numbers using multi-process or multi-threaded. ^-^

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.