gpt4 book ai didi

C - 如何通过函数增加指向字符串的指针?

转载 作者:行者123 更新时间:2023-12-05 08:49:12 25 4
gpt4 key购买 nike

我想通过一个函数来执行下面的代码。

    #include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
char* string = "hello_World";
string++;
printf("%s\n", string);
// ignore memory leak for sake of quiz
return 0;
}

输出:

ello_World

现在,我尝试将指针的引用传递给一个函数,然后递增它,而不是在 main 中递增指针。

    #include <stdio.h>
#include <string.h>
#include <stdlib.h>
void myfunc(char** param){
param++;
}

int main(){
char* string = "hello_World";
myfunc(&string);
printf("%s\n", string);
return 0;
}

但输出是 hello_World

能否请您告诉我我编写的代码中发生了什么以及如何更改它以获得所需的结果?

最佳答案

您将指针递增到指针,您需要 (*param)++;

关于C - 如何通过函数增加指向字符串的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64354819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com