将字符串数组添加到c中的字符指针

Add a string array to a character pointer in c

我定义了一个结构array_string,它包含一个字符指针和一个表示大小的整数。我想做的是在该指针中存储一个用户输入字符串数组。默认情况下,指针不能接受多个字符串值,所以我尝试动态分配 space 给指针以存储多个字符串。但它仍然只需要 1 个输入。谁能告诉我这是怎么做到的? 代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
struct arr_string{
    char *arr;
    int size;
};
char* alloc(int num){
    char *temp = (char*)malloc(num*sizeof(char));
    return temp;
}
void main(){
    struct arr_string words;
    int num;
    printf("Enter no. of words: ");
    scanf("%d",&num);
    words.arr = alloc(num);
    words.size = num;
    for(int i=0;i<num;i++)
        scanf("%s",*(words.arr+i));
}

scanf("%s",*(words.arr+i));

%s 表示输入字符串一次

and *(words.arr+i) 是字符而不是数组的地址

scanf 必须传递数组指针

%c表示输入字符1乘以1

为你案例 scanf("%c ",&words.arr[i]);将输入 1 by 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
struct arr_string{
    char *arr;
    int size;
};
char* alloc(int num){
    char *temp = (char*)malloc(num*sizeof(char));
    return temp;
}
int main(){
    struct arr_string words;
    int num,i;
    printf("Enter no. of words: ");
    scanf("%d ",&num);
    words.arr = alloc(num);
    words.size = num;
    for(i=0;i<words.size-1;i++){
        scanf("%c ",&words.arr[i]);
    }
    for(;i<words.size;i++){
        scanf("%c",&words.arr[i]);
    }
    for(i=0;i<words.size;i++){
        printf("your %d is:%c \n",i,words.arr[i]);
    }
    //alloc need free back to system
    free(words.arr);

    return 0;
}

如果你想在一行中输入必须使用如下的 %s

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
struct arr_string{
    char *arr;
    int size;
};
char* alloc(int num){
    char *temp = (char*)malloc(num*sizeof(char));
    return temp;
}
int main(){
    struct arr_string words;
    int num;
    printf("Enter no. of words: ");
    scanf("%d ",&num);
    words.arr = alloc(num+1);//remember if you string printf by %s last one character must be '[=11=]' so need one more space
    words.size = num;
    scanf("%s",words.arr);

    printf("your string is:%s \n",words.arr);
    //alloc need free back to system
    free(words.arr);

    return 0;
}

您的程序只能读取一个单词的原因是因为您正在创建变量 words,它只能存储 1 struct arr_string 的实例.在结构内部,我们可以看到您有指向 charint 的指针。 int 成员应该存储单词的大小,但在您的代码中,它存储了您要存储的单词数 是不正确的。 char * 成员将存储您介绍的单词中的字符。

char* alloc(int num){
    char *temp = (char*)malloc(num*sizeof(char));
    return temp;
}

您从结构中为您的 words.arr 成员分配了 n 个字符。

words.arr = alloc(num);

这意味着你的变量词只能存储1个字符串n个字母。 为了存储更多的单词,您需要有一个 arr_string 个对象的数组。

您可以在这里找到问题的解决方案:

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

struct arr_string{
    char *arr;
    int size;
};
char* alloc(int num){
    char *temp = (char*)malloc(num*sizeof(char));
    return temp;
}
int main(){
    struct arr_string *words;
    int num;
    int i,j;
    int letters;
    char *buff;

    printf("Enter no. of words: ");
    scanf("%d",&num);

    /*here it creates an array of arr_string objects*/
    words = (struct arr_string*)malloc(num*sizeof(struct arr_string));

    for(i=0;i<num;++i)
    {
        letters=0;
        /*alloc some memory for the buffer in witch you store the word entered from keyboad*/
        buff=(char *)malloc(sizeof(char)*40);
        scanf("%s",buff);

        /*seach how many letters are in the word*/
        for(j=0;j<40;++j)
        {
            if(buff[j] != '[=12=]')
            {
                letters++;
            }
            else
            {
                break;
            }
        }

        /*set the value of size with the numbers of letters*/
        words[i].size=letters;

        /*alloc necessar memory for each word*/
        words[i].arr=(char *)malloc(sizeof(char)*(words[i].size+1));

        /*realloc memory so the word can be copied from the buffer*/
        realloc(buff,sizeof(char)*(letters+1));

        strcpy(words[i].arr,buff);
        free(buff);
    }

    for(i=0;i<num;++i)
    {
        printf("%s ",words[i].arr);
    }


    for(i=0;i<num;++i)
    {
        free(words[i].arr);
    }

    free(words);
    return 0;
}