如何将空终止符附加到索引字符指针的末尾

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

我 运行 下面的代码,它永远与 while 循环 运行 一起崩溃。当我调试这段代码时,我发现问题在*(pointer+cnt)='[=11=]';空字符永远不会存在。我不知道如何在此处附加空终止符以使程序不会崩溃。

#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!='[=10=]')
   {
     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)='[=10=]';
return pointer;
}

您选择写:

while(ptr!='[=10=]')

这种写法很有趣:

while (ptr != 0)

或:

while (ptr != NULL)

您打算写的地方:

while (*ptr != '[=13=]')

*(pointer+cnt)的常规写法是pointer[cnt]

你不能释放递增的指针;您必须释放 malloc()calloc()realloc() 或 …

返回的内容

保留 binary_decimal() 返回值的副本并释放副本(或递增副本并释放 ptr 中的值)。

您可以在下面的代码中使用两个 binary_decimal() 函数中的任何一个:

#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 != '[=14=]')
    {
        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] = '[=14=]';
    return pointer;
}

或者:

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] = '[=15=]';
    return pointer;
}

这可以进一步压缩(甚至可读性更差):

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] = '[=16=]';
    return pointer;
}

对于 9 字节的缓冲区,您完全可以在 main() 中分配一个局部变量并将地址传递给 decimal_binary(),因此它不需要使用 malloc()main() 不需要使用 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 != '[=17=]')
    {
        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] = '[=17=]';
}