I need to convert Integer to Char, I can use only pointers without array indexes. Char array must be dynamically allocated. Can anybody review my code and tell my what am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
int main(){
int myNumber = 1234;
char *myString = (char*)malloc(2 * sizeof(char)); //memory for 1 char and '\0'
int i = 0; //parameter for tracking how far my pointer is from the beggining
if (myNumber < 0){
*myString = '-'; //if myNumber is negative put '-' in array
*myString = *(myString + 1); //move pointer to next position
i++;
}
while (myNumber != 0){
myString = (char*)realloc(myString, (i + 2) * sizeof(char)); //increse memory =+1
*myString = (myNumber % 10) + '0'; //put digit as char to array
myNumber /= 10;
*myString = *(myString + 1); //move pointer to next position
i++;
}
*myString = '\0'; //mark end of string
*myString = *(myString - i); //move pointer back to the beggining of string
printf("\n%s", *myString); // print char array (not working..)
return 0;
}
I know there are better ways of converting Int to String (sprintf), but my task is to do it that way. In the code above I`m taking the digits from Int backwards, can it be done in correct order?
edit. as mentioned in the comments the part:
*myString = *(myString + 1);
is wrong, correct way of redirecting pointer by one space is:
myString++;
same with:
*myString = *(myString - i); //wrong
myString -=i; //ok
Edit2: Now my code works! But I need to think how to correct the order of the digits.
#include <stdio.h>
#include <stdlib.h>
int main(){
int myNumber = 1234;
char *myString = (char*)malloc(2 * sizeof(char)); //memory for 1 char and '\0'
char * position = myString;
int i = 0;
if (myNumber < 0){
*position = '-'; //if myNumber is negative put '-' in array
position += i; //move pointer to next position
i++;
}
while (myNumber != 0){
myString = (char*)realloc(myString, ((i + 2) * sizeof(char))); //increse memory =+1
position = myString + i; // getting current position after reallocating
*position = (myNumber % 10) + '0'; //put digit to array
myNumber /= 10;
position++; //move pointer to next position
i++;
}
*position = '\0'; //mark end of string
char * temp = myString;
while (*temp != '\0'){
printf("%c", *temp); // print char array (not working..)
temp++;
}
return 0;
}
Edit 3 Problem solved, thanks for comments, I`m posting code in case anybody will look for similar solution.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// move each character in array one place to the right
// we need to make place for new character on the left
void moveArrayElementsRight(char *ptr, int len) {
for (int j = len; j > 1; j--) {
*(ptr + j - 1) = *(ptr + j - 2);
}
}
void intToStr(int myNumber, char* myString){
int i = 1; //track size of allocated memory
bool isMinus = false;
if (myNumber < 0) {
myNumber *= -1; //without this (myNumber % 10) + '0' wont work
isMinus = true;
}
if (myNumber == 0){ //special case for 0
myString = (char*)realloc(myString, ((i + 1) * sizeof(char)));
*myString = '0';
*(myString + 1) = '\0';
}
while (myNumber != 0) {
myString = (char*)realloc(myString, ((i + 1) * sizeof(char))); //increse memory =+1 for next digit
i++;
moveArrayElementsRight(myString, i);
*myString = (myNumber % 10) + '0'; //put digit to array
myNumber /= 10;
}
if (isMinus) {
myString = (char*)realloc(myString, ((i + 1) * sizeof(char))); //increse memory =+1 for '-' sign
i++;
moveArrayElementsRight(myString, i);
*myString = '-'; //put sign at the beginning
}
}
int main() {
int numberToConvert = -10;
char *numberAsString = (char*)malloc(sizeof(char)); //create empty array, with place only for '\0'
*numberAsString = '\0'; //mark the end of array
intToStr(numberToConvert, numberAsString);
printf("%s", numberAsString);
return 0;
}
SPOILER: Don't read or copy this if you don't want the solution.
The following is an example of implementation, using recursive: