this is ridiculous easy

It's been awhile and I seem to have forgotten some of the simplest things... le sigh

1
2
3
char handle[20]; // don't want the handle to get bigger than 20 chars
handle = NULL //won't work, can't convert into to char[20]
handle = "\0" //won't work, can't convert const char to char[20] 


i need to be able to 'reset' the handle to an empty state of some sort. this should be super easy.
nevermind.

*handle = NULL;
forgot to dereference. ahhh pointers :)
:)

handle is the address of handle[0];
Better still is either

1
2
#include <string.h>
memset( handle, 0, sizeof( handle ) );


or

1
2
#include <strings.h>    // strings, with an "s" at the end, not string.h
bzero( handle, sizeof( handle ) );


NULL is a (void) pointer which just happens to be zero, which is what you want, but there are no guarantees that NULL will always be zero.

as long as later on I can just check if *handle == NULL...
>> handle is the address of handle[0];
Actually, it has the same value but it is NOT the same type.
soldstatic, yo umight find the following articles useful...
http://www.cplusplus.com/forum/articles/10/
http://www.cplusplus.com/forum/articles/9/
http://www.cplusplus.com/forum/articles/8/

It would be useful to know what it is you are actually doing to ensure the answers given here are not misleading you. A handle is not normally an array, it is normally a pointer or opaque integer value, which represents a reference to another entity. This being the case, declaring you handle as an array would suggest incorrect semantics.

Maybe you meant to have the array as a buffer and the handle as a pointer that can point to the buffer? You could then set handle to NULL to disable it as necessary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

namespace {
	inline void test_handle(char * h)
	{
		std::cout << ((NULL == h) ? "handle disabled" : "handle enabled") << std::endl;
	}
}

int main()
{
	char buffer[20];

	char * handle = buffer;
	test_handle(handle);

	handle = NULL;
	test_handle(handle);
}


haha yea I've actually got another thread open where I'm talking with someone and this confused them too.

i'm writing a chat program, in my case i thought 'handle' instead of 'screen name'. so all i wanted was a character array. and i'll have an array of these screen names of a maximum size, and if there is a slot where the screen name is empty (or null) then that means a new person can connect and i would set that screen name to something else.

i'll have to do a find/replace on the word handle in my program though since its such a weird thing though lol
Ah ok, in that case just use a std::string :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

namespace {
	inline void test_handle(std::string const & user_handle)
	{
		std::cout << (user_handle.empty() ? "User disabled" : "User enabled") << std::endl;
	}
}

int main()
{
	std::string user_handle = "someuser";
	test_handle(user_handle);

	user_handle.clear();
	test_handle(user_handle);
}
trying to stick with old school char *
Topic archived. No new replies allowed.