Hello,
Je cherche un moyen de connaitre la position d'une occurence dans une chaine
J'ai essayé :
temp = strstr(bufferspace, "truc");
taille = temp-bufferspace;
Ne fonctionne pas car temp peut prendre la valeur NULL et donc le
compilateur refuse.
Etant dans l'espace noyau, je n'ai accès qu'a des fonctions sortant un
pointeur, et non la position.
Cordialement, 10 2377
Francois Cartegnie wrote: Hello,
Je cherche un moyen de connaitre la position d'une occurence dans une chaine
J'ai essayé : temp = strstr(bufferspace, "truc"); taille = temp-bufferspace; Ne fonctionne pas car temp peut prendre la valeur NULL et donc le compilateur refuse.
Etant dans l'espace noyau, je n'ai accès qu'a des fonctions sortant un pointeur, et non la position.
Cordialement,
oops, srry, this isn't the right forum localization :(
Topic is transferred on fr.comp.lang.c The compiler _need_ not complain about the possibility that temp may be NULL, but it _may_ complain about the current weather (warning: possibly too hot to compile at the moment).
Your code does what it is supposed to do if taille is a ptrdiff_t.
You can always check for NULL at runtime, and return -1 if "truc" is not present in bufferspace. However: What was the exact wording of your compilers "refusal" to accept that code?
invalid operands to binary -
which is told in the NG archives to be substracting two pointers whith
one of unknown size.
In <3F***************@stat.uni-muenchen.de> Kurt Watzka <ku*********@stat.uni-muenchen.de> writes: Francois Cartegnie wrote: Hello,
Je cherche un moyen de connaitre la position d'une occurence dans une chaine
J'ai essayé : temp = strstr(bufferspace, "truc"); taille = temp-bufferspace; Ne fonctionne pas car temp peut prendre la valeur NULL et donc le compilateur refuse. ^^^^^^ The compiler _need_ not complain about the possibility that temp may be NULL, but it _may_ complain about the current weather (warning: possibly too hot to compile at the moment).
However, it cannot *refuse* to compile the code because of that.
OTOH, it's hard to judge the OP's code without seeing the declarations
for temp, taille, bufferspace and without knowing whether <string.h>
has been included. E.g. if temp has an integer type, the expression
temp-bufferspace is a constraint violation and a diagnostic is required.
Your code does what it is supposed to do if taille is a ptrdiff_t.
Or any other arithmetic type capable of representing strlen(bufferspace).
The code also requires that temp is pointer to char. Etant dans l'espace noyau, je n'ai accès qu'a des fonctions sortant un pointeur, et non la position.
You can always check for NULL at runtime, and return -1 if "truc" is not present in bufferspace.
You actually *must* do that, unless you *know* that the substring is
present.
However: What was the exact wording of your compilers "refusal" to accept that code?
Good question.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Francois Cartegnie wrote: However: What was the exact wording of your compilers "refusal" to accept that code?
invalid operands to binary - which is told in the NG archives to be substracting two pointers whith one of unknown size.
taille = temp - (char*)bufferspace;
--
pete
In <bg**********@aphrodite.grec.isp.9tel.net> Francois Cartegnie <NO****@TAMAMAN.OWC> writes: Topic is transferred on fr.comp.lang.c
The compiler _need_ not complain about the possibility that temp may be NULL, but it _may_ complain about the current weather (warning: possibly too hot to compile at the moment).
Your code does what it is supposed to do if taille is a ptrdiff_t.
You can always check for NULL at runtime, and return -1 if "truc" is not present in bufferspace. However: What was the exact wording of your compilers "refusal" to accept that code?
invalid operands to binary - which is told in the NG archives to be substracting two pointers whith one of unknown size.
One of your pointers was probably a void pointer! You got what you
deserved.
The compiler complained about a void pointer, not about a null pointer!
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <3F***********@mindspring.com> pete <pf*****@mindspring.com> writes: Francois Cartegnie wrote:
> However: What was the exact wording of your > compilers "refusal" to accept that code?
invalid operands to binary - which is told in the NG archives to be substracting two pointers whith one of unknown size.
taille = temp - (char*)bufferspace;
Or
taille = (char *)temp - bufferspace;
In the absence of a working crystal ball:
taille = (char *)temp - (char*)bufferspace;
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Dan Pop wrote: In <3F***************@stat.uni-muenchen.de> Kurt Watzka <ku*********@stat.uni-muenchen.de> writes: Francois Cartegnie wrote:
Hello,
Je cherche un moyen de connaitre la position d'une occurence dans une chaine
J'ai essayé : temp = strstr(bufferspace, "truc"); taille = temp-bufferspace; Ne fonctionne pas car temp peut prendre la valeur NULL et donc le compilateur refuse.
^^^^^^ The compiler _need_ not complain about the possibility that temp may be NULL, but it _may_ complain about the current weather (warning: possibly too hot to compile at the moment).
However, it cannot *refuse* to compile the code because of that.
OTOH, it's hard to judge the OP's code without seeing the declarations for temp, taille, bufferspace and without knowing whether <string.h> has been included. E.g. if temp has an integer type, the expression temp-bufferspace is a constraint violation and a diagnostic is required.
Your code does what it is supposed to do if taille is a ptrdiff_t.
Or any other arithmetic type capable of representing strlen(bufferspace). The code also requires that temp is pointer to char.
Etant dans l'espace noyau, je n'ai accès qu'a des fonctions sortant un pointeur, et non la position.
You can always check for NULL at runtime, and return -1 if "truc" is not present in bufferspace.
You actually *must* do that, unless you *know* that the substring is present.
However: What was the exact wording of your compilers "refusal" to accept that code?
The problem has been solved.
There was a pointer declared as char * and the other as unsigned char *.
What's strange, is that it was giving an error instead of typeage
warning, even with prefixing the two pointers with (unsigned char *).
The error message was "invalid operands to binary - "
and compiler version is GCC 3.2/i386/linux
> The problem has been solved. There was a pointer declared as char * and the other as unsigned char *.
What's strange, is that it was giving an error instead of typeage warning, even with prefixing the two pointers with (unsigned char *). The error message was "invalid operands to binary - " and compiler version is GCC 3.2/i386/linux
okay, I made a mistake, it was working with forcing both to (unsigned
char *)
but this simple compilation test code still gives the
"invalid operands to binary - " error.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(int argc, char* argv)
{
char * a; unsigned char * b;
b = strstr(a, "truc");
int size = b - a;
}
On Tue, 5 Aug 2003, Francois Cartegnie wrote: The problem has been solved. There was a pointer declared as char * and the other as unsigned char *.
<snip> but this simple compilation test code still gives the "invalid operands to binary - " error.
.... char * a; unsigned char * b;
.... b - a;
Good. It's supposed to. (char *) and (unsigned char *) are different
pointer types, and can't be subtracted from each other. Just like
'int *' and 'float **' can't be subtracted from each other. The fact
that 'sizeof (char *) == sizeof (unsigned char *)' is completetly
irrelevant.
HTH,
-Arthur
In <bg**********@aphrodite.grec.isp.9tel.net> Francois Cartegnie <NO****@TAMAMAN.OWC> writes: but this simple compilation test code still gives the "invalid operands to binary - " error.
#include <stdlib.h> #include <stdio.h> #include <string.h>
main(int argc, char* argv) {
char * a; unsigned char * b; b = strstr(a, "truc"); int size = b - a; }
Yes, your code violates a constraint in the C standard:
Constraints
3 For subtraction, one of the following shall hold:
- both operands have arithmetic type;
- both operands are pointers to qualified or unqualified versions
of compatible object types; or
- the left operand is a pointer to an object type and the right
operand has integer type. (Decrementing is equivalent to
subtracting 1.)
Note that char and unsigned char are NOT compatible object types, this is
why you have to convert one of the pointers to the type of the other.
OTOH, are you sure you need b to be pointer to unsigned char?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: YouPoP |
last post by:
I have an MFC application (VS2005) with splitter window (one is the view other is a formview). you can select from the main menu different math operation on polynoms and polynoms shows on the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |