I have to constantly be aware of the capacity of str3 and make absolutely positively sure that the size of str1 + the size of str2 will never be >= 10. So here are my questions:
1. Is there an if statement I can use to check the capacity of str3 beforehand and only run the strcpy or strcat if size/capacity allows?
2. I know that instead of "10" I can put a #define macro called, say, ARRMAX up at the top and just use that. However, I've read repeatedly that #defines are deprecated nowadays and are to be avoided. What can I use instead? I tried "const int ARRMAX = 20;" and that didn't work.
1. You can use strlen() if you don't have the size of the array as a constant somewhere.
2. constint ARRMAX = 20; should work. Could you post some code that just allocates an array using such a constant and tell us what the error is?
1. Is there an if statement I can use to check the capacity of str3 beforehand and only run the strcpy or strcat if size/capacity allows?
In your example, use sizeof(str3) to get its size.
However, I've read repeatedly that #defines are deprecated nowadays and are to be avoided.
I don't think #define will be deprecated, but I believe it's encouraged to avoid using it if there is other way. Actually #define is still powerful if one really knows well about it and how to use it.
I tried "const int ARRMAX = 20;" and that didn't work.
In your example, use sizeof(str3) to get its size.
I don't recommend using sizeof for this as it can lead to incorrect results if the buffer is dynamically allocated, or passed to a function, etc. If someone is struggling to understand C strings, you can't really expect them to understand when sizeof will and won't work. So to play it safe, I'd just say don't use it at all.
There are safer alternatives to sizeof anyway. Like the common 'countof' function:
Thank you all for your help. Re: #2 I was mistaken, yes "const int ARRMAX = 20;" works just fine. But I am still not getting part one quite yet. Here is my code: