somebody please explain about strings in c? -
how store 2 strings 1 after other without concatenation (we can increment address)
char str[10]; scanf("%s",str); str=str+9; scanf("%s",str); note: here if give first string bala , 2nd hi, should print hi after bala . hi should not replace bala.
you cannot increment (or change in other way) array that, array variable (str) constant cannot changed.
you can so:
char str[64]; scanf("%s", str); scanf("%s", str + strlen(str)); this first scan str, scan once more, starting new string right on top of terminating '\0' of first string.
if enter "bala" first, beginning of str this:
+---+---+---+---+----+ str: | b | | l | | \0 | +---+---+---+---+----+ and since strlen("bala") four, next string scanned buffer starting right on top of '\0' visible above. if enter "hi", str start so:
+---+---+---+---+---+---+----+ str: | b | | l | | h | | \0 | +---+---+---+---+---+---+----+ at point, if print str print "balahi".
of course, very dangerous , introduce buffer overrun, that's wanted.
Comments
Post a Comment