Closed Thread Icon

Topic awaiting preservation: C programming - pointer arithemetic Pages that link to <a href="https://ozoneasylum.com/backlink?for=8599" title="Pages that link to Topic awaiting preservation: C programming - pointer arithemetic" rel="nofollow" >Topic awaiting preservation: C programming - pointer arithemetic\

 
Author Thread
supreme_commander
Nervous Wreck (II) Inmate

From: QLD
Insane since: Mar 2003

posted posted 04-09-2003 03:10

Hey i need help with using strcat(). I need to append a string to the end of anotehr string but always in the same place. Eg - i have a name and i need to append 'the bold' or 'the great' to the end of the name. But each timea loop comes round if a certain condition is meet i need to append a different 'the whatever', but it must overerite the previous thing. So i always needs to start say 1 place after the original name. Please if you know how to do it with strcat or another way please help.

Thanks a lot

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 04-10-2003 17:11

um... what?

I think I can help you, but I don't know what the fuck you're talking about...

you mean like with 'strlen'?

silence
Maniac (V) Inmate

From: soon to be "the land down under"
Insane since: Jan 2001

posted posted 04-16-2003 23:35

I think I get the gist of what you're saying. Since you titled the subject "point arithmetic", we'll use that method.

Okay, say you've got your string:
char str[80]; // allocate enough space for your purposes

Now, you want to append, say, "the great"

First, loop through the string until you hit the null character:

code:
char *char_ptr;
char_ptr = str; // point to the first index value in the string

for (int i; i <= 80; i++) // note: for illustration purposes only, hard coding is bad
{
if (*char_ptr == '\0') // if you're at the null character, i.e. the end of the string
{
*char_ptr = ' '; // change the null to a space
char_ptr++; // increment the pointer to the next address value
*char_ptr = '\0'; // make this value the null character so that concatenation will start here
break;
}
else
char_ptr++; // if this is not the null character, increment the pointer to the next address value
}



Now, you can run strcat() and it will append the text after the string with a space inserted.

Now, say you want to just overwrite whatever's after the name. Then just do the same thing, but instead of searching for the null character, search for that part of the string you want to start concatenation at and insert a null character there.




hyperbole
Paranoid (IV) Inmate

From: Madison, Indiana, USA
Insane since: Aug 2000

posted posted 04-21-2003 16:41

Let's assume the value you want to append to is in the variable name
(As silence said, make sure you have enough room in the variable for the value and whatever you want to append)

Also let's assume the suffixes are in the array of character strings suffix_array[SUFFIX_ARRAY_LENGTH]

Do the following:

code:
char * name_end;

name_end = name + strlen(name);
for (indx=0; indx<SUFFIX_ARRAY_LENGTH; indx++)
{
*name_end = '\000';
strcat(name, suffix_array[indx]);
}



-- not necessarily stoned... just beautiful.

« BackwardsOnwards »

Show Forum Drop Down Menu