Simpel question in C (tcshell) about strings and pointers

  • Thread starter Thread starter ydan87
  • Start date Start date
  • Tags Tags
    Pointers Strings
Click For Summary
SUMMARY

The discussion focuses on handling strings in C programming, specifically using the `fscanf` function to read input from standard input (stdin). A user seeks to manipulate a string received via a command line echo, and the solution involves declaring a character array, such as `char instring[40]`, to store the input string. The `fscanf` function is utilized to read the string, ensuring that the maximum length does not exceed the allocated array size. This method effectively captures the input for further manipulation.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with standard input/output functions in C
  • Knowledge of string manipulation techniques in C
  • Experience with memory management and array handling in C
NEXT STEPS
  • Explore the usage of `fgets` for safer string input handling in C
  • Learn about dynamic memory allocation using `malloc` for flexible string sizes
  • Investigate string manipulation functions in the C standard library, such as `strcpy` and `strcat`
  • Study buffer overflow vulnerabilities and how to prevent them in C programs
USEFUL FOR

C programmers, software developers, and students learning about string handling and memory management in C.

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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
8
Views
2K