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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to free a *char variable

Status
Not open for further replies.

nitroprog

Technical User
Jun 18, 2003
4
PT
Hello,

i have the following code

char *ficheiro;
.
.
i and i use the "ficheiro" variable. I would like to know how to clean this variable, so i can use it again with no value.

is it ficheiro=NULL;
or ficheiro="";

I have used this, but i havent had the results i need.

Thank you
 
iam still not clear whether u want to clear the pointer or
the address it references it to.

>I would like to know how to clean this variable, so i can >use it again with no value
If the variable does not have any value how can u use it ?

ficheiro=NULL; --> This makes the pointer ficheiro , point to NULL. so the memory location still exists.


ficheiro=""; --> OOPS! This does not achieve the intended.

To free a memory location that you have allocated,
use free(ficheiro);

To re-init it with some default value
*ficheiro = 'x'
'x': some value...

I hope this is of some help to you.

-vs
 
It depends on how you intend to use it. If you want to point it at allocated storage:

char *ficheiro;

ficheiro = malloc(sizeof "hello");
strcpy(fichiero, "hello");
free(fichiero); /* this "uninitializes variable */
fichiero = NULL; /* optional */

fichiero = malloc(sizeof "goodbye");
/* same thing, but with "goodbye" ... */

On the other hand, maybe you want to use it to point to string literals.

ficheiro = "hello";
ficheiro = ""; /* Set to point to empty string */
ficheiro = "goodbye"; /* Reinitialize it */
 
try this

ficheiro = ' ';

if you are trying to set it to a space.

its a pointer to a char not a string or array.

you know - " " for string as opposed to ' ' for char.

please specify the rresults that you require.

tomcruz.net

 
butthead
when u do
ficheiro = ' ';
u are actually making the pointer ficherio point to a character const. ' '
nitro, if u observe is trying to make the pointer to NULL.
so the answer is ficherio = NULL;
nitro was getting confused abt how to re-init the pointer.
or maybbe free the contents .. which wud mean
free(ficherio);
Nitro in no way was trying 2 make d pointer point to " "
 
Why are you sure that he wanted to set the pointer to NULL, as opposed to ""? His question seems very unclear to me.
 
as I said "if"

how can we be so sure what this guy meant unless we are mind readers. what he wrote does not make sense to no one but him. that is why we ask for clarification.

if you no what I mean. ;-)

I know what ficheiro = ' ' points to. It is just another posible interpretation of the facts available. some of these posts are written by people who most probably speak english only as a second language and you cant rely upon there experience as a programmer to properly phrase what they want. that does not mean that they do not know what they want. but it is up to us to be patient with them as well as each other.

he used

ficheiro=NULL;
ficheiro="";

in an effort to try to find his solution.

both are trying to do something quite different so you must
persue all avenues in an effort to answer the question.

tomcruz.net
 
Or you could just recommend that he research memory
allocation and research the basics of compiler and the C
language to determine what it is he means to say. ;)

It's may be more polite however to posit a solution and
see if the OP will clarify.
 
but then some one else will turn your polite response into you do not now what you are talking about it is obvious you are an idiot so on and so forth. you know - few people can properly allocate memory for a char string with out totally bunging up the process.. oh I'll never live that one down will I...............................
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top