0
char *val1 = "/root";
char *val2 = "/p";
val1 = val1+val2;

I want to add 2 char pointer value and assign it to 1st value. above is the code snippt.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Zeb-ur-Rehman
  • 1,121
  • 2
  • 21
  • 37
  • You should really take a look at C++ if you want to write code this way... Or any other higher level language. – rubenvb Jul 01 '12 at 17:23

2 Answers2

6

It can't be done that way. Since you have two pointers, trying to add them will try to add the pointers themselves, not manipulate what they point at. To concatenate the two strings, you need to have/allocate a single buffer large enough to hold both:

char *both = malloc(strlen(val1) + strlen(val2) + 1);
if (both != NULL) {
    strcpy(both, val1);
    strcat(both, val2);
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

Use strcat or strncat functions to concatenate strings. C has no string concatenation operator.

ouah
  • 142,963
  • 15
  • 272
  • 331