I wrote a function which takes in a string and simply encodes the string (compresses if you will) the string in this format: input = aassda , output = 2a2s1d1a. My problem is if I input a string that is 47 characters or longer I get a seg fault. I attached the function bellow. I started C about a week ago so please bear with me =/
Edit: Any improvements suggestions would be nice also. I know I could make the code much more efficient but coming from a small background in python I decided to do it in the most direct way possible.
stringis just a junk pointer. It is never set and it is never associated with any memory. You either need tomalloc()some memory forstring(don’t forget tofree()it when you’re done!), or you need to make it an array. That is, your two choices are:Using
malloc()/free():Using arrays: