我正在學習 C 中的指標,我遇到了指標 X struts X 函式之間的混淆
目標:創建兩個結構并在其中改變屬性。
我要走的路:我正在創建這兩個結構,然后將其記憶體地址傳遞給 mutate 函式,然后該函式列印并改變這些結構的一些屬性。
結果我得到:
1:創建的struct的名字是nod被完全列印出來的,并且傳遞了錯誤的struct,life屬性沒有正確改變并列印到螢屏上。
2:在終端上我得到“Segmentation Fault”,不知道為什么,但我很確定我做錯了什么。
這是我的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int power;
int life;
char name[];
} Hero;
void attackHero(Hero *hero, int *power) {
(*hero).life = (*hero).life - *power;
printf("Damage: %d\n", *power);
printf("Attacked hero: %s\n", (*hero).name);
printf("Hero's remaining life: %d\n", (*hero).life);
};
int main () {
Hero flash;
flash.power = 250;
flash.life = 500;
strcpy(flash.name, "The Flash");
Hero batman;
batman.power = 380;
batman.life = 700;
strcpy(batman.name, "Batman arkham knight");
attackHero(&flash, &batman.power);
return 0;
}
結果列印到終端(Vscode gcc):

uj5u.com熱心網友回復:
這是我在編譯原始代碼時收到的警告:
1.c:25:2: warning: ‘__builtin_memcpy’ writing 10 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
25 | strcpy(flash.name, "The Flash");
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.c:30:2: warning: ‘__builtin_memcpy’ writing 21 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
30 | strcpy(batman.name, "Batman arkham knight");
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
如果要使用靈活陣列,則必須為它分配空間,如下所示:
int main () {
Hero *flash = malloc(sizeof(*flash) sizeof("The Flash"));
flash->power = 250;
flash->life = 500;
strcpy(flash->name, "The Flash");
Hero *batman = malloc(sizeof(*flash) sizeof("Batman arkham knight"));
batman->power = 380;
batman->life = 700;
strcpy(batman->name, "Batman arkham knight");
attackHero(flash, &batman->power);
free(flash);
free(batman);
}
這里對結果代碼進行了一些重構,我為 malloc 添加了錯誤檢查:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int power;
int life;
char name[];
} Hero;
Hero *createHero(int power, int life, const char *name) {
Hero *h = malloc(sizeof(*h) strlen(name) 1);
if(!h) {
printf("malloc failed\n");
exit(1);
}
h->power = power;
h->life = life;
strcpy(h->name, name);
return h;
}
void attackHero(Hero *hero, int power) {
hero->life -= power;
printf(
"Damage: %d\n"
"Attacked hero: %s\n"
"Hero's remaining life: %d\n",
power,
hero->name,
hero->life
);
};
int main(void) {
Hero *flash = createHero(250, 500, "The Flash");
Hero *batman = createHero(380, 700, "Batman arkham knight");
attackHero(flash, batman->power);
free(flash);
free(batman);
}
或者使用固定陣列(char [64]如@Diego 建議的那樣)或 achar *并為其分配空間。前者只需要從原來的兩行代碼更改:
// largest name in use
#define NAME_LEN sizeof("Batman arkham knight")
typedef struct {
int power;
int life;
char name[NAME_LEN];
} Hero;
uj5u.com熱心網友回復:
整個 lotta malloc() 繼續。由于英雄的名字是字串字面量(假設它們沒有改變),只需改變 name[]; 到結構中的 const char *name 并通過簡單的賦值進行初始化:
flash.name = "The Flash";
batman.name = "Batman arkham knight";
不用擔心 malloc() 失敗、名稱大小或 free() 要求。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/516825.html
上一篇:沒有多載函式“std::vector<_Ty,_Alloc>::erase[with_Ty=Enemy*,_Alloc=std::allocator<Enemy*>]”的實體與
下一篇:這個語法是什么“成功|=MAX35101_Read_2WordValue(TOF_DIFF_AVG_REG,&TOF_DIFF_Results->TOF_DiffData);”意思是?用C/C
