0

我试图从我的 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;
}
4

5 回答 5

1

下面我的分析。从我在这里看到的内容来看,您应该了解指向 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;
}

我真的希望这会对你有所帮助。我很抱歉,但我不能写出解决方案,因为我认为如果你努力学习它会更好。你可以找到很多关于 char 指针的教程,我相信你会解决这个问题。

(输入读数)C scanf() 和 fgets() 问题

于 2013-03-15T03:51:21.033 回答
0
#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;
}
于 2013-03-15T05:18:18.770 回答
0

char *t未分配内存。您可以使用直接实现字符串复制strdup

char *newBuffer = NULL;
...
...
newBuffer = strdup(buffer);
于 2013-03-15T03:17:51.887 回答
0

尝试

char* newBuffer; // point to a c-string // here the return value of sCopy

static char nBuffer[256]; // make nBuffer survive beyond function call

并且只使用 n return nBuffer,而不是t.

这是您可以做到的一种方式,无需分配。或者分配内存并返回它而不是static char.

于 2013-03-15T03:18:07.523 回答
0

试试下面:

#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;
}

请注意,在输入字符串的末尾fgets附加 a ,因此如果您输入,则字符串是ASCII 代码的位置,并且是 的 ASCII 代码,要删除这个换行符,请参阅此链接new line character60x36 0x0a0x3660x0anew line character

于 2013-03-15T03:27:35.753 回答