據說我們可以寫多個聲明但只有一個定義。現在,如果我實現我自己的strcpy函數有相同的原型:實現一個新的strcpy函數重新定義了庫函數strcpy?
char * strcpy (char * destination, const char * source);
然後我不能重新定義現有的庫函數?不應該顯示一個錯誤?或者它是否與目標代碼形式提供的庫函數有關?
編輯:在我的機器上運行下面的代碼說「分段錯誤(核心轉儲)」。我在linux上工作,編譯時沒有使用任何標誌。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strcpy(char *destination, const char *source);
int main(){
char *s = strcpy("a", "b");
printf("\nThe function ran successfully\n");
return 0;
}
char *strcpy(char *destination, const char *source){
printf("in duplicate function strcpy");
return "a";
}
請注意,我沒有試圖實現該功能。我只是試圖重新定義一個功能並要求後果。
編輯2: 通過Mats應用建議的更改後,該程序不再給出分段故障,但我仍在重新定義該函數。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strcpy(char *destination, const char *source);
int main(){
char *s = strcpy("a", "b");
printf("\nThe function ran successfully\n");
return 0;
}
char *strcpy(char *destination, const char *source){
printf("in duplicate function strcpy");
return "a";
}
你不能做到這一點,那將崩潰 – DGomez
這可能與:如何以取代C標準庫函數](http://stackoverflow.com/questions/9107259/how-to-replace-c-standard-library-functioin)。 –
@DGomez在我的機器上,程序沒有崩潰。 –