int* ptr = (int*)malloc(sizeof(int));
*ptr = 10;
printf("%d\n",*ptr);
printf("%p\n",ptr);
printf("%p\n",&ptr);
複製代碼
char arr[] = "Wo Ai Sun Yi Hang";
char *ptr = "Wo Ai Sun Yi Hang";
strcpy(arr,"SYH");
strcpy(ptr,"SYH");//FAIL pointer is pointing to a read only memory
複製代碼
Array can only point to stack. ptr can point to any memory. It can't be changed when pointing to a read only memory.ide
char arr[] = "SYHAW";
char* ptr = "WASYH";
sizeof(arr);//6;
sizeof(ptr);//8 because ptr is a pointer
複製代碼
int* f1(int *p) {//OK,pointer point to another integer
*p = 42;
return p;
}
char* f2() {//FAIL, array is pointing to stack, invalid afer return
char p[] = "Hello";
return p;
}
char* f3() {//OK, string contans don't change
char *p = "Hello";
return p;
}
char* f4() {//OK, static will keep it after return, static not on stack or heap
static char p[] = "Hello";
return p;
}
複製代碼
strdup() will malloc enough space and then call strcpy(). It is more safe than strcpy and won't overflow, but need to be free.ui
printf("%d and %s\n", int, str);
putchar(char);
puts(ptr*);
fprintf(file,"%d and %s\n", int, str);
dprintf(fd,"%d and %s\n", int, str);
複製代碼
perror(const char* message) will print to stderrspa
gets() assume that the space is enough and has been removed. Now we are using fgets and getline()code
char buff[10];
fgets(buff, sizeof(buff), stdin);
if(buff[strlen(buff)-1] == '\n'){
buff[strlen(buff)-1] = '\0';
}
char* buffer = NULL;
size_t size = 0;
ssize_t char = getline(&buffer, *size, stdin);
if(char>0 && buffer[strlen(buffer)-1]=='\n'){
buffer[strlen(buffer)-1] = '\0';
}
char = getline(&buffer, *size, stdin); //read another line
free(buffer);
}
複製代碼
char* strcpy(char*dest, char*src);
char* strncpy(char*dest, char*src, size_t n);
char* strcat(char*dest, char* src);
char* strncat(char* dest, char* src, size_t n);
int strcmp(char* dest, char* src); //compare, will return 0 if they are equal
int strncmp(char* dest, char* src, size_t n);
void* memcopy(void* dest, void* src, size_t n);
void* memset(void* b, int c, size_t len); // set first n to be c
複製代碼
Q 1.1: Global variable and static variable will be initialized as 0 Q 1.2: Calloc will initialize to 0 Q 1.3: Need strlen()+1 Q 1.4a: Malloc didn't initialize Q 1.4b: First, sizeof(src) is always 8. Second, array point to stack, will be free after return Q 1.5: First is valid, string is imutable Q 1.6: Q 1.7: Q 1.8:rem
char* strcat(char* dest,const char* src){
int len = sizeof(*dest) - 1;
char* end = NULL;
for(; len >= 0; len--){
if(len == 0 && dest[0] == NULL){
end = dest[0];
}
if((dest[len]==NULL)&&(dest[len-1]!=NULL)){
end = dest[len];
break;
}
}
strcpy(end, src);
return dest;
}
複製代碼
Q 1.9: find the first null char Q 1.10: const variable can't be changed Q 2.1: no file stream Q 3.1: "w+", fprintf(f, "%s score is %d", name, score);get
System call is inside kernel and very costly. man 2 is System, man 3 is Library.string
char* str = "Wo ai ni";
printf("%p",ptr);
printf("%s",*ptr);
printf("%p",&ptr);
複製代碼
If s is '\0'.it
A bunch of bytes until reach the '\0' pr NULL. Need strlen(s)+1 space.io
void trunc(char*s, size_t max){
if(strlen(s) < max || s == NULL){
return;
}
for(int i=max; i<strlen(s); i++){
s[i] = '\0';
}
return;
}
複製代碼
void duplicate(char** argc, char** result){
int size = sizeof(argc)/sizeof(argc[0]);
char** result = malloc(sizeof(argc));
for(int i = 0; i < size; i++){
memcpy(result[i],argc[i],sizeof(*result[i]));
}
return;
}
複製代碼