Returning string from function having multiple NULL '\0' in C

时间秒杀一切 提交于 2019-12-14 02:58:01

问题


I am compressing string. And the compressed string sometimes having NULL character inside before the end NULL. I want to return the string till the end null.But the compressor function is returning the sting till the occurring of the first NULL. Please help me.

char* compressor(char* str)
    {
      char *compressed_string;
      //After some calculation
      compressed_string="bk`NULL`dk";// at the last here is automatic  an NULL we all know
    return compressed_string;
    }
void main()
   {
    char* str;
    str=compressor("Muhammad Ashikuzzaman");
    printf("Compressed Value = %s",str);

   }

The output is : Compressed Value = bk; And all other characters from compressor function is not here. Is there any way to show all the string.


回答1:


#include <stdio.h>
#include <stdlib.h>
char* func(char *str, int *length)
        {
          int i;
          *length=9;//Put string length you calculated
          str=(char*)malloc(*length*sizeof(char));
      for(i=0;i<5;i++)
        str[i]=i+65;
        str[5]=0;
        str[6]=65;
        str[7]=67;
        str[8]=0;   
          return str;
        }
int main()
{
    char *str;
    int i,length=0; 
    str=func("Muhammad Ashikuzzaman",&length); 
    for(i=0;i<length;i++)
         printf("%c",str[i]);
    scanf("%d",&i);
    return 0;
}



回答2:


The function returns "entire string". It is printf that outputs it until the null character will be encountered.

You could define the function the following way

char * compressor( const char* source, size_t *result_len );

To understand the problem consider the following code

#include <stdio.h>

char * compressor( const char* source, size_t *result_len )
{
    char *compressed_string = "bk\0dk";
    *result_len = sizeof( "bk\0dk" );

    return compressed_string;
}


int main( void )
{
    char* str;
    size_t n;

    str = compressor( "Muhammad Ashikuzzaman", &n );

    int i;
    printf( "Compressed Value = " );

    for ( char *p = str; n; n -= i + 1, p += i + 1 )
    {
        i = printf( "%s", p );
    }

    return 0;
}

The output is

Compressed Value = bkdk



回答3:


Solution using std::string:

#include <string>
#include <iostream>
#include <iterator>

std::string compressor(char* str)
{
   char *compressed_string;
   int len;  // this is the size of the compressed data
   //...
   // compress the data and assume that len has the number of characters
   //...
   std::string theString(compressed_string, len);
   // clean up any memory here.
   //...
   return theString;
}

using namespace std;

int main()
{
    std::string str = compressor("Muhammad Ashikuzzaman");
    std::copy(str.begin(), str.end(), std::ostream_iterator<char>(cout,""));
}

Note the usage of std::string, as well as how the information is outputted using the copy algorithm function. The reason why copy is used instead of printf is to ensure that all of the characters, including the (invisible) embedded NULL's are printed.

Also, the size of the compressed data is easily retrieved by calling str::size().



来源:https://stackoverflow.com/questions/25835422/returning-string-from-function-having-multiple-null-0-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!