I am trying to clear the first character of my array. The first two methods don't seem to work. The third method seems to but doesn't look like a good idea.

char operand[] = "abc";
//both methods seem to null it out
operand[0] = 0;
memset(operand, 0, 1);
printf("%s", operand);


//seems to work
memset(operand, 5, 1);

Recommended Answers

All 2 Replies

operand[0] = '';

When you say you want to clear the character, do you mean you want to insert a space character (' '), ASCII 0x20), or a null character (ASCII 0x00)? These are two different things with different consequences. If you insert a space,

operand[0] = '\0x20';

the string will be " bc", and the string length will still be 3, and the size of the array will still be 4 (you have to count the null delimiter).

Whereas if you insert a NULL,

operand[0] = '\0x0';

you will get "" (an empty string). The array will still have the same size, but the string it holds would be size zero.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.