Converting Integer to dynamically allocated Char array, digit by digit using pointers - c

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:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <time.h>
#include <errno.h>
static size_t int_to_str_size(int n, size_t acc, int base_size) {
int next = n / base_size;
if (next != 0) {
return int_to_str_size(next, acc + 1, base_size);
} else {
return n < 0 ? acc + 2 : acc + 1;
}
}
static void int_to_str_write(int n, char *str, char *base, int base_size) {
*str = base[abs(n % base_size)];
int next = n / base_size;
if (next != 0) {
int_to_str_write(next, str - 1, base, base_size);
} else {
if (n < 0) {
*(str - 1) = '-';
}
}
}
static char *int_to_str(int n, char *base) {
size_t base_size = strlen(base);
if (base_size < 2 || base_size > INT_MAX) {
errno = EINVAL;
return NULL;
}
size_t size = int_to_str_size(n, 0, (int)base_size);
char *str = malloc(size + 1);
if (str == NULL) {
return NULL;
}
str[size] = '\0';
int_to_str_write(n, str + size - 1, base, (int)base_size);
return str;
}
int main(void) {
srand((unsigned int)time(NULL));
for (uintmax_t i = 0; i < 42; i++) {
int n = rand() % 2 ? rand() : -rand();
char *str = int_to_str(n, "0123456789");
if (str != NULL) {
printf("%d == %s\n", n, str);
free(str);
} else {
perror("int_to_str()");
}
}
}

Related

Inserting a character into a char array

I have a char array containing a number.
char number[] = "12000000"
I need to have a function to insert a divider in every 3 digits. Like:
char result[] = "12,000,000"
My function accepts the number as a char pointer and it needs to return result as a char pointer too.
char* insert_divider(char* number) {
some magic;
return result;
}
I have no idea of working with pointers. Thanks.
Here you have a function that adds char c every num characters starting from the end. You need to make sure that the string buffer is long enough to accommodate the amended string.
char *addEvery(char *str, char c, unsigned num)
{
char *end = str;
if(str && *str && num)
{
size_t count = 1;
while(*(end)) end++;
while(end != str)
{
end--;
count++;
if(!(count % (num + 1)) && str != end)
{
memmove(end + 1, end, count);
*end = c;
count++;
}
}
}
return str;
}
int main(void)
{
char str[100] = "120000000000";
printf("%s", addEvery(str,',',3));
}
I came up with this piece of code:
char *result;
result = (char*) malloc(15);
int len= strlen(input);
uint8_t cursor= 0;
for(int i = 0; i < len; i++) {
if ((len- i) > 0 && (len- i) % 3 == 0) {
result[i + cursor] = ',';
cursor++;
}
result[i + cursor] = input[i];
}
result[len+ cursor] = '\0';
Thanks everyone for help and advice.
Here is another way to do it:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* insert_divider(char* number, size_t length) {
int j = length + length/3; // every 3 digits a ',' will be inserted
char *out = (char*)malloc(j + 1);
out[j--] = '\0';
for (int i = length - 1, k = 1; i >= 0; i--, k++) {
out[j--] = number[i];
if ((k%3) == 0) {
out[j--] = ',';
}
}
return out;
}
int main(){
char number[] = "12000000";
char *outNumber = insert_divider(number, strlen(number));
printf("%s", outNumber);
free(outNumber);
return 0;
}

How to modify a char pointer passed as parameter in a function?

