Converting Int to a char* pointer

Eddy Y

I'm trying to convert an integer to a char* pointer so I can properly send the argument into another function. Is there anyway to do this without atoi?

int number=2123, number2= 1233; 
char* arg[];

arg[0] = number;
arg[1] = number2;

**Sorry for not making things clear so basically I want the arg[0] to equal the string rep of the number, so that i can send it into a function like this: converted(char* arg);

i would change the parameter data type but it has to be that pointer to send in.

Fabel

If you assume that a pointer is always at least the same size as an integer (are there any exceptions to this?) you can safely convert integers to pointers and back with these macros:

#define INT2POINTER(a) ((char*)(intptr_t)(a))
#define POINTER2INT(a) ((int)(intptr_t)(a))

Or if that other function isn't your function, but a function that wants the integers as string you could use asprintf() like this:

asprintf(&arg[0],"%d",number);
asprintf(&arg[1],"%d",number2);

But asprintf is not posix standard so it might not be available on all systems. You could use sprintf() (or snprintf() so be on the safe side) instead, but then you need to calculate the string length first.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive