GP32 Cutting A String - How ?


ConsoleTom

Member
Joined
Dec 4, 2003
Messages
106
Age
48
Location
Germany
Website
Visit site
Hii !

Let's say i have a string

char strA[10];

The string contains "abcdef" but i need only "abc". Is it enough to terminate the string at the needed position ?

strA[4] = '\0'

Or can i use

strncpy (strA,strA,3);

or must i use a temp-variable

strcpy (strB, strA);
strncpy (strA,strB,3);

Tell me, what is the best solution.

Tobias

PS. Don't say strcpy(strA,"abc"); !!!!!
 
me too but sometimes you may want to use strA again,write something over strA[4] and when you will print it on the screen this will write the strA[5] and the following too, that's why sometime i fill the whole array with '\0' but maybe that's not the best technique, i write something like:

for(;strA[n];n++)strA[n]='\0';

sorry for my bad english
 
me too but sometimes you may want to use strA again,write something over strA[4] and when you will print it on the screen this will write the strA[5] and the following too, that's why sometime i fill the whole array with '\0' but maybe that's not the best technique, i write something like:

for(;strA[n];n++)strA[n]='\0';

sorry for my bad english
Hi !

I can't understand what the filling of the whole string with \0 is good for.

When i declare a string variable:

char strA[10];

i define a size of 10 characters (correct me if i'm wrong) and the last char must be \0.
When i set the 4th position to \0 then there is still space for 10 characters allocated. So i can just copy a new string to it:

strcpy(strA,"longer");

Strcpy overwrites the old data and terminates the string automatically (doesn't it ?)

Greetings

Tobias
 
Last edited by a moderator:
Hi !

I can't understand what the filling of the whole string with \0 is good for.
you're right, if you don't forget to put a '\0' there's no problem, i was wrong.

When i declare a string variable:

char strA[10];

i define a size of 10 characters (correct me if i'm wrong)

I think that's 11 char but usually you finish it with a \0 so it's the same thing.
and the last char must be \0.
When i set the 4th position to \0 then there is still space for 10 characters allocated. So i can just copy a new string to it:

strcpy(strA,"longer");

Strcpy overwrites the old data and terminates the string automatically (doesn't it ?)

Yes that's it
 
Last edited by a moderator:
Back
Top