Click to See Complete Forum and Search --> : c question about pointers
Fandelem
03-28-2002, 03:43 AM
I'm trying to write a scramble word function..
however I don't really need help with the scramble word function, I just have a basic question :)
Let's say I have two char pointers.
char *tmp, *tmp1;
I set:
tmp = word; // tmp now points to word's memory address
and yet now I can do something like:
while ( *tmp != '\0' )
{
// do stuff
}
however *tmp1 is pointing to random space, correct?
well, i guess my question is how can I set *tmp1 to kind of be 'nulled out'?
this is what i have so far:
edit: this does not work, it segfaults on the first *tmp1 = '\0' :(
length = strlen(tmp)-1;
/* null out our new word */
{
for ( i = 0; i < length; i++)
{
*tmp1 = '\0';
tmp1++;
}
}
*tmp1 = '\0';
so then, i can do something like this:
while ( *tmp != '\0' )
{
do
{
for ( i = number_range(0,length); i < length; i++ )
tmp1++;
/* make sure it's not taken */
if ( *tmp1 == '\0' )
{
*tmp1 = *tmp;
free = TRUE;
}
} while ( !free );
/* move our new word back to beginning */
for ( ; i >= 0; i-- )
tmp1--;
/* move our original word along one */
tmp++;
free = FALSE;
}
but I'm wondering if there's a better way, or if this is even accomplishing what I want to do?
(confused, please don't flame :)
cheers,
kyle
[ 28 March 2002: Message edited by: Fandelem ]
sans-hubris
03-28-2002, 05:42 AM
Set your pointers to NULL and check for that.
if(tmp1==NULL)
{
tmp1=tmp;
free=TRUE;
}
Also, always, always, always keep one pointer pointing to the head of the string, or else you will not be able to free() that memory.
Fandelem
03-28-2002, 06:11 AM
Originally posted by sans-hubris:
<STRONG>Set your pointers to NULL and check for that.
if(tmp1==NULL)
{
tmp1=tmp;
free=TRUE;
}
Also, always, always, always keep one pointer pointing to the head of the string, or else you will not be able to free() that memory.</STRONG>
okay.. a few questions, if you don't mind :)
1. if i do: char *tmp1 = NULL; ... will i still be able to increase/decrease the pointer and have it still be 'null' where i haven't already copied information to it?
essentially, with char arrays, it would be like putting each element = to '\0' and then only adding something when it equaled '\0', otherwise you know you have put something there, and it would just move on to the next element..
2. what do you mean by keeping track of the pointer to the head of the string? how can i determine this (from the start) and is there a way to figure this out at the end?
3. if all i do is: char *tmp; .. do i still need to free() it? i thought if i use malloc() or calloc() (etc) ..THEN i would need to use free()
thanks so much for your advise!
cheers,
kyle
bwkaz
03-28-2002, 10:08 AM
Originally posted by Fandelem:
<STRONG>okay.. a few questions, if you don't mind :)
1. if i do: char *tmp1 = NULL; ... will i still be able to increase/decrease the pointer and have it still be 'null' where i haven't already copied information to it?</STRONG>
If you set the pointer to NULL, you are doing nothing to the memory it pointed to. If you say "char *tmp1 = NULL", all that does is make the address in tmp1 be NULL, or zero on ia32. The advantage of that is so you can trap indirection through the pointer, because no one has permission to read from or write to NULL -- this is why some programs segfault.
What you should do is first, this:
char *tmp1 = NULL;
Then, this:
tmp1 = (char *)malloc(<insert maximum size of string here> * sizeof(char));
if(tmp1 == NULL) {
printf("Error could not allocate memory!\n");
exit(1);
}
memset(tmp1, '\0', <insert maximum size here also> );
Then tmp1 will point to a set of characters big enough to hold anything, filled with zeros for the moment.
<STRONG>essentially, with char arrays, it would be like putting each element = to '\0' and then only adding something when it equaled '\0', otherwise you know you have put something there, and it would just move on to the next element..</STRONG>
This is what the code above sets up for you.
<STRONG>2. what do you mean by keeping track of the pointer to the head of the string? how can i determine this (from the start) and is there a way to figure this out at the end?</STRONG>
You create another pair of character pointers, and set one to be equal to tmp, and one equal to tmp1 (before you change either tmp or tmp1). Then after you're done moving things around, you free() the second pair of pointers (NOT tmp and tmp1!).
<STRONG>3. if all i do is: char *tmp; .. do i still need to free() it? i thought if i use malloc() or calloc() (etc) ..THEN i would need to use free()</STRONG>
If all you do is char *tmp, then you haven't allocated any memory for it to point to! All you've done is made a new pointer that at the moment points at nothing useful. As soon as you try to read from tmp, or write to it, you will almost definitely either (a) segfault, or (b) overwrite some other data that your program was saving, because that's where tmp happened to point to. Both of these are bad.
You need to set your pointers to some memory, and if you get that memory with malloc() (rather than, say, by using strcat() with other strings that have already been allocated), you have to keep a pointer to the beginning of the block or you won't be able to free() it. And yes, you do need to free() it.
s3raphim
03-31-2002, 06:45 PM
in the first line do this:
char *tmp, *tmp1=NULL
that's it. it's null to begin with now until you assign it otherwise