【问题标题】:How to append null terminator to end of the indexed char pointer如何将空终止符附加到索引字符指针的末尾
【发布时间】:2015-10-07 03:37:03
【问题描述】:

我运行了以下代码,但它在 while 循环永远运行时崩溃了。当我调试这段代码时,我在*(pointer+cnt)='\0' 发现了问题;空字符永远不存在。我不知道如何在此处附加空终止符,以免程序崩溃。

#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;
}

【问题讨论】:

  • *(pointer+cnt) 的常规写法是pointer[cnt]
  • 你不能释放递增的指针;您必须释放 malloc() — 或 calloc()realloc() 或……返回的内容。保留返回值的副本。
  • @ Jonathan..感谢您的回答,我意识到空字符('\0)和空指针之间的区别。有关释放指针的更正也有所帮助。

标签: c null-terminated char-pointer


【解决方案1】:

你选择写作:

while(ptr!='\0')

这是一种有趣的写作方式:

while (ptr != 0)

或:

while (ptr != NULL)

你打算写的地方:

while (*ptr != '\0')

*(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 != '\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;
}

或者:

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;
}

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

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;
}

对于 9 字节的缓冲区,您可以很好地在 main() 中分配一个局部变量并将地址传递给 decimal_binary(),因此它不需要使用 malloc() 并且 main() 不需要免费使用:

#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';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 2022-12-11
    • 2014-03-06
    • 2020-08-19
    • 1970-01-01
    • 2017-09-29
    • 2015-09-21
    • 2015-04-24
    相关资源
    最近更新 更多