0

I defined a structure array_string which contains a character pointer and a integer denoting size. 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. 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));
}

2 Answers 2

1

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. Inside the struct we can see that you have pointer to char and an int. 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. The member that is char * will store the characters from a word that you introduce.

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.

words.arr = alloc(num);

That means what your variable words can store only 1 string with n letters. In order to store more words, you need to have an array of arr_string objects.

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

%s is mean enter string one times

and *(words.arr+i) is character not address of your array

scanf must pass pointer of array

%c is mean enter character 1 by 1

for you case scanf("%c ",&words.arr[i]); will key in 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;
}

If you want key in in one line must use %s like below

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

2 Comments

Failure to validate the return of scanf("%d ",&num); invites Undefined Behavior. Always validate each input, especially user-input, e.g. if (scanr("%d", &num) != 1) { /* handle error */ }.
@David C. Rankin sure OP need notice if someone key in not integer after execute work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.