Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I blank out a string? 1

Status
Not open for further replies.

RoyceyBaby

IS-IT--Management
Mar 23, 2001
22
Hi,

Simple question, I have

char remember[5000];

I assign a value to it.

Then I want to blank the string completly.

Who can I do this, it must be really obvious but I can't figure out how.

Many thanks,

Royce
 
Simple answer:
memset(remember,32,5000)...
This will fill your string with spaces... Nosferatu
We are what we eat...
There's no such thing as free meal...
 
Nosferatu,

Of course,
Code:
 memset(remember, 32, 5000)
will not terminate the string with a null. Any attempt to access the string will likely cause illegal memory access.

You should follow with
Code:
remember[5000-1] = '\0' ;

Brudnakm
 
define a function called as RESET() as follows

#define RESET(x) memset(x,0,sizeof(x))
#define REFILL(x) memset(x,' ',sizeof(x)-1)
#define ZFILL(x) memset(x,'0',sizeof(x)-1)

when u want to initialise a char variable/array in the program say

RESET(remember);

to blank it or empty the array say

REFILL(remember);

in case u want to fill the same with zeroes use the third syntax namely

ZFILL(remember);


try out if it works do add a note below.
this should do the job.

bestof luck

s-)




 
Assigning zero to char * may result in memory leak, after using free the memory saying free(remember) and when want to create that again use
remember = (char *) calloc(5000);

Correct me if I am wrong.
SwapSawe.
 
Right, but u can free it only if u use malloc() or calloc() or realloc() in the first place.

If u make it
char remember[5000];
u can't free it.
It will stay till its scope is lived (till the control is in the function it is declared in).

But using one of the allocation functions (like SwapSawe), right from the start would be better.

And Matt did not
remember = 0;
what he did was
*remember = 0;
equivalent to
remember[0] = 0;
equivalent to
remember[0] = '\0'; :) Ankan.

Please do correct me if I am wrong. s-)
 
Sorry to beat a dead horse but...

remember = "";

will NOT work. I think you are confusing the declaration of remember with assignments later on.

char remember[5000] = ""; WILL work but
remember = ""; should give you some sort of error.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top