So, I am doing my own rudimentary version of itoa(), and I just realized I don't exactly know how to modify a char* passed as parameter, or if there is something wrong with the way I am doing it...
The way I am doing it is by doing malloc() onto the buffer passed as argument, then write the number into it. This is apparently working before returning from the function (the buffer is printed correctly), but then when trying to print it back in main(), it segfaults.
If I understand the error correctly, I am changing the address buff points to inside with that malloc(), and then modify its contents, but the new malloc'd address inside is not returned. How could I do that without changing the parameters or the return value?
int itoa(int i, char *buff) {
int length = 0;
// get the length
long temp = 1;
while (temp <= i) {
length++;
temp *= 10;
}
buff = malloc(length + 1); // buff will have 'length' chars + one extra (\0)
int j = 0;
do { /* generate digits in reverse order */
buff[j++] = i % 10 + '0'; /* get next digit */
} while ((i /= 10) > 0); /* delete it */
buff[length] = '\0';
// reverse it
int k, l;
char c;
for (k = 0, l = length - 1; k<l; k++, l--) {
c = buff[k];
buff[k] = buff[l];
buff[l] = c;
}
printf("buff's now:%s\n", buff);
return 0;
}
int main() {
char *buff = NULL;
itoa(45, buff);
printf("%s\n", buff);
}
Your pointer isn't modified as it was copied. You can read more here. You can try this code after reading the above link.
#include <stdlib.h>
#include <stdio.h>
int itoa_(int i, char **parabuff)
{
int length = 0;
// get the length
long temp = 1;
while (temp <= i)
{
length++;
temp *= 10;
}
char *buff = malloc(length + 1); // buff will have 'length' chars + one extra (\0)
int j = 0;
do
{ /* generate digits in reverse order */
buff[j++] = i % 10 + '0'; /* get next digit */
} while ((i /= 10) > 0); /* delete it */
buff[length] = '\0';
// reverse it
int k, l;
char c;
for (k = 0, l = length - 1; k < l; k++, l--)
{
c = buff[k];
buff[k] = buff[l];
buff[l] = c;
}
printf("buff's now: %s\n", buff);
*parabuff = buff;
return 0;
}
int main()
{
char *buff = NULL;
itoa_(45, &buff);
printf("buff in main: %s\n", buff);
}
//OUTPUT
buff's now: 45
buff in main: 45

C - invalid read of size 1

ok that's it, it's been more than 15 hours and i still can't figure out what's going on !
Here is a code for timeconversion problem in hackerrank, a function takes a string (time in 12--hour AM/PM format) and converts it to military (24-hour) time (returning a string)
the problem is exactly in the function char* timeConversion(char* s)
in this line of code
b = strcmp(ampm,"PM");
it always gives me error which i can not understand
"ERROR: invalid read of size 1"
can anyone help me ?!
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
/*
* Complete the timeConversion function below.
*/
/*
* Please either make the string static or allocate on the heap. For example,
* static char str[] = "hello world";
* return str;
*
* OR
*
* char* str = "hello world";
* return str;
*
*/
/* Swaps strings by swapping pointers */
void swap(char **str1_ptr, char **str2_ptr)
{
char *temp = *str1_ptr;
*str1_ptr = *str2_ptr;
*str2_ptr = temp;
}
void reverse(char str[], int length)
{
int start = 0;
int end = length -1;
while (start < end)
{
swap(*(str+start), *(str+end));
start++;
end--;
}
}
// Implementation of itoa()
char* itoa(int num, char* str, int base)
{
int i = 0;
bool isNegative = false;
/* Handle 0 explicitely, otherwise empty string is printed for 0 */
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}
// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
if (num < 0 && base == 10)
{
isNegative = true;
num = -num;
}
// Process individual digits
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
// If number is negative, append '-'
if (isNegative)
str[i++] = '-';
str[i] = '\0'; // Append string terminator
// Reverse the string
reverse(str, i);
return str;
}
char* timeConversion(char* s) {
/*
* Write your code here.
*/
char *result = (char*)calloc(8,sizeof(char)) ;
char *ampm = (char*)calloc(2,sizeof(char)) ;
char *hh = (char*)calloc(2,sizeof(char)) ;
int a = 0, b = 0 ,c = 0,i;
long int dec = 0;
int len = strlen(s);
// substring hh:mm:ssAM
while ( c < 2) // 2 : LENGTH
{
ampm[c] = s[9+c-1]; // 9 : position
hh[c] = s[1+c-1]; // 1 : position
c++ ;
}
// string to int
//len = strlen(ampm);
for(i = 0; i < 2 ; i++)
{
dec = dec * 10 + (hh[i] - '0');
}
b = strcmp(ampm,"PM");
a = strcmp(ampm,"AM");
printf("%d\n",a);
printf("%d\n",b);
// processing
if (!strcmp(ampm,"AM") && dec==12) dec = 0;
if (!strcmp(ampm,"PM") && dec!=12) dec += 12;
//if (strcmp(s[9],'A') && dec==12) dec = 0;
//if (strcmp(s[9],'P') && dec!=12) dec += 12;
// convert int back to string
char* hhh = itoa(dec, hh, 10);
//dec = atol(hh);
// hh = itoa(dec,10);
// snprintf(result,9,"%d", dec);
//printf("%s\n",hh);
c = 0;
char* sub;
while (c < 9)
{
sub[c] = s[3+c-1];
c++ ;
}
strcat(result,hhh);
strcat(result,sub);
return result;
}
int main()
{
char *s = "07:05:45PM";
char* result = timeConversion(s);
printf("%s\n", result);
return 0;
}
Like the commenters mentioned, it seems like you are missing the NULL-termination, e.g.:
char *ampm = (char*)calloc(2,sizeof(char)) ;
Two characters ('am'/'pm') plus the NULL-Termination would be 3 characters, not 2.
You have to make sure that all your strings have a size of len + 1 and are properly '\0' terminated.
There are 4 issues in your code.
You are not allocating memory to NULL char for apmp
char *ampm = (char*)calloc(3,sizeof(char)) ;
You are receiving double pointer for swap function and passing char value
void swap(char **str1_ptr, char **str2_ptr)
should be
void swap(char *str1_ptr, char *str2_ptr)
and you call swap function like below
swap((str+start), (str+end));
You are not allocating memory to sub pointer
char* sub = malloc (9 * sizeof(char));
You are not deallocating memory for hh, sub and ampm.
free (hh); free(ampm); free(sub);

