Simpel question in C (tcshell) about strings and pointers

  • Thread starter Thread starter ydan87
  • Start date Start date
  • Tags Tags
    Pointers Strings
AI Thread Summary
To read a string from standard input in a C program, use a character array, such as char instring[40], to store the input. The fscanf function can be utilized to read the string with fscanf(stdin, "%s", instring), ensuring that the array size accommodates the maximum expected length. The string will be accessible at the address pointed to by the instring pointer. Proper memory management is crucial to avoid buffer overflow. This approach allows for string manipulation after capturing the input.
ydan87
Messages
25
Reaction score
0
If I need to write a c program which get a string from the stdin and prints it after a certain manipulation, the program is called that way:
Code:
echo "Hello, World. bla bla bla" | program <arg>
How can I save the string in my program before working on it?

Thanks in advance
 
Physics news on Phys.org
Try
Code:
char instring[40];
fscanf(stdin,"%s",instring):
where use 40 or whatever your max string length is. Your string is located at the address pointed to by the pointer instring.
 
Back
Top