strcpy function does not work on Linux

Asked

Viewed 282 times

1

#include <stdio.h>
#include <string.h>

void envia(char* pdados);

int main()
{
char* link[1000];
strcpy(link,"http://site.com/data.php?dados=");

char* dados = "name";

strcat(link,dados);

//printf("%s\n",link);
envia(link);
}

void envia(char *pdados){
printf("%s\n",pdados);

char comando[2000];
sprintf(comando, "curl %s >> /dev/null", pdados);
system(comando);
}

ERROR:

manda.c: In function ‘main’:
manda.c:9:2: warning: passing argument 1 of ‘strcpy’ from incompatible pointer type [enabled by default]
  strcpy(link,"http://site.com/data.php?dados=");
  ^
In file included from manda.c:2:0:
/usr/include/string.h:129:14: note: expected ‘char * __restrict__’ but argument is of type ‘char **’
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^
manda.c:13:2: warning: passing argument 1 of ‘strcat’ from incompatible pointer type [enabled by default]
  strcat(link,dados);
  ^
In file included from manda.c:2:0:
/usr/include/string.h:137:14: note: expected ‘char * __restrict__’ but argument is of type ‘char **’
  • 2

    Instead of char* link[1000];, how about trying char link[1000];, that is, without the *?

3 answers

1


As a link has been declared as a pointer (from a char array), therefore the link variable (or the link[0]) represents a pointer pointer (char **), as both of them alone (without the statement as pointer) indicate a pointer to the beginning of the char array. So instead of using char* link[1000] utilise char link[1000]. Because the strcpy function receives a char * and not a char **, the same goes for strcat.

0

I changed the data type of the function, and now it’s working, thanks for the help

void envia(char *pdados)
{
    char* dados = pdados;

    char* resultado;
    size_t  len;

    len = (size_t)("http://site.com/data.php?dados=");
    resultado = malloc((len + 1) * sizeof(char));

    strncpy(resultado, "site.com/data.php?dados=", len);

    strcat(resultado,dados);
    //printf("%s\n",resultado);

    char comando[2000];
    sprintf(comando, "curl -s %s >> /dev/null &", resultado);
    system(comando);
}
  • Warning: strcat has not allocated the space needed to receive the "data".

-1

#include <stdio.h>
#include <stdlib.h>

void envia(char *pdados) {
   char comando[1000];
   sprintf(comando, "curl -s '%s%s' > /dev/null",     
             "http://site.com/data.php?dados=", pdados);
   system(comando);
}

int main(){
   envia("aaa");
   return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.