我想撰寫一個函式,它可以使用目標指標來分配記憶體并將源復制到其中。
如果我嘗試在函式內部為目標分配記憶體,函式內部的 char *dest 將獲得一個完整的新地址,我的意思是指標本身的地址。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void ownStrCpy(char *dest, char *src){
printf("%p = address inside ptr variable c inside function before malloc\n", dest);
dest = (char*)malloc(strlen(src)*sizeof(char) 1);
printf("%p = address inside ptr variable c inside function after malloc\n", dest);
printf("%p = address from ptr variable c inside function after malloc\n", &dest);
for(int i=0; i < strlen(src); i ){
dest[i] = src[i];
}
dest[strlen(src) 1] = '\0';
}
int main()
{
char *c;
char a = 'a';
//c = NULL;
c = &a;
printf("%p = address from variable a\n", &a);
printf("%p = address from ptr variable c\n", &c);
printf("%p = address inside ptr variable c\n", c);
ownStrCpy(c, "Example String");
printf("%p = address inside ptr variable c after function call\n", c);
char *c2;
printf("\n%p = address from variable c2\n", &c2);
printf("%p = address inside ptr variable c2\n", c2);
c2 = (char*)malloc(10*sizeof(char) 1);
printf("%p = address inside ptr variable c2\n", c2);
printf("%p = address from ptr variable c2 after malloc\n", &c2);
//free(c);
free(c2);
return 0;
}
這就是輸出。
000000000061FE17 = address from variable a
000000000061FE18 = address from ptr variable c
000000000061FE17 = address inside ptr variable c
000000000061FE17 = address inside ptr variable c inside function before malloc
0000000000A46E30 = address inside ptr variable c inside function after malloc
000000000061FDE0 = address from ptr variable c inside function after malloc
000000000061FE17 = address inside ptr variable c after function call
000000000061FE08 = address from variable c2
000000000000003E = address inside ptr variable c2
0000000000A46E50 = address inside ptr variable c2
000000000061FE08 = address from ptr variable c2 after malloc
如您所見,“malloc 后函式內部 ptr 變數 c 的地址”已更改為不存在的地址?
我嘗試更改將指標傳遞給函式的方式,嘗試使用雙指標,嘗試以不同方式訪問函式內部的指標,但沒有任何效果。
我知道我可以在主函式中回傳指向指標變數“c”的指標,但這不是我想要的。在為 c2 呼叫 malloc() 后,主函式內部的部分保持不變的區別在哪里?
uj5u.com熱心網友回復:
指標只是一個數字。考慮一下:
int dest = 123;
dest = 456;
dest將是 456。
現在考慮一下。
char *c;
char a = 'a';
c = &a;
char *dest = c;
dest = malloc(10);
將dest包含&a? 不,它將包含malloc.
這就是ownStrCpy正在做的事情。dest它立即用 的內容覆寫malloc。
你可以用雙指標做你想做的事。
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
void ownStrCpy(char **dest, const char *src) {
size_t len = strlen(src);
// Allocate new memory.
// char is defined to be sizeof 1
// don't cast the result of malloc
char *copy = malloc(len 1);
// Copy to the new memory.
for(int i=0; i <= len; i ) {
copy[i] = src[i];
}
copy[len 1] = '\0';
// `dest` points at c.
// `*dest` is the value of c, currently `&a`.
// `*dest = copy` changes the value of `c` to `copy`.
*dest = copy;
}
int main() {
char *c;
char a = 'a';
c = &a;
ownStrCpy(&c, "Example String");
printf("c = %s", c);
}
通過傳入,&c您可以更改cinside的值ownStrCpy。
但是,如果您要在函式內分配新記憶體,只需回傳新指標即可。
char *ownStrCpy(const char *src) {
size_t len = strlen(src);
// Allocate new memory.
// char is defined to be sizeof 1
// don't cast the result of malloc
char *copy = malloc(len 1);
// Copy to the new memory.
for(int i=0; i <= len; i ) {
copy[i] = src[i];
}
copy[len 1] = '\0';
return copy;
}
int main() {
char *c;
char a = 'a';
c = &a;
c = ownStrCpy("Example String");
printf("c = %s", c);
}
而您剛剛實施了strdup.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/430734.html