How to append null terminator to end of the indexed char pointer

I ran the following code and it crashes with the while loop running forever. When I debugged this code, I found the problem at *(pointer+cnt)='\0'; the null character is never there. I don't know how to append the null terminator here so that the program doesn't crash.
#include <stdio.h>
#include <stdlib.h>
char* decimal_binary(int);
int main()
{
int n;
char *ptr=NULL;
printf("Enter the number\n");
scanf("%d",&n);
ptr=decimal_binary(n);
//printing out the characters
while(ptr!='\0')
{
printf("%c",*ptr);
ptr++;
}
free(ptr);
return 0;
}
char* decimal_binary(int n)
{
int c,d,cnt=0;
char *pointer=(char*)malloc(8+1);
if(pointer==NULL)
exit(EXIT_FAILURE);
for(c=7;c>=0;c--)
{
d=n>>c;
if(d&1)
*(pointer+cnt)=1+'0';
else
*(pointer+cnt)=0+'0';
cnt++;
}
//Null not getting added at the end of this sequence.Hence while loop in main runs forever.
*(pointer+cnt)='\0';
return pointer;
}
You've chosen to write:
while(ptr!='\0')
That's a funny way of writing:
while (ptr != 0)
or:
while (ptr != NULL)
where you intended to write:
while (*ptr != '\0')
The conventional way of writing *(pointer+cnt) is pointer[cnt].
You can't afford to free the incremented pointer; you must free what was returned by malloc() — or calloc() or realloc() or …
Preserve a copy of the value returned by binary_decimal() and free the copy (or increment the copy and free the value in ptr).
You can use either of the two binary_decimal() functions in the code below:
#include <stdio.h>
#include <stdlib.h>
char *decimal_binary(int);
int main(void)
{
int n;
char *ptr = NULL;
printf("Enter the number\n");
scanf("%d", &n);
ptr = decimal_binary(n);
char *cpy = ptr;
//printing out the characters
while (*ptr != '\0')
{
printf("%c", *ptr);
ptr++;
}
putchar('\n');
free(cpy);
return 0;
}
char *decimal_binary(int n)
{
int cnt = 0;
char *pointer = (char *)malloc(8 + 1);
if (pointer == NULL)
exit(EXIT_FAILURE);
for (int c = 7; c >= 0; c--)
{
int d = n >> c;
if (d & 1)
pointer[cnt] = 1 + '0';
else
pointer[cnt] = 0 + '0';
cnt++;
}
pointer[cnt] = '\0';
return pointer;
}
Or:
char *decimal_binary(int n)
{
int cnt = 0;
char *pointer = (char *)malloc(8 + 1);
if (pointer == NULL)
exit(EXIT_FAILURE);
for (int c = 7; c >= 0; c--)
pointer[cnt++] = ((n >> c) & 1) + '0';
pointer[cnt] = '\0';
return pointer;
}
This could be compressed still more (and even less readably):
char *decimal_binary(int n)
{
char *pointer = (char *)malloc(8 + 1);
if (pointer == NULL)
exit(EXIT_FAILURE);
for (int c = 7; c >= 0; c--)
pointer[7 - c] = ((n >> c) & 1) + '0';
pointer[8] = '\0';
return pointer;
}
And for a buffer of 9 bytes, you could perfectly well allocate a local variable in main() and pass the address to decimal_binary() so it does not need to use malloc() and the main() does not need to use free:
#include <stdio.h>
#include <stdlib.h>
void decimal_binary(int, char *);
int main(void)
{
int n;
char buffer[9];
char *ptr = buffer;
printf("Enter the number\n");
scanf("%d", &n);
decimal_binary(n, buffer);
while (*ptr != '\0')
{
printf("%c", *ptr);
ptr++;
}
putchar('\n');
return 0;
}
void decimal_binary(int n, char *pointer)
{
for (int c = 7; c >= 0; c--)
pointer[7 - c] = ((n >> c) & 1) + '0';
pointer[8] = '\0';
}

