使用参数从C中的函数返回字符串

使用参数从C中的函数返回字符串,c,C,我试图从我的sCopy()函数返回一个字符串,这样我就可以在代码的main()函数中打印它。请帮忙 const char *sCopy(char buffer[256], int i); int main() { int i; int x; char buffer[256]; char newBuffer;//[256]; printf("Please enter a number: "); fgets(buffer, 256, stdin);

我试图从我的sCopy()函数返回一个字符串,这样我就可以在代码的main()函数中打印它。请帮忙

const char *sCopy(char buffer[256], int i);

int main() {
    int i;
    int x;
    char buffer[256];
    char newBuffer;//[256];
    printf("Please enter a number: ");
    fgets(buffer, 256, stdin);
    i = atoi(buffer);

printf("The value you entered is %d.  Its double is %d.\n", i, i*2);
newBuffer = sCopy(buffer, i);  
printf(newBuffer);    
return 0;
}   

const char *sCopy(char buffer[256], int i){
    char nBuffer[256];
    char *t;        
    int x;
    for(x = 0; x < i; x++){
        strcat(t, buffer);
    }
    //t = nBuffer;
    return t;
}
const char*scope(字符缓冲区[256],int i);
int main(){
int i;
int x;
字符缓冲区[256];
char newBuffer;//[256];
printf(“请输入一个数字:”);
fgets(缓冲区,256,标准输入);
i=atoi(缓冲区);
printf(“您输入的值是%d。它的双精度值是%d。\n”,i,i*2);
newBuffer=1(缓冲区,i);
printf(newBuffer);
返回0;
}   
常量字符*1(字符缓冲区[256],整数i){
char-nBuffer[256];
char*t;
int x;
对于(x=0;x
未分配
字符*t
的内存。您可以使用
strdup
直接实现字符串复制

char *newBuffer = NULL;
...
...
newBuffer = strdup(buffer);
试一试

只需使用n return
nBuffer
,而不是
t

这是一种不用分配的方法。或者,分配内存并返回它,而不是
静态字符

尝试以下操作:

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

void sCopy(char buffer[256], int i, char newBuffer[], int size)
{
    char *t;        
    for(int x = 0; x < i; x++){
      strcat(newBuffer, buffer);
     }
 }

int _tmain(int argc, _TCHAR* argv[])
{
   int i;
   char buffer[256] = {0};
   char newBuffer[256] = {0};
   printf("Please enter a number: ");
   fgets(buffer, 256, stdin);
   i = atoi(buffer);

   printf("The value you entered is %d.  Its double is %d.\n", i, i*2); 
   sCopy(buffer, i, newBuffer, 256);
   printf("%s", newBuffer);   
   return 0;
}
#包括
#包括
#包括
无效缓冲区(字符缓冲区[256],整数i,字符新缓冲区[],整数大小)
{
char*t;
对于(int x=0;x

请注意,
fgets
在输入字符串的末尾追加了一个
新行字符
,因此如果您输入
6
,则字符串为
0x36 0x0a
,其中
0x36
6
的ASCII代码,
0x0a
新行字符
的ASCII代码,要删除此新行字符,请参阅下面的我的分析。根据我在这里看到的内容,您应该了解指向char(char*)的指针和char数组之间的区别。不过我真的很感激你在问之前自己解决了这个问题

const char *sCopy(char buffer[256], int i); 
/* let's start from here, what i represents? Keep in mind that the most of the   */
/* time an external developer will see just your declaration of a method, always */
/* try to give significant names to variables.                                   */

int main() {
    int i = 0;
    /* always initialize variables to default values, especially if they are     */
    /* going to be indexes in a buffer.                                          */  

    int x = 0;
    char buffer[256] = "";
    /* you can even initialize this to "" in order to mimic an empty string,     */
    /* that is a char array cointining just \0 (string null-terminator).         */

    char newBuffer[256] = "";
    /* same here, you always need to declare the size of a char array unless     */ 
    /* you initialize it like this -char newBuffer[] = "hello"-, in which case   */
    /* the size will automatically be 6 (I'll let you discover/understand */
    /* why 6 and not 5).                                                         */

    printf("Please enter a number: ");
    fgets(buffer, 256, stdin); // nice link at the bottom on input reading
    i = atoi(buffer);

    printf("The value you entered is %d.  Its double is %d.\n", i, i*2);
    newBuffer = sCopy(buffer, i);  
    printf(newBuffer);    
    return 0;
}   

/* I am not going to debate the idea of returning a char pointer here :)           */
/* Remember that in this case you are returning a pointer to some memory that has  */
/* been allocated somewhere inside your function and needs to be released (freed)  */
/* by someone outside your control. Are they going to remember it? Are they        */
/* going to do it? In this case "they" is "you" of course.                         */
/* I'll let you explore alternative approaches.                                    */

const char *sCopy(char buffer[256], int i){
    char nBuffer[256] = ""; // see above
    char *t = NULL;
    /* you always init a pointer to NULL. As you can see, initializing here will */
    /* make you think that there might be problem with the strcat below.           */

    int x; // ok here you can omit the init, but be aware of it.
    for(x = 0; x < i; x++){
        strcat(t, buffer);

    /* what are you missing here? this piece of code is supposed to concatenate the */
    /* input buffer to a brand new buffer, pointed by your variable t. In your implementation */
    /* t is just a pointer, which is nothing more than a number to a memory location */
    /* With the initialization, the memory location you are pointing to is NULL, which */
    /* if de-referenced, will cause massive problems.                                */
    /* What you are missing is the blank slate where to write your data, to which your */ 
    /* pointer will read from.                                                        */
    }
    //t = nBuffer;
   return t;
}
const char*scope(字符缓冲区[256],int i);
/*让我们从这里开始,我代表什么?请记住,大多数*/
/*外部开发人员看到您的方法声明的时间总是*/
/*尝试为变量指定重要名称*/
int main(){
int i=0;
/*始终将变量初始化为默认值,尤其是在*/
/*将成为缓冲区中的索引。*/
int x=0;
字符缓冲区[256]=“”;
/*您甚至可以将其初始化为“”,以模拟空字符串*/
/*这是一个仅与\0(字符串空终止符)匹配的字符数组*/
char newBuffer[256]=“”;
/*同样,您始终需要声明字符数组的大小,除非*/
/*您可以这样初始化它-char newBuffer[]=“hello”-,在这种情况下*/
/*尺寸将自动为6(我会让您发现/了解*/
/*为什么是6而不是5)*/
printf(“请输入一个数字:”);
fgets(buffer,256,stdin);//输入读取时底部的链接很好
i=atoi(缓冲区);
printf(“您输入的值是%d。它的双精度值是%d。\n”,i,i*2);
newBuffer=1(缓冲区,i);
printf(newBuffer);
返回0;
}   
/*我不打算在这里讨论返回char指针的想法:)*/
/*请记住,在这种情况下,您将返回一个指向某个内存的指针*/
/*已分配到函数中的某个位置,需要释放(释放)*/
/*你无法控制的人。他们会记得吗?是吗*/
/*你打算怎么做?在这种情况下,“他们”当然就是“你”*/
/*我将让您探索其他方法*/
常量字符*1(字符缓冲区[256],整数i){
char nBuffer[256]=“”;//参见上文
char*t=NULL;
/*您总是初始化一个指向NULL的指针*/
/*让您认为下面的strcat可能有问题*/
int x;//这里可以省略init,但要注意它。
对于(x=0;x
我真的希望这能对你有所帮助。很抱歉,我不能写解决方案,因为我认为如果你通过艰苦的方式学习会更好。你可以找到很多指向char的指针的教程,我相信你会解决这个问题

(输入读数)

#包括
#包括
#包括
常量字符*1(字符缓冲区[256],整数i);
int main(){
int i;
int x;
字符缓冲区[256];
常量字符*newBuffer;//[256];
printf(“请输入一个数字:”);
fgets(缓冲区,256,标准输入);
i=atoi(缓冲区);
printf(“您输入的值是%d。它的双精度值是%d。\n”,i,i*2);
newBuffer=1(缓冲区,i);//缓冲区尾部'\n'需要切割吗?
printf(newBuffer);
免费(纽伯弗);
返回0;
}   
常量字符*1(字符缓冲区[256],整数i){
中国
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

void sCopy(char buffer[256], int i, char newBuffer[], int size)
{
    char *t;        
    for(int x = 0; x < i; x++){
      strcat(newBuffer, buffer);
     }
 }

int _tmain(int argc, _TCHAR* argv[])
{
   int i;
   char buffer[256] = {0};
   char newBuffer[256] = {0};
   printf("Please enter a number: ");
   fgets(buffer, 256, stdin);
   i = atoi(buffer);

   printf("The value you entered is %d.  Its double is %d.\n", i, i*2); 
   sCopy(buffer, i, newBuffer, 256);
   printf("%s", newBuffer);   
   return 0;
}
const char *sCopy(char buffer[256], int i); 
/* let's start from here, what i represents? Keep in mind that the most of the   */
/* time an external developer will see just your declaration of a method, always */
/* try to give significant names to variables.                                   */

int main() {
    int i = 0;
    /* always initialize variables to default values, especially if they are     */
    /* going to be indexes in a buffer.                                          */  

    int x = 0;
    char buffer[256] = "";
    /* you can even initialize this to "" in order to mimic an empty string,     */
    /* that is a char array cointining just \0 (string null-terminator).         */

    char newBuffer[256] = "";
    /* same here, you always need to declare the size of a char array unless     */ 
    /* you initialize it like this -char newBuffer[] = "hello"-, in which case   */
    /* the size will automatically be 6 (I'll let you discover/understand */
    /* why 6 and not 5).                                                         */

    printf("Please enter a number: ");
    fgets(buffer, 256, stdin); // nice link at the bottom on input reading
    i = atoi(buffer);

    printf("The value you entered is %d.  Its double is %d.\n", i, i*2);
    newBuffer = sCopy(buffer, i);  
    printf(newBuffer);    
    return 0;
}   

/* I am not going to debate the idea of returning a char pointer here :)           */
/* Remember that in this case you are returning a pointer to some memory that has  */
/* been allocated somewhere inside your function and needs to be released (freed)  */
/* by someone outside your control. Are they going to remember it? Are they        */
/* going to do it? In this case "they" is "you" of course.                         */
/* I'll let you explore alternative approaches.                                    */

const char *sCopy(char buffer[256], int i){
    char nBuffer[256] = ""; // see above
    char *t = NULL;
    /* you always init a pointer to NULL. As you can see, initializing here will */
    /* make you think that there might be problem with the strcat below.           */

    int x; // ok here you can omit the init, but be aware of it.
    for(x = 0; x < i; x++){
        strcat(t, buffer);

    /* what are you missing here? this piece of code is supposed to concatenate the */
    /* input buffer to a brand new buffer, pointed by your variable t. In your implementation */
    /* t is just a pointer, which is nothing more than a number to a memory location */
    /* With the initialization, the memory location you are pointing to is NULL, which */
    /* if de-referenced, will cause massive problems.                                */
    /* What you are missing is the blank slate where to write your data, to which your */ 
    /* pointer will read from.                                                        */
    }
    //t = nBuffer;
   return t;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char *sCopy(char buffer[256], int i);

int main() {
    int i;
    int x;
    char buffer[256];
    const char* newBuffer;//[256];
    printf("Please enter a number: ");
    fgets(buffer, 256, stdin);
    i = atoi(buffer);

printf("The value you entered is %d.  Its double is %d.\n", i, i*2);
newBuffer = sCopy(buffer, i);  //buffer tail '\n' need cut?
printf(newBuffer);

    free(newBuffer);
    return 0;
}   

const char *sCopy(char buffer[256], int i){
    char *t;        
    int x;

    t=(char*)malloc(strlen(buffer)*i + 1);
    *t='\0';
    for(x = 0; x < i; x++){
        strcat(t, buffer);
    }
    //t = nBuffer;
    return (const char*)t;
}