2

I'm wondering if anyone could help me out with the following code fragment. What I'm trying to figure out is how to store one array in another. I've tried everything I could think of yet all resulted in errors from the compiler. The following is a fragment from my code that should be enough show you where I stand:

char min[20], max[20], input[20] ;
printf("Enter word: ");
scanf("%s", &input);
min = input; max = input;
2
  • You should drop the &, or use fgets. And loop through to copy arrays. Commented Apr 15, 2013 at 4:43
  • As @squiguy says, plus in C you copy strings with strcpy. You can easily look that up. Commented Apr 15, 2013 at 4:44

4 Answers 4

3
char min[20], max[20], input[20] ;
printf("Enter word: ");
scanf("%s", input);
strcpy(min, input);
strcpy(max, input);

This is how you do it. Note that I have removed the & in the scanf also.

scanf is not a good function to use - http://c-faq.com/stdio/scanfprobs.html

#include <string.h> to get the declarations for strcpy.

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

1 Comment

Thanks, I've used this function before but it slipped my mind. Especially since I'm currently working on various projects in C, Java and Python. Also, this is for an intro C course where the instructor encourages us to use scanf for simplicity but thanks for showing me issues with it for future reference.
2

I think you have to copy input into max and min array. So code should be

char min[20], max[20], input[20] ;
printf("Enter word: ");
scanf("%s", input);
strcpy(min,input);
strcpy(max,input);

Comments

1

memcpy is your friend:

char min[20], max[20], input[20];
memset(min,'d',19);
min[19] = 0;
memcpy(min,max,20);

Comments

0

You should try to copy the string.

strncpy(input, min, sizeof(min)-1);
strncpy(input, max, sizeof(max)-1);
//to be careful
min[sizeof(min)-1] = '\0';
max[sizeof(max)-1] = '\0';

Comments

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.