Big number adding function crashes program

I wrote an adding function for very large numbers and when it gets called, the program crashes. I am assuming that it has to do with the carrying. Here is the code:
char * bigadd(char a[], char b[]){
int i, temp;
char useadd[MAX];
char usea = strrev(a);
char useb = strrev(b);
char ret[strlen(useadd)];
char *pa, *pb, *padd;
padd = &useadd;
pa = &usea;
pb = &useb;
while(*pa != '\0' && *pb != '\0'){
if(atoi(*pa) + atoi(*pb) + temp > 9){
if(temp + atoi(*pa) + atoi(*pb) < 20){
temp = 1;
*padd = atoi(*pa) + atoi(*pb) - 10;
}
else{
temp = 2;
*padd = atoi(*pa) + atoi(*pb) - 20;
}
}
else{
*padd = atoi(*pa) + atoi(*pb);
temp = 0;
}
++padd;
++pa;
++pb;
}
i = 0;
while(useadd[i] != '\0'){
ret[i] = useadd[i];
++i;
}
return strrev(ret);
}
Thanks for all of the help. I'm sorry if this ends up being a stupid mistake.
Your program has so many problems!
char * bigadd(char a[], char b[]){
int i, temp;
char useadd[MAX]; // MAX might not be large enough
char usea = strrev(a); // should not modify argument a
// strrev() is not standard C undefined on my system
// if defined, it returns char*, not char
char useb = strrev(b); // see above
char ret[strlen(useadd)]; // useadd is uninitialized -> undefined behaviour
char *pa, *pb, *padd;
padd = &useadd; // & is incorrect, useadd is an array
pa = &usea; // same as above
pb = &useb; // idem
// forgot to initialize temp to 0
while(*pa != '\0' && *pb != '\0'){
if(atoi(*pa) + atoi(*pb) + temp > 9){ // atoi converts a string, not a char
if(temp + atoi(*pa) + atoi(*pb) < 20){ // same... sum cannot exceed 19
temp = 1;
*padd = atoi(*pa) + atoi(*pb) - 10; // atoi...
}
else{ // never reached
temp = 2;
*padd = atoi(*pa) + atoi(*pb) - 20; // atoi...
}
}
else{
*padd = atoi(*pa) + atoi(*pb); // same atoi problem
temp = 0;
}
++padd;
++pa;
++pb;
}
// if a and b have a different size, loop fails to copy digits and propagate carry
// if temp is not 0, you forget to add the leading '1'
// trailing '\0' is not set
i = 0;
while(useadd[i] != '\0'){ // undefined behaviour, '\0' not set.
ret[i] = useadd[i];
++i;
}
// forgot to set the final '\0'
// why not use strcpy?
return strrev(ret); // strrev not defined.
// if defined, returning ret, address of local array
}
Here is a complete rewrite:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *bigadd(const char a[], const char b[]) {
int ia = strlen(a);
int ib = strlen(b);
int size = 2 + (ia > ib ? ia : ib), ic = size - 1, temp = 0;
char *res = malloc(size);
if (res) {
res[--ic] = '\0';
while (ia + ib) {
if (ia > 0) temp += a[--ia] - '0';
if (ib > 0) temp += b[--ib] - '0';
res[--ic] = '0' + temp % 10;
temp /= 10;
}
if (temp) {
memmove(res + 1, res, size - 1);
*res = '1';
}
}
return res;
}
int main(int argc, char *argv[]) {
for (int i = 1; i + 1 < argc; i += 2) {
char *res = bigadd(argv[i], argv[i+1]);
printf("%s + %s = %s\n", argv[i], argv[i+1], res);
free(res);
}
}

Resources