Inputting to Char Arrays from Keyboard - Linux/GCC

In summary, the conversation is about a person asking for help with input in C. They want to take two lines of input from the keyboard and store them in two separate char arrays. They are not looking for a copy function, but rather a way to take the input and store it in the correct array. Pointers may be helpful for solving this problem. The person is planning to study on their own and will ask for help if needed.
  • #1
rambo3131
18
0
Hi,I have a question about C.I have declared two char arrays.I want to write one string through one line to take them first array.When ı touch enter and go to next line ,it will place my charecters which have been given later touching enter to second array.What can I do this? ,please help me .(.By the way I use linux and gcc compiler and ı must take inputs from keyboard not from file).
 
Technology news on Phys.org
  • #2
rambo3131 said:
Hi,I have a question about C.I have declared two char arrays.I want to write one string through one line to take them first array.When ı touch enter and go to next line ,it will place my charecters which have been given later touching enter to second array.What can I do this? ,please help me .(.By the way I use linux and gcc compiler and ı must take inputs from keyboard not from file).

It's not clear what you're asking.
"I want to write one string through one line to take them first array." - What does this mean?

"When ı touch enter and go to next line ,it will place my charecters which have been given later touching enter to second array." - What does this mean?
 
  • #3
ı mean : before ı clicked enter, i will write some charecters from keyboard .These must be taken first char array.Then, i will click enter then go to next line.Later i will write some charecters again,but this time these must be taken second char array .
 
  • #4
You can use scanf or gets for input. Do you know how to do input in C?
 
  • #5
I think he means he wants to copy the string from one array to the other before asking for another line of inputs. You can use strccpy() if these are zero terminated strings, or memcpy() if you know the lenghts of the strings.
 
  • #6
No ,ı don't mean copy.while taking input ı will write this:


abcdefg klmnfgftrgfdgdgdg dsdghbvd hhyjdsv mjkkdsdg dsrghjmd
bsxatujkolhfdsrfb fgg edfgshdh sdgsgdh gfsdgsg sgsgs ıluıuıl jyujtjdjyj


first line must be taken array 1,second line must be taken array 2.
 
  • #8
rambo3131 said:
No ,ı don't mean copy.while taking input ı will write this:


abcdefg klmnfgftrgfdgdgdg dsdghbvd hhyjdsv mjkkdsdg dsrghjmd
bsxatujkolhfdsrfb fgg edfgshdh sdgsgdh gfsdgsg sgsgs ıluıuıl jyujtjdjyj


first line must be taken array 1,second line must be taken array 2.

How much knowledge do you have about pointers?
 
  • #9
My sense is that rambo3131 is waiting for someone to come along and write his program for him.
 
  • #10
I don't know pointers well .Do you really think that i must know pointers to solve this problem?
 
  • #11
A little knowledge of pointers would be helpful.

As I said in post #4, you can use scanf or gets for input. Do you know how to do input in C?
 
  • #12
yes ,i will study for a while myself .If i have a problem,i ll ask .thank you
 

1. How do I input characters to a char array in Linux/GCC?

To input characters to a char array in Linux/GCC, you can use the scanf() function. This function allows you to specify the number of characters to be read and the address of the array where the characters will be stored.

2. Can I use fgets() to input characters to a char array in Linux/GCC?

Yes, you can use the fgets() function to input characters to a char array in Linux/GCC. This function reads characters from the standard input stream and stores them in the specified array. However, fgets() reads until a newline character is encountered, so you may need to use the strchr() function to remove the newline character from the input.

3. How do I handle input errors when using scanf() to input characters to a char array?

To handle input errors when using scanf() to input characters to a char array, you can check the return value of the function. If the return value is less than the specified number of characters to be read, an error has occurred. You can also use the feof() and ferror() functions to check for end-of-file and other error conditions, respectively.

4. Is there a limit to the number of characters I can input to a char array in Linux/GCC?

There may be a limit to the number of characters you can input to a char array in Linux/GCC, depending on the size of the array. If the input exceeds the size of the array, it may cause a buffer overflow, which can lead to unexpected behavior or security vulnerabilities. It is important to ensure that your input is within the boundaries of the array to avoid these issues.

5. Can I input special characters, such as spaces or punctuation, to a char array using scanf() in Linux/GCC?

Yes, you can input special characters, such as spaces or punctuation, to a char array using scanf() in Linux/GCC. However, you may need to specify the proper format specifier in your scanf() statement to read these characters correctly. For example, to read a string with spaces, you can use the %s format specifier instead of %c.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
11
Views
157
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
2
Replies
65
Views
5K
  • Programming and Computer Science
Replies
6
Views
976
Replies
3
Views
768
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
2
Views
377
  • Poll
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
Back
Top