2

I have char array to store string values. I wanted to store the value of a string variable into the char array.

char Password[30];
char User[2];
int i;

for(i=0; i<5; i++) {
        printf("Enter Password");
        scanf("%s", Password);
        strcpy(User[i],Password,30);
}

I wanted to input the values for the array and it should throw a buffer overflow but I couldn't do it. How can I do it?

0

1 Answer 1

4

This should work for you:

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

int main() {

    char Password[30];
    char User[5][30];
    int i;

    for(i = 0; i < 5; i++) {
        printf("Enter Password");
        scanf("%s", Password);
        strcpy(User[i],Password);
    }

    for(i = 0; i < 5; i++)
        printf("Password %d: %s\n", i+1, User[i]);

    return 0;

}

The second for loop is to show the output and that every thing is stored right!

Sign up to request clarification or add additional context in comments.

1 Comment

I have a doubt, char User[2][30]

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.