Note that there are some explanatory texts on larger screens.

plurals
  1. POParse a URL in C
    text
    copied!<p>I am trying to parse several elements of a URL in C. </p> <p>This is a prototype of a URL:</p> <blockquote> <p>ftp://[&lt; name>:&lt; pass>@]&lt; domain>/&lt; url>";</p> </blockquote> <p>The problem is that certain elements aren't being saved correctly, for example I try to save the <strong>ftp:// in here ftp[6]</strong>. However, when I print it I get something like this <strong>ftp:/d@</strong>. Which should not even be possible since the array does not have enough space for this. </p> <pre><code>int main() { char ftp[6]; char *name; char *pass; char *domain; char *url; char *var = "ftp://[coiso:pass@]teste/umgrandeurl"; int x; int size_name; int size_pass; int size_domain; int size_url; int flag = 0; for (x = 0; x &lt; strlen(var); x++) { if (x == 6) { strncpy(ftp, var, 6); if (strcmp(ftp, "ftp://") != 0) { } } if (var[x] == ':' &amp;&amp; x &gt; 3) { size_name = x - 7; name = (char *)malloc(size_name + 1); strncpy(name, &amp;var[7], size_name); } if (var[x] == '@') { size_pass = x - (8 + size_name); pass = (char *)malloc(size_pass + 1); strncpy(pass, &amp;var[8 + size_name], size_pass); } if (var[x] == '/' &amp;&amp; x &gt; 6 &amp;&amp; flag == 0) { flag = 1; size_domain = x - (10 + size_pass + size_name); domain = (char *)malloc(size_domain + 1); strncpy(domain, &amp;var[10 + size_pass + size_name], size_domain); } if (x == strlen(var) - 1) { size_url = x - (10 + size_pass + size_name + size_domain); url = domain = (char *)malloc(size_url + 1); strncpy(url, &amp;var[11 + size_pass + size_name + size_domain], size_url); printf("%d", size_url); } } return 0; } </code></pre> <p>EDIT: Well the same thing happens to me in the pass but it isn't because of the null char.</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