如何增加“ char *” *&quot在功能中没有返回?
我有这样的东西(简化):
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);
}
但是我真的不想要这个,因为我的计数函数已经返回了我需要的值。我可以做什么来增加函数内部的格式而不返回它?
I have something like this (simplified):
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);
}
Gives:
d 0x100003f8b
i 0x100003f94
d 0x100003f8b%
Only way to make it work is by doing:
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);
}
But I really don't want this since my count function is already returning a value that I need. What can I do to increment format inside the function without returning it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过引用将指针传递给函数。
在 C 中,通过引用传递意味着通过指向对象的指针间接传递对象。因此,取消引用指针后,您将可以直接访问原始对象并可以更改它。
例如
并调用如下函数
Pass the pointer to the function by reference.
In C passing by reference means passing an object indirectly through a pointer to it. So dereferencing the pointer you will have a direct access to the original object and can change it.
For example
and call the function like