简体   繁体   中英

How to add to String Array from inside recursive function in C

I am trying to write a C function that will give me the binary representation of a number n . The function I have prints the number correctly; however I would like the string array word to be updated with the same data as is being printed:

#include <stdio.h>
#include <stdlib.h>

#define MAXBIN 100

void printbitsrec(unsigned n, int n_bits, char *w)
{
    if (n_bits-- > 0) {
        printbitsrec(n >> 1, n_bits, w);
        if (n & 1) {
            printf("1");
            *w++ = '1';
        }
        else {
            printf("0");
            *w++ = '0';
        }

    }
}

void printBits(unsigned n, int n_bits, int ret)
{
    char word[MAXBIN];
    printbitsrec(n, n_bits, &word[0]);
    word[n_bits + 1] = '\0';
    if (ret)
        printf("\n");
    printf("word = %s\n", word);
}

int main() {
    printBits(2, 4, 1);
}

Is there a more elegant way to do this? What am I doing wrong in my code?

Here's my attempt:

#include <stdio.h>
#include <stdlib.h>

#define MAXBIN 100

char * printbitsrec(unsigned n, int n_bits, char *w)
{
    if (n_bits-- > 0) {
        w = printbitsrec(n >> 1, n_bits, w);
        const char c = (n & 1) ? '1' : '0';
        putchar(c);
        *w++ = c;
    }
    return w;
}

void printBits(unsigned n, int n_bits, int ret)
{
    char word[MAXBIN];
    char * w = printbitsrec(n, n_bits, &word[0]);
    *w++ = '\0';
    if (ret)
        printf("\n");
    printf("word = %s\n", word);
}

int main(void) {
    printBits(2, 4, 1);
    return 0;
}

Notable changes:

  • Use a returned value to keep track of the end of the string
  • Only compute the new character once
  • Use putchar rather than printf() , just a basic performance consideration ( printf() is overkill when printing single characters)

Note that no attempt to prevent buffer overflow is done, that could of course be added.

There is issue in recursive function call, it should be below after w++ . Check below code

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3
  4 #define MAXBIN 100
  5
  6 void printbitsrec(unsigned n, int n_bits, char *w)
  7 {
  8     if (n_bits-- > 0) {
  9         if (n & 1) {
 10             printf("1");
 11             *w = '1';
 12         }
 13         else {
 14             printf("0");
 15             *w = '0';
 16         }
 17         w++;
 18         printbitsrec(n >> 1, n_bits, w);
 19     }
 20 }
 21
 22 void printBits(unsigned n, int n_bits, int ret)
 23 {
 24     char word[MAXBIN];
 25     memset(word, 0x00, sizeof(word));
 26     printbitsrec(n, n_bits, &word[0]);
 27     //word[n_bits] = '\0';
 28     if (ret)
 29         printf("\n");
 30     printf("word = %s\n", word);
 31 }
 32
 33 int main() {
 34     printBits(2, 4, 1);
 35 }

Take Any global variable to preserve the array index inside recursive function which will solve your problem.

#include <stdio.h>
#include <stdlib.h>

#define MAXBIN 100

static int i = 0;
void printbitsrec(unsigned n, int n_bits, char *w)
{
    if (n_bits-- > 0) {
        printbitsrec(n >> 1, n_bits, w);
        if (n & 1) {
            printf("1");
            w[i] = '1';
        }
        else {
            printf("0");
            w[i] = '0';
        }
        i++;
    }
}

void printBits(unsigned n, int n_bits, int ret)
{

    char word[MAXBIN];
    printbitsrec(n, n_bits, word);
    word[i] = '\0';
    if (ret)
        printf("\n");
    printf("word = %s\n", word);
}

int main() {
    printBits(2, 4, 1);
}

As you increment *w after the recursive call of printbitsrec() w always points to the same memory Location. If you do not like global variables this works but looks not very nice:

void printBits(unsigned n, int n_bits, int ret)
{
    char word[MAXBIN];

    memset(word,0,MAXBIN);

    printbitsrec(n, n_bits, &word[0]);
//    *w++ = '\0';
    if (ret)
        printf("\n");
    printf("word = %s\n", word);
}

void printbitsrec(unsigned n, int n_bits, char *w)
{
    if (n_bits-- > 0)
    {
        w++;
        printbitsrec(n >> 1, n_bits, w);
        if (n & 1)
        {
            printf("1");
            *(w - 1)= '1';
        }
        else
        {
            printf("0");
            *(w - 1) = '0';
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM