473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Soustraire 2 pointeurs

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,
Nov 13 '05 #1
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 :(

Nov 13 '05 #2
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.

Nov 13 '05 #3
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
Nov 13 '05 #4
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
Nov 13 '05 #5
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
Nov 13 '05 #6
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
Nov 13 '05 #7
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

Nov 13 '05 #8
> 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;
}

Nov 13 '05 #9

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

Nov 13 '05 #10
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
Nov 13 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
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...
0
isladogs
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
BarryA
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...
1
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...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.