Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have code in c# like this :

char* length = (char*) value.Length;
char* chPtr = (char*) startIndex;
char* chPtr2 = (char*) count;

//Error
if ((chPtr + length) <= chPtr2)
{}

if condition with operator + ERROR

can you help me

What I have tried:

if ((chPtr) + (length)) <= chPtr2)
{}
Posted
Updated 30-May-16 23:40pm
Comments
John C Rayan 31-May-16 5:38am    
You have to explain first what you are trying to do.
The code is too dangerous.

What is your requirement?

Um...there are a couple of oddities there.
startIndex doesn't sound like a pointer - it sounds like an integer which indexes into a memory structure of some form: an array or similar.
You are also converting a Length to a pointer which is going to be odd.
But when you get to your code that shows the error:
C#
if ((chPtr + length) <= chPtr2))
What you are telling the compiler to do is add chPtr to length: and they are both declared as pointers:
C#
char* length = (char*) value.Length;
char* chPtr = (char*) startIndex;
Which means you are telling the compiler to add two pointers - which doesn't produce anything useful, but certainly doesn't produce another pointer. Think of it in terms of DateTime: if you subtract two DateTime values, you get a Timespan - the difference between the two dates. If you subtract two pointers, you get in integer (or long) - the number of bytes between the two pointer values. But if you add two DateTimes, what do you get? Nothing useful. The same applies to pointers - adding them doesn't produce anything useful!

I don't think any of those three values should be pointers at all!
 
Share this answer
 
This is from Microsoft official c# site.

To maintain type safety and security, C# does not support pointer arithmetic, by default. However, by using the unsafe keyword, you can define an unsafe context in which pointers can be used. For more information about pointers, see the topic Pointer types.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900