I try to develop my own Left/Right functions for C but when I use them, they crash my app...
Here are the two functions :
char *strgetright( char *source, int lentoread ){
char teststring;
int txtlen =strlen( source );
// Si le texte source est plus grand que le texte cible
if ( txtlen > lentoread ){
source = source + ( txtlen - lentoread );
strcpy( teststring, source );
}
// Si le texte source est de la meme dimension que la cible demandee
if ( txtlen == lentoread ){
strcpy( teststring, source );
}
// Si le texte source est moins long que la cible demandee
if ( txtlen < lentoread ){
teststring = "";
}
return &teststring;
}
char *strgetleft( char *source, int lentoread ){
char teststring;
int txtlen = strlen( source );
// Si le texte source est plus grand que le texte cible
if ( txtlen > lentoread ){
strncpy( teststring, source, lentoread );
teststring = teststring + "\0";
}
// Si le texte source est de la meme longueur que la cible demandee
if ( txtlen == lentoread ){
strcpy( teststring, source );
}
// Si le texte source est moins long que la cible demandee
if ( txtlen < lentoread ){
teststring = "";
}
return &teststring;
}
All we have to decide is what to do with the time that is given to us.
Ouch you are strcpy'ing a string into a char !! This should have thrown warning in gcc. Then even if you would change teststring from char to char* this would still crash as before strcpy'ing into a string you should assure yourself that the target string is large enough to hold the data (i.e. you should allocate it accordingly before).
Apart from copying to the wrong type of receiver, you should consider strlcpy() which checks the length, and only copies a certain amount of characters so as to avoid writing beyond the buffer.
One other point, if you are going to post code into an English forum, you might want to do something about the Italian comments. :)
First of all you forgot the second equal sign and it doesn't work that way in C language. In C you can only compare two memory pointers that way and not any string content.
Rock lobster bit me - so I'm here forever X1000 + AmigaOS 4.1 FE "Anyone can build a fast CPU. The trick is to build a fast system." - Seymour Cray
A little test on the nright value (like nLeft in the first function) is necessary if you don't want to have crash.
As rigo wrote, you must use 2 equals. For comparison, you can write a comparison function which compares each char one by one until it is false or it the end of the word. I will give you one way to do that. I code this very rapidly so don't compile that directly. I don't like C code, I prefer C++ so verify the initializations and bool type and min function.
bool compare(firstword, secondword, sz) char *firstword, *secondword; int sz;
s1=strlen(firstword); s2=strlen(secondword); int i; bool continue=true;
if ((s1<>s2) || (sz <0)) return false; for (i=0;(i<min(s1,sz)) && continue;i++) if (firstword[i]!=secondword[i]) continue=false;
Using a single = in C doesn't work like that. It's an assignment, meaning that it copies ".mrw" into the variable szBuffer in your code. And usually it ends up being true because the assignment was successful.
if (a == 3) {
is how you compare two things. However, you can't compare strings like that in C as they're effectively an array of variables, not just a single variable. You need to use the strcmp() function, which will return 0 if they're identical (be careful with that!)
I dont want to be rude, but your comprension of the C language actually it's a bit to low, you actually cannot understand how comparision and assignement works in C, so you have so many problems with your code. I suggest you to read/study a good C language manual.
Sorry for the rant, but I am sure you can have much satisfaction knowing the language better.
P.S. it's not a good idea to use a global variable for the use you want to, because if you invoke yours' function more than once the result it's overwritten.
Using a single = in C doesn't work like that. It's an assignment, meaning that it copies ".mrw" into the variable szBuffer in your code. And usually it ends up being true because the assignment was successful.
This is wrong. It will not "copy" ".mrw" into szBuffer. What will happen is that a pointer to the string literal ".mrw" will be assigned to szBuffer. That the expression will always be true is correct, but not because the assignment is "successful" but because the value being assigned is not a 0 or a NULL pointer and is therefore regarded as true in the C language.
To copy a string you need to use the strcpy() or the strlcpy() function.
Quote:
is how you compare two things. However, you can't compare strings like that in C as they're effectively an array of variables, not just a single variable. You need to use the strcmp() function, which will return 0 if they're identical (be careful with that!)
For a case insensitive string comparison (might be more appropriate in this case) one should use the stricmp()/strcasecmp() function.
strcmp() does a case sensitive string comparison (i.e. ".MRW" is not the same as ".mrw" or ".MrW").
First of all your code only works for strings smaller than 1023 characters. Then using a global variable to return a result is generally something to be avoided, you should better allocate a new string and return it from your functions.
And last everyone told you you can't compare strings using '==' in C, but noone tought to tell you that this is done using strcmp() (ANSI C = portable) or locale.library/StrnCmp() (Amiga specific). But then the caller should remember to free it when it does not need it anymore. Just for your information maybe strncmp or strnicmp might interest you see :
char * extPos = strrchr(filename,'.'); // find the extension pos
if( extPos )
{
// an extension was found
if( strncmp(filename+extPos,".mrz")
{
// ... do what you want
}
}
Thanks, I'm quite familiar with the intricacies of C I was trying to keep things as simple as possible by not adding that strings can't be assigned like that. I meant that in general, that's how the statement reads. Since the OP wasn't looking to copy the string buffer I just left it at that...
I guess I was a bit off with the assignment being successful - it was early in the morning
@YesCop : It's something similar to this that I planed to do after posting the message .. I was sure that the problem was here that I can't compare string directly ...
@Daedalus & @Pvanni: Heh Guys :p Concerning comparizons, I know how they work ... just in that case I've forgotten to put == instead of = ... it's something I've understood but when I code ... I write so many lines that sometimes an error happen ...
@Pvanni : You're wrong in your facts .. It's better for me to begin with a big project because it forces me to learn the language. It's only a period of adaptation for me to learn C ... regarding to other languages I already know. The use of global is deliberately defined because this variable will be used in definitive, in a slightly different way (from commands) and it's wanted that the result is overwritten ... you cannot actually understand why but maybe later when the project will be more advanced, you'll understand why ;)
@Salass00 : Ok
@abalaban : Thank you for this advice :) It's a really good and optimised one :p
All we have to decide is what to do with the time that is given to us.
If this function is for your Basic compiler, you might want to note that Basic generally uses length-terminated strings while C uses the older-style null-terminated strings. If you're using C++ you can use the standard template library function std::string to get length-terminated functionality.
For an example of how length-terminated strings can be implemented in C, you might want to look at the old code for mb_strings.c and mb_strings.h.