Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm trying to increment values in a C character array, and it keeps failing - help?
    text
    copied!<p>Here is my function's code:</p> <pre><code>char * increment_b_string(char * b_string) { char * ret_string = (char *) malloc(7); ret_string = "001aaa"; if (*(b_string + 2) == '9') { *(ret_string +2) == '0'; if (*(b_string + 1) == '9') { *(ret_string +1) = '0'; *(ret_string) = *(b_string) + 1; } else { *(ret_string + 1) = *(b_string + 1) + 1; } } else { *(ret_string + 2) = *(b_string + 2) + 1; } return ret_string; } </code></pre> <p>Any thoughts as to why it might be failing? </p> <p>The general idea is that b_string will contain a value like "123aaa". The "aaa" portion does not matter, and will never change. Only the first 3 numbers will. They need to increment as though they were integers. Leading zeroes need to be preserved in the beginning. So, if input is "004aaa", output should be "005aaa". This only needs to go up to "300aaa", hence why I'm not considering anything more than that. It's a school programming assignment, hence the very contrived nature of the problem.</p> <p>Thanks.</p> <p>Edit: I've changed my function. Here is what it is now.</p> <pre><code>void increment_b_string(char * b_string) { if (b_string[2] == '9') { b_string[2] == '0'; if (b_string[1] == '9') { b_string[1] = '0'; b_string[0] += 1; } else { b_string[1] += 1; } } else { b_string[2] += 1; } } </code></pre> <p>Assuming that b_string is initially populated with...</p> <p>strcpy(b_string, "001aaa");</p> <p>...would this be correct? My program is still exhibiting the same behavior, but this may be an error somewhere else in the code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload