简体   繁体   English

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

[英]Add a string array to a character pointer in c

I defined a structure array_string which contains a character pointer and a integer denoting size. 我定义了一个结构array_string,其中包含一个字符指针和一个表示大小的整数。 What I wanted to do was to store a user input string array in that pointer. 我想要做的是将用户输入的字符串数组存储在该指针中。 By default pointer cannot take in multiple string values so I tried to dynamically allocate space to pointer inorder to store multiple strings. 默认情况下,指针不能接受多个字符串值,因此我尝试为指针动态分配空间以存储多个字符串。 But it's still taking only 1 input. 但是它仍然只接受1个输入。 Can anyone please tell me how it's done? 谁能告诉我这是怎么做的? Code: 码:

#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)); scanf(“%s”,*(words.arr + i));

%s is mean enter string one times %s是一次输入字符串的平均值

and *(words.arr+i) is character not address of your array 和*(words.arr + i)是字符而不是数组的地址

scanf must pass pointer of array scanf必须传递数组的指针

%c is mean enter character 1 by 1 %c表示1乘1的字符

for you case scanf("%c ",&words.arr[i]); 为您案例scanf(“%c”,&words.arr [i]); will key in 1 by 1 将以1键入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;
}

If you want key in in one line must use %s like below 如果要在一行中键入,必须使用如下所示的%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 '\0' 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;
}

The reason why your program can read just one word is because you are creating the variable words that can store just 1 instance of the struct arr_string . 您的程序只能读取一个单词的原因是因为您正在创建仅存储1个 struct arr_string实例的变量words Inside the struct we can see that you have pointer to char and an int . 在struct内部,我们可以看到您有指向charint指针。 The int member should store the size of the word, but in your code, it store the numbers of words that you want to store witch is not correct. int成员应该存储单词的大小,但是在您的代码中,它存储您要存储的单词数不正确。 The member that is char * will store the characters from a word that you introduce. char *成员将存储您引入的单词中的字符。

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

you allocate n numbers of char for your words.arr member from the structure. 您从结构中为您的word.arr成员分配了n个字符。

words.arr = alloc(num);

That means what your variable words can store only 1 string with n letters. 这意味着您的可变词只能存储1个带有n个字母的字符串。 In order to store more words, you need to have an array of arr_string objects. 为了存储更多单词,您需要具有arr_string对象的数组。

Here you can find a solution to your problem: 在这里您可以找到解决问题的方法:

#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] != '\0')
            {
                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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM