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

如何在不使用strcpy的情况下将字符数组复制到字符指针,c,arrays,pointers,c-strings,C,Arrays,Pointers,C Strings,在不手动使用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 thi

在不手动使用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->
}

我不知道如何解决这个问题。

所以现在发生的是更改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 \0 */
            else
                Strings[i][j] = temp[j];
        }
    }

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

    return 0;
}
#包括
#包括
#包括
int main()
{
int LEN=20;
int NUM=3;
字符*字符串[NUM];
半焦温度[LEN];
int i,j;

对于(i=0;i,在您的示例中,使用以下两行:

Strings[i] = malloc(strlen(temp)+1);    /* you should check return of malloc() */
Strings[i] = temp;
这是不正确的。第二行只是覆盖了从
malloc()
返回的指针。您需要使用from

char*strcpy(char*dest,const char*src)
将指向的字符串从
src
复制到
dest
dest
是目标,而
src
是要复制的字符串。返回指向
dest
的指针

您也没有检查<代码> fgScript()/<代码>的返回,它在失败时返回<代码> null <代码>。您还应该考虑删除< <代码> \n>代码>字符> <代码> fgScript()>代码>,因为您复制到<代码>字符串[i] <代码>的字符串将有一个尾随的换行符,这可能不是您想要的。

因为另一个答案显示了如何手动完成,您可能还想考虑使用它来为您拷贝。

strdup()
返回指向与string str重复的新字符串的指针。内存从
malloc()
获取,并使用从堆中释放

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

#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] = {'\0'};
    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] = '\0';
        } 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;
}
#包括
#包括
#包括
#定义len3
#定义大小20
内部主(空){
char*strings[LEN]={NULL};
字符缓冲区[BUFFSIZE]={'\0'};
尺寸,strcnt=0,i;
printf(“输入:\n”);
对于(i=0;i0&&buffer[slen-1]='\n'){
缓冲区[slen-1]='\0';
}否则{
fprintf(stderr,“输入的字符太多”\n);
退出(退出失败);
}
如果(*缓冲区){
字符串[strcnt]=strdup(缓冲区);
if(字符串[strcnt]==NULL){
fprintf(stderr,“无法分配缓冲\n”);
退出(退出失败);
}
strcnt++;
}
}
printf(“\n输出:\n”);
对于(i=0;i
strcpy有什么问题吗?使用
strdup
作为一个单行程序…
Strings[i]=temp;
覆盖您刚从
malloc
中获得的指针。您不能像这样复制C中的字符串。并且您会出现内存泄漏,因为现在无法
释放该指针。在循环中“手动”复制字符串是“经典”CS1练习。我建议你借任何介绍C的书,阅读关于字符串的章节。使用
strdup()
这是最简单的方法。您没有复制
'\0'
终止符,也没有检测并忽略使用
fgets
时通常出现的
换行符。谢谢!我不知道是否有其他方法不必逐字复制,但这很有效。@Robert应该是
的(j=0;j@Robert这确实是一个字母一个字母的复制。但是
j@WeatherVane谢谢你注意到这一点。但是是的,这确实是一封信一封信地抄写。
#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] = {'\0'};
    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] = '\0';
        } 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;
}