Simpel question in C (tcshell) about strings and pointers

In summary, a string in C is a sequence of characters stored in consecutive memory locations, while a pointer is a variable that stores the memory address of another variable or data type. Strings can be declared and initialized using the <code>char</code> keyword and double quotes, and their value can be changed using pointers and functions such as <code>strcpy()</code>. The length of a string can be found using the <code>strlen()</code> function, and a character array is a collection of characters while a string is a character array terminated by a null character.
  • #1
ydan87
25
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
  • #2
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.
 

1. What is the difference between a string and a pointer in C?

A string in C is a sequence of characters stored in consecutive memory locations, with a null character (\0) marking the end of the string. A pointer, on the other hand, is a variable that stores the memory address of another variable or data type. In C, strings are often represented as arrays of characters, with the array name acting as a pointer to the first character in the string.

2. How do you declare and initialize a string in C?

A string can be declared and initialized in C using the char keyword and enclosing the characters within double quotes. For example: char str[] = "Hello World"; This creates an array of characters with the name "str" and initializes it with the string "Hello World".

3. Can you change the value of a string in C?

In C, strings are typically treated as constant arrays and cannot be modified directly. However, by using pointers, it is possible to change the value of a string. For example, you can use the strcpy() function to copy a new string into the memory location of an existing string.

4. How do you find the length of a string in C?

The strlen() function can be used to find the length of a string in C. It takes a string as an argument and returns the number of characters in the string, not including the null character at the end.

5. What is the difference between a character array and a string in C?

In C, a character array is a collection of characters stored in consecutive memory locations, while a string is a character array that is terminated by a null character (\0). This null character indicates the end of the string and allows C to determine the length of the string. Additionally, character arrays can store any type of character, while strings are typically used to store printable characters only.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
942
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Programming and Computer Science
Replies
5
Views
881
  • Engineering and Comp Sci Homework Help
Replies
3
Views
940
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
10
Views
2K
Back
Top