Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to return an script in c / gcc?
    primarykey
    data
    text
    <p>had been years since I wrote my last line in C, now I'm trying to start again to make some function and use than as an extension for php.</p> <p>this is an easy function to create "small urls"</p> <p>lets say:</p> <p>a0bg a0bf a0bh</p> <p>the problem I'm having is when I have to "increment" an string like <em>zzz</em> then I got: <em>Bus Error</em></p> <p>otherwise if I increment <em>abr</em> for example I got the result: <em>abs</em></p> <p>at some point I think that my problem here is returning the string result</p> <p>both code are fully functional as I posted them here.</p> <p>to compile I'm using: <em>gcc append_id_test.c -o append_id</em></p> <p>I'm on OS X leopard</p> <p>when I do this, it works: (please pay attention on the call to the function append_id)</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;stdio.h&gt; char* append_id(char*); int main(void) { char *x; char *incremented; x = "aaab"; printf("%s\n", append_id(x)); // should print 00000 (lenght 5) incremented = (char *) malloc((strlen(x) + 2) * sizeof(char)); incremented = append_id(x); printf("---&gt; %s\n", incremented); // should print 00000 (lenght 5) } char* append_id(char *id) { int x; char* new_id; int id_size = strlen(id); new_id = (char *) malloc((strlen(id) + 2) * sizeof(char)); for ( x = 0; x &lt; id_size; x++ ) { new_id[x] = '0'; } strcat(new_id, "0"); return new_id; } </code></pre> <p>but the whole code isn't working (AS you see the call to the append_id function was made in the same way as the above example)</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;stdio.h&gt; char* append_id(char*); char* increment_id(char*, int); char* get_next_id(char*); int main(int argc, char *argv[]) { char *x; int a; x = "zz"; printf("incrementando %s -&gt; %s\n", "zz", get_next_id(x)); return 0; } char * get_next_id(char *last_id) { int x, pos; char *next_id; char is_alnum = 1; // if the last id is -1 (non-existant), start at the begining with 0 if ( strlen(last_id) == 0 ) { next_id = "0"; } else { // check the input for(x = 0; last_id[x]; x++) { if(!isalnum(last_id[x])) { is_alnum = 0; break; } } if (is_alnum == 0) { return ""; } // all chars to lowercase for(x = 0; last_id[x]; x++) { last_id[x] = tolower(last_id[x]); } // loop through the id string until we find a character to increment for ( x = 1; x &lt;= strlen(last_id); x++ ) { pos = strlen(last_id) - x; if ( last_id[pos] != 'z' ) { next_id = increment_id(last_id, pos); break; // &lt;- kill the for loop once we've found our char } } // if every character was already at its max value (z), // append another character to the string if ( strlen(next_id) == 0) { next_id = (char *) malloc((strlen(last_id) + 2) * sizeof(char)); next_id = append_id(last_id); } } return next_id; } char* append_id(char *id) { int x; char* new_id; int id_size = strlen(id); new_id = (char *) malloc((strlen(id) + 2) * sizeof(char)); for ( x = 0; x &lt; id_size; x++ ) { new_id[x] = '0'; } strcat(new_id, "0"); return new_id; } char* increment_id(char *id, int pos){ char current, new_char; char * new_id ; int x; new_id = (char *) malloc((strlen(id) + 1) * sizeof(char)); current = id[pos]; if ( current &gt;= 0x30 &amp;&amp; current &lt;= 0x39 ) { if ( current &lt; 0x39 ) { new_char = current + 1; } else // if we're at 9, it's time to move to the alphabet { new_char = 'a'; } } else // move it up the alphabet { new_char = current + 1; } for ( x = 0; x &lt; strlen(id); x++ ) { if (x == pos) { new_id[x] = new_char; } else { new_id[x] = id[x]; } } // set all characters after the one we're modifying to 0 if ( pos != (strlen(new_id) - 1) ) { for ( x = (pos + 1); x &lt; strlen(new_id); x++ ) { new_id[x] = '0'; } } return new_id; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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