我有這樣的事情(簡化):
void count(char *fmt)
{
while (*fmt != 'i')
{
fmt ;
}
printf("%c %p\n", *fmt, fmt);
}
int main(void)
{
char *a = "do something";
char *format;
format = a;
printf("%c %p\n", *format, format);
count(format);
printf("%c %p", *format, format);
}
給出:
d 0x100003f8b
i 0x100003f94
d 0x100003f8b%
使其發揮作用的唯一方法是:
char *count(char *fmt)
{
while (*fmt != 'i')
{
fmt ;
}
printf("%c %p\n", *fmt, fmt);
return (fmt);
}
int main(void)
{
char *a = "do something";
char *format;
format = a;
printf("%c %p\n", *format, format);
format = count(format);
printf("%c %p", *format, format);
}
但我真的不想要這個,因為我的 count 函式已經回傳了我需要的值。我可以做些什么來增加函式內部的格式而不回傳它?
uj5u.com熱心網友回復:
通過參考傳遞指向函式的指標。在 C 中,通過參考傳遞意味著通過指向物件的指標間接傳遞物件。因此,取消參考指標,您將可以直接訪問原始物件并可以更改它。
例如
void count(char **fmt)
{
while ( **fmt != 'i')
{
*fmt;
}
printf("%c %p\n", **fmt, *fmt);
}
并呼叫函式
count( &format);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/450296.html
上一篇:CORS問題-React/Axios前端和Golang后端
下一篇:在這種情況下我應該檢查指標嗎?
