如何在不使用 strcpy 的情况下将字符数组复制到 char 指针

How to copy an array of characters to a char pointer without using strcpy

如何在不手动使用 strcpy 的情况下将 char 数组的字符复制到 char 指针中。例如:

char *Strings[NUM];
char temp[LEN];
int i;
for (i = 0; i < NUM; i++){
    fgets(temp, LEN, stdin);
    Strings[i] = malloc(strlen(temp)+1);    
    Strings[i] = temp; // What would go here instead of this, 
                       // because this causes this to happen->
}
Input:
Hello
Whats up?
Nothing

Output (when the strings in the array of char pointers are printed):
Nothing
Nothing
Nothing

我不确定如何解决这个问题。

所以发生的事情是您更改了 temp 的值,但所有指针都指向 temp 的一个实例。您需要分配内存,然后手动复制数组。

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

int main()
{
    int LEN = 20;
    int NUM = 3;
    char* Strings[NUM];
    char temp[LEN];
    int i,j;

    for (i=0;i<NUM;i++){
        fgets(temp,LEN,stdin);
        Strings[i] = (char*)malloc(strlen(temp)+1);    

        for(j=0;j<=strlen(temp);j++) { /* this part */
            if (j == strlen(temp))
                Strings[i][j - 1] = temp[j]; /* overwrite \n with the terminating [=10=] */
            else
                Strings[i][j] = temp[j];
        }
    }

    for (i=0;i<NUM;i++)
        printf("%s\n", Strings[i]);

    return 0;
}

在您的示例中,您使用了这两行:

Strings[i] = malloc(strlen(temp)+1);    /* you should check return of malloc() */
Strings[i] = temp;

这是不正确的。第二行只是覆盖了从 malloc() 返回的指针。您需要改用 strcpy() from <string.h>:

Strings[i] = malloc(strlen(temp)+1);    
strcpy(Strings[i], temp);

char *strcpy(char *dest, const char *src) copies the string pointed to, from src to dest. dest is the destination, and src is the string to be copied. Returns a pointer to dest.

您也没有检查 fgets() 的 return,这 returns NULL 失败。您还应该考虑删除由 fgets() 附加的 \n 字符,因为您复制到 Strings[i] 的字符串将有一个尾随换行符,这可能不是您想要的。

由于另一个答案显示了如何手动进行,您可能还想考虑只使用 strdup() 来为您进行复制。

strdup() returns a pointer to a new string which is duplicate of string str. Memory is obtained from malloc(), and deallocated from the heap with free().

下面是一些进行额外错误检查的示例代码。

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

#define LEN 3
#define BUFFSIZE 20

int main(void) {
    char *strings[LEN] = {NULL};
    char buffer[BUFFSIZE] = {'[=12=]'};
    size_t slen, strcnt = 0, i;

    printf("Input:\n");
    for (i = 0; i < LEN; i++) {
        if (fgets(buffer, BUFFSIZE, stdin) == NULL) {
            fprintf(stderr, "Error from fgets()\n");
            exit(EXIT_FAILURE);
        }

        slen = strlen(buffer);
        if (slen > 0 && buffer[slen-1] == '\n') {
            buffer[slen-1] = '[=12=]';
        } else {
            fprintf(stderr, "Too many characters entered\n");
            exit(EXIT_FAILURE);
        }

        if (*buffer) {
            strings[strcnt] = strdup(buffer);
            if (strings[strcnt] == NULL) {
                fprintf(stderr, "Cannot allocate buffer\n");
                exit(EXIT_FAILURE);
            }
            strcnt++;
        }
    }

    printf("\nOutput:\n");
    for (i = 0; i < strcnt; i++) {
        printf("%s\n", strings[i]);
        free(strings[i]);
        strings[i] = NULL;
    }
    return 0;
}