0

For my final project, I decided to write a C program that conjugates German verbs. Overall, it's gone very, very well, and the program can handle quite a bit. Everything was working until I implemented a feature to deal with prefixed verbs. The feature essentially removes the prefix and passes the rest of the verb onto the remainder of the program. The prefix is then added back in at the end.

However, a design choice very, VERY early on has reared it's ugly head.

I have the program split across several different .c files with a single header file linking it all together. The main purpose of those extra files is to clean up the main file and pass back the various verb forms required. But when I declared, for example, the conjugated verb form in the file that has all the functions dealing with conjugation, clang gave me an error that the conjugated verb variable declared in that second file was not, in fact, declared. I dealt with this by, instead, declaring it in the header, but because it's a string, I needed to give it a size. That early on, there was no way I could determine how big it needed to be, so I declared it as char conjverb[1]. Later, I was forced to do the same with the prefix variable, but now I'm faced with the problem I was hoping I would never see: my prefix variable is being overwritten by conjverb.

Is there some other way I can deal with these string declarations in my program to declare them when I need them instead of declaring them WAY in advance? I need the variables to be able to move from function to function and from file to file.

Thanks in advance!

0

When you declare a string or char array, you have to give it a size, which cannot be changed later. In your case, you gave it a size of 1. The problem is that c doesn't prevent you from reading and writing beyond the end of the memory allocated to the string. It also sounds like your prefix variable and converb are adjacent to each other, so one overwrites the other.

It seems to me that the simple remedy would be to just declare each of these with a size that is far larger than anything that will be loaded into them. Remember, you can have extra space on the end of the string inside the variable because the \0 end of string marker will tell your code how long the string really is. I don't know or speak german, but I'd guess that if you sized each to 20 or so, it would resolve the problem.

Of course, I could be totally wrong, but if this answers your question, please click on the check to accept it. Let's keep up on forum maintenance. ;-)

1
  • Yeah, that was the simplest answer I had come up with too, but it seemed so, for lack of a better word, medieval. If I translated the program into PHP, would I avoid this issue entirely? – Chris Nov 5 '15 at 1:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .