Please help me understand c-style strings!!

OK, so I am just TERRIBLE with C-style strings and I know it, so I apologize in advance. Here is my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <stdio.h>

int main()
{
	const char str1[] = "nit";
	const char str2[] = "wit";

	char str3[10];
	strcpy(str3, str1); 
	strcat(str3, str2);
	printf("str3: '%s'\n", str3);

	std::cin.get();
	return 0;
}

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. const int 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.
What do you mean it 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:

1
2
template <typename T,unsigned S>
inline unsigned countof(const T (&v)[S]) { return S; }


Does the same thing as sizeof in this case, but unlike sizeof it will yield a compiler error if used incorrectly.
If someone is struggling to understand C strings, you can't really expect them to understand when sizeof will and won't work.
I think most of the time, if one is struggling to understand C strings, he/she will be much more struggling to understand templates.
It's black box copypasta. They don't have to understand how it works, just that it works.
If you want a deeper understanding of c-style string take assembly.
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <iostream>

int main()
{
	const int ARRAY_MAX = 1;

	const char str1[] = "nit";
	const char str2[] = "wit";

	char str3[ARRAY_MAX];

	if (strlen(str3) > 0)
	{
		printf("strlen(str3) = %d\n", strlen(str3));
		printf("str3: '%s'\n", str3);
	}
	else
	{
		printf("str3 is empty");
	}
	std::cin.get();

	return 0;
}


So in the above code when I run it I get strlen(str3) = 12. Where does that come from? Shouldn't it be zero since I never put anything into it?



Where does that come from? Shouldn't it be zero since I never put anything into it?
It's an uninitilized array with random content. So if you're lucky there's at least one 0 in it.

It should be char str3[ARRAY_MAX] = { 0 };

There's the function strncpy http://www.cplusplus.com/reference/clibrary/cstring/strncpy/

1
2
strncpy(str3, str2, ARRAY_MAX - 1);
str3[ARRAY_MAX - 1] = 0; // This ensures that the string is always 0 terminated! 
Last edited on
Topic archived. No new replies allowed.