我不确定我的标题是否正确,但基本上。我想知道是否有一种方法可以使用参数中的指针从hey函数添加到buff数组,如果有,为什么它会工作?
布夫[100]。
例子:

int main(){
  char buf[100];
  hey("320244",buf);
  printf("%s", buf);
}
  void hey(char* s, char* result){
  /*
  some code that appends to result using pointers
  do some stuff with s and get the result back in buf without using return.
  */
}

最佳答案

如果我正确地理解了你,你的意思是

#include <string.h>

//...

void hey(char* s, char* result)
{
    strcpy( result, s );
}

这是一个演示程序
#include <stdio.h>
#include <string.h>

void hey( const char* s, char* result);

int main(void)
{
    char buf[100];

    hey( "320244", buf );

    printf( "%s\n", buf );

    return 0;
}

void hey( const char* s, char* result )
{
    strcpy( result, s );
}

它的输出是
320244

如果数组buf已经存储了一个字符串,那么可以向它追加一个新字符串。例如
#include <string.h>

//...

char buf[100] = "ABC";

strcat( buf, "320244" );

考虑到函数hey应该在使用前声明,并且根据C标准,函数main应该声明如下
int main( void )

07-24 09:46
查看更多