#include<stdio.h> int main(void) { char *ptr = "Linux"; *ptr = 'T'; printf("\n [%s] \n", ptr); return 0; }
Q:你能否寫一個程序在它運行時修改它的名稱? (Can you write a program that changes its own name when run?)
#include<stdio.h> int main(int argc, char *argv[]) { int i = 0; char buff[100]; memset(buff,0,sizeof(buff)); strncpy(buff, argv[0], sizeof(buff)); memset(argv[0],0,strlen(buff)); strncpy(argv[0], "NewName", 7); // Simulate a wait. Check the process // name at this point. for(;i<0xffffffff;i++); return 0; }
int i = 0; //…… for(;i<0xffffffff;i++);
for ( i = 0 ; i < 0xffffffff ; i ++ ) ;
char buff[100]; memset(buff,0,sizeof(buff)); strncpy(buff, argv[0], sizeof(buff)); memset(argv[0],0,strlen(buff)); strncpy(argv[0], "NewName", 7);
memset(buff,0,sizeof(buff));
strncpy(buff, argv[0], sizeof(buff));
char s[4]; strncpy( s , "1" , 4); printf("%d %d %d %d\n",s[0],s[1],s[2],s[3]);
49 0 0 0
memset(argv[0],0,strlen(buff));
memset(argv[0],0,strlen(argv[0]));
strncpy(argv[0], "NewName", 7);