2015-10-07 36 views
2

我跑了下面的代码,并且它永远运行的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; 
} 
+0

写入'*(指针+ CNT)的传统方法'是'指针[CNT] '。 –

+0

你不能释放递增的指针;你必须释放'malloc()' - 或'calloc()'或'realloc()'返回的内容或者...保留返回值的副本。 –

+0

@乔纳森..感谢您的回答,我意识到空字符('\ 0)和空指针之间的区别。还有关于释放指针的更正帮助。 – Rommel

回答

0

您已选择写:

while(ptr!='\0') 

这是写作的一种有趣的方式:在这里你打算写

while (ptr != NULL) 

while (ptr != 0) 

或:

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字节的缓冲区,你完全可以在01中分配一个局部变量并通过地址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'; 
} 
相关问题