Returning string from function having multiple NUL

2019-08-01 11:54发布

问题:

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().