strcpy array element to string?

Hello everyone,
The code below is supposed to split an IPv4 address into 4 segments, {"192","168","1","1"}, and concatenate it together again with the last value (in this case "1") being iterated through using the for() loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
         strcpy(src_ip,"192.168.1.5");
 
         char *ip = src_ip, *ip2; // Split src_ip into array : val[]
         unsigned char val[4] = {0};
         size_t index = 0;
         ip2 = ip;
         while(*ip){
                 if(isdigit((unsigned char)*ip)){
                         val[index] *= 10;
                         val[index] += *ip - '0';
                 }else{
                         index++;
                 }
                 ip++;
         }
         char tmp_ip[20];
         for(i=0;i<sizeof(target)/sizeof(target[0]);i++){
                strcpy(tmp_ip,val[0]); // <--- Errors encountered here,
                strcat(tmp_ip,val[2]); // Here,
                strcat(tmp_ip,val[3]); // Here,
                strcat(tmp_ip,val[i]); // And here.
                target[i] = tmp_ip;
         }


The errors I'm receiving when compiling (using gcc) are:
1
2
3
4
5
6
7
8
9
10
11
12
13
test.c: In function ‘main’:
test.c:104:3: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [enabled by default]
In file included from test.c:3:0:
/usr/include/string.h:128:14: note: expected ‘const char * __restrict__’ but argument is of type ‘unsigned char’
test.c:105:3: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [enabled by default]
In file included from test.c:3:0:
/usr/include/string.h:136:14: note: expected ‘const char * __restrict__’ but argument is of type ‘unsigned char’
test.c:106:3: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [enabled by default]
In file included from test.c:3:0:
/usr/include/string.h:136:14: note: expected ‘const char * __restrict__’ but argument is of type ‘unsigned char’
test.c:107:3: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [enabled by default]
In file included from test.c:3:0:
/usr/include/string.h:136:14: note: expected ‘const char * __restrict__’ but argument is of type ‘unsigned char

I've tried googling but no luck.
TL;DR: How do I strcpy an array element to a string?
Problem is that strcpy and strcat works only on strings (null terminated char arrays). If you want to convert the integer values back to strings you can use something like sprintf.
Topic archived. No new replies allowed.