string - C compilation error for basic function -
i'm trying write own version of strcat (i call "append"). here's have:
#include <stdio.h> int main() { char *start = "start"; char *add = "add"; append(start, add); printf(start); } void append(char *start, char *add) { //get end of start word char *temp = &start; while (*temp != '\0') { temp++; } *temp = *add; while (*temp != '\0') { *temp = *add; } } when compile, 3 warnings , error:
1) warning: implicit declaration of function 'append' invalid in c99
2) warning: format string not string literal (potentially insecure)
3) error: conflicting types 'append'
i don't see how arguments pass append function within main conflict function definition below it.
4) warning: incompatible pointer types initializing 'char *' expression of type 'char **'; remove &
why want remove & here? thought declare , initialize char pointer proper memory address @ once.
any appreciated.
you have multiple problems short code. first have
warning: implicit declaration of function 'append' invalid in c99
the meaning of warning need declare functions before use them. if don't declare function before use it, compiler have guess arguments , return type, , guesses badly.
continuing next warning:
warning: format string not string literal (potentially insecure)
this because provide string variable printf, is, warning tells you, insecure. think example case read input user, , use input format string printf. stop user adding format codes in input string? , since don't pass arguments arguments formats come from?
and error:
error: conflicting types 'append'
this because of first problem, compiler guessed arguments or return type of function wrongly.
now on major problem doesn't show compiler errors or warnings, namely undefined behavior.
the problem start , add variables points string literals. string literal read only (in fact, string literal pointer array of non-modifiable characters). first problem try modify contents of these arrays, second arrays big needed , writing outside of memory. both these problems causes undefined behavior.
Comments
Post a Comment