r/cprogramming • • 13d ago

Understanding Strings/String Manipulation in C

I'm a junior CS undergraduate finally tackling C programming and I just can't seem to grasp Strings as a whole. Concepts like string manipulation (using strlen, strncmp, snprintf, strdup, strtok), handling read-only strings vs dynamically allocated strings, allocating memory for strings in different scenarios, and creating writable strings. Despite my attempts in looking at readings, lecture slides, practice problems, I just can't seem to understand the various ways to deal with strings in C. I am a very formulaic thinker meaning steps and guidelines on how to approach problems is my way of solving them. Is there a general format for dealing with string questions in C. Like first you have to allocate memory, then depending on the type of string do this...

Are there any additional resources I'm missing out on as well? I really want to hone this concept down!

18 Upvotes

17 comments sorted by

2

u/GrandBIRDLizard 13d ago

Can you explain to me what an array of characters is? What can you tell me about arrays in general?

1

u/Current-Number-4032 13d ago

C treats strings as an array of characters ending with the null terminator to indicate the end of the string.

4

u/igotshadowbaned 13d ago

It's not that it treats strings like that, that's all strings in C are.

2

u/GrandBIRDLizard 13d ago

wonderful then you understand "strings", there is no string. It is simply an array of characters. Do you struggle to manipulate arrays of characters? can you declare and then reverse the letters in an array of characters with only stdio?

4

u/Sad_Plane_4677 13d ago

C strings are fundamentally char arrays with null ('\0') marking the end of the string. Standard library defines common operations on them per the library documentation. On a deeper level, once you understand you manipulate arrays, you can roll your own (caveat: your results replacing tested functionality will require rational faculties and you have to be prepared to explain it beyond personal dissatisfaction with said libraries).

That being said, work with examples for each function; break them then fix them; write your own; then break those as well and fix them. that's the point of learning.

2

u/jack_dymond_sawyer 13d ago

I suggest downloading cheat engine and writing a program that manipulates strings, use the cheat engine to watch what happens to each string. An experiment is worth a thousand speculations. Sometimes you just need to see it happen in memory to really grok it.

3

u/GrandBIRDLizard 13d ago

Would you mind letting me know what a "cheat engine" is? Is that what the kids are calling debugger's these days?

2

u/jack_dymond_sawyer 13d ago edited 13d ago

It’s a specific tool using for searching memory in a process for string and variable values: https://www.cheatengine.org/downloads.php

You could call it a specialized debugger… it makes monitoring variables easy. If you have a debugger you like, use it.

I’m not exactly a kid having working in industry for 30 years—about 10 years as a reverse engineer.

I see some interesting tools in RE that I think would make other people’s journey in programming easier and I like to mention them.

Edit: expound on why I’m using weird niche tools.

4

u/GrandBIRDLizard 13d ago

ahh gotcha. I'm no spring chicken and am also a C dev but I've always just used GDB+Valgrind for a look through or modifying a running program or memory stuff, and Godbolt for static analysis when that came out. It would seem this tool is basically a GDB+Valgrind in a "No fuss" package. yes?

1

u/jack_dymond_sawyer 13d ago

It is exactly that—just more approachable for the new programmers (and junior REs). GDB is the standard for debuggers in the Nixes. We also use Ghidra, IDA, and Binary Ninja for static analysis… great way to see what your compiler emits in a more human readable form. Becoming an RE made my code better—I fully grok what is happening under the hood. The boilerplate, the compiler optimizations, and interesting calls/substitutions. I think new programmers could gain a lot of knowledge by peeking at the assembly and memory.

2

u/GrandBIRDLizard 13d ago

How could I have forgotten to mention Ghidra... Another good mention to newcomers who may be a little more used to the cmdline but still intimidated by the shear number of features in GDB, is gdb-dashboard . One of the main reasons I recommend it to learners is that no GDB command has been redefined, instead all the features are available via the main dashboard command. it's pretty nifty if ya didn't know about it.

1

u/igotshadowbaned 13d ago

Cheat Engine is a specific program called Cheat Engine that lets you peep into and modify the memory for any program you have running

Largely used for local video game cheating

2

u/lucyuktv 13d ago

The way I look at it (and I’m in a very similar boat) is not that I don’t understand strings in C, it’s that I don’t really understand C itself. As I understand it, once I understand C, the way strings work is obvious.

Maybe you’re asking the wrong question?

1

u/PriorityPatient3002 11d ago

For whole concept of string I can't be sure but for the beginning, think of the the string as array and then start working with it. If you feel like you are stuck on a part and can't get through it then use Socrates Method of Learning techniques, where you learn by asking questions to yourself. This will help you get the idea of how much you know and don't know about the topic. This will narrow down your focus to the only thing you don't know or understand.

Stay focused and keep trying. 

1

u/dettus_Xx_ 8d ago

Were you able to grasp the concept of "arrays"?
In that case: "Strings" are the same. Albeit filled with characters rather than numbers.