改变指针adress在c

问题描述 投票:0回答:1
s

? 我的代码:

newstr

	
有任何方法可以在不制作新字符串的情况下做到这一点,或者有没有办法更改指针的指针地址指向

void str_trim(char *s) { // your code char newstr[200]; char *newstrp = newstr; while (*s != '\0') { if (*s != ' ') { *newstrp = *s; *newstrp++; *s++; } else if (*s == ' ') { *newstrp = *s; *newstrp++; *s++; while (*s == ' ') { s++; } } else { *newstrp = *s; *newstrp++; *s++; } } s = &newstr; }

没有将the的参数类型更改为
c string pointers
1个回答
0
投票
的情况下,无法更改呼叫者的变量。 因此,这给您留下了两种选择:

改变您的功能以返回新的
newstr
指针。 但是,您不能将指针返回到本地变量,这意味着您必须

s
新字符串,然后呼叫者必须

char**

char*
。  可能不是你想做的。

    ,只需完全摆脱本地
  • malloc()

    free()
    缓冲液即可修改
    char[]
    in-place
    的内容。  如果您完全无法更改
    s

    的签名,这是您唯一的选择。
  • trone this:

    str_trim()
    在线演示
        
    	
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.