passing a string of characters to a function

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Guys when I pass a string of characters it only works once.  This is the results:

 

and the code is:

 

static struct group test(char * string, unsigned int size){

	printf("\n\renter %s", string);
	struct group found;

	for(int block_size = size; block_size > 0; block_size--){

		for(int index = 0; index < dictionary_index; index++){

			if(strlen(dictionary
.string_entry) == size){ if(memcmp(dictionary
.string_entry, string, block_size) == 0){ // Append data to work on to dictionary string[block_size] = '\0'; insert_dictionary(string); // configure coded output found.index = index; found.size = block_size; found.data = string[block_size + 1]; return(found); } } } size--; } // Append data to work on to dictionary string[1] = '\0'; insert_dictionary(string); found.index = 0; found.size = 1; found.data = string[0]; return found; } void encode_lz78(char * inRaw, unsigned int Size){ test(&inRaw[0], 0); test(&inRaw[1], 1); test(&inRaw[2], 1); test(&inRaw[3], 1); printf("\n\r%s", dictionary[0].string_entry); printf("\n\r%s", dictionary[1].string_entry); printf("\n\r%s", dictionary[2].string_entry); printf("\n\r%s", dictionary[3].string_entry); }

EDITED: I'm passing "abbcbcababcaabcaab"

 

OH! and it happens when I pass the string directly, test("abbcbcababcaabcaab", 0);

 

This topic has a solution.
Last Edited: Tue. Dec 24, 2019 - 05:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not enough context. Make a complete, compilable program that shows the issue.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:
Guys when I pass a string of characters it only works once.

 

No surprise about that then.

    // Append data to work on to dictionary
    string[1] = '\0';
    insert_dictionary(string);

If you need your functions to NOT modify original strings; add const to the signature as below. You'll receive a warning if you subsequently try to modify it.

static struct group test(const char * string, unsigned int size)
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh - and this also looks suspicious.

static struct group test (char *string, unsigned int size)
{
    printf("\n\renter %s", string);
    struct group found;

    ...
    ...

    found.index = 0;
    found.size = 1;
    found.data = string[0];

    return found;
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fianawarrior wrote:
Guys when I pass a string of characters

 

You are not passing a string of characters. You are passing a pointer to a [first] character in a string of characters. Everything you modify through that pointer impacts the original string. If you store that pointer for a long term, it will continue to point to the original string: if you modify the original string later, the modification will be seen through a stored pointer; if you destroy the original string later, the pointer will become a "hanging" one.

 

It is not entirely clear what happens in your code, since it is not sufficiently complete, but it seems to suffer from failing to take into account what I stated above.

 

 

Dessine-moi un mouton