Soustraire 2 pointeurs

  • Thread starter Francois Cartegnie
  • Start date
F

Francois Cartegnie

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,
 
F

Francois Cartegnie

Francois said:
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 :(
 
F

Francois Cartegnie

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.
 
D

Dan Pop

^^^^^^
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.
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
 
P

pete

Francois said:
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;
 
D

Dan Pop

In said:
Topic is transferred on fr.comp.lang.c


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
 
D

Dan Pop

In said:
taille = temp - (char*)bufferspace;

Or

taille = (char *)temp - bufferspace;

In the absence of a working crystal ball:

taille = (char *)temp - (char*)bufferspace;

Dan
 
F

Francois Cartegnie

Dan said:
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.




Or any other arithmetic type capable of representing strlen(bufferspace).
The code also requires that temp is pointer to char.




You actually *must* do that, unless you *know* that the substring is
present.

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
 
F

Francois Cartegnie

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;
}
 
A

Arthur J. O'Dwyer

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
 
D

Dan Pop

In said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top