help a beginner with a basic function that should return a char

L

luserXtrog

I think that there is a very important distinction to be made
here.  It may be the case that every thing can be described using
one or more numbers, but that doesn't mean that every thing is a
number.

Well I disagree, but I've made more than sufficient fuss about it.
If you find value in keeping these concepts distinct, then by all
means, have at it.
 
N

Nobody

For the difference to be defined, p and q must point to elements of the
same array.

You are wrong.

N869
6.5.6 Additive operators

[#7] For the purposes of these operators, a pointer to a
nonarray object behaves the same as a pointer to the first
element of an array of length one with the type of the
object as its element type.

I'm hard pushed to think of a non-contrived case where this would be
meaningful. If p points to an object which isn't an array element, then
it's meaningless to evaluate p-q unless you know that q points to the same
object as p; but if you know that, you already know that p-q==0, so
evaluating p-q is rather pointless.
 
K

Keith Thompson

Nobody said:
You can do some arithmetic operations on some pointers.

If p and q point to parts of the same object,
then (p - q) and (q - p) are defined.

For the difference to be defined, p and q must point to elements of the
same array.

You are wrong.

N869
6.5.6 Additive operators

[#7] For the purposes of these operators, a pointer to a
nonarray object behaves the same as a pointer to the first
element of an array of length one with the type of the
object as its element type.

I'm hard pushed to think of a non-contrived case where this would be
meaningful. If p points to an object which isn't an array element, then
it's meaningless to evaluate p-q unless you know that q points to the same
object as p; but if you know that, you already know that p-q==0, so
evaluating p-q is rather pointless.

It means that you can treat a single object as a one-element array.
For example (untested code):

void func(double *arr, size_t count) {
for (ptr = arr; ptr < arr+count; ptr ++) {
printf("%g\n", *arr);
}
}

...

double x = 1.234;
func(&x, 1);

I'm sure someone else can come up with a less contrived example.
 
J

jameskuyper

Nobody said:
You can do some arithmetic operations on some pointers.

If p and q point to parts of the same object,
then (p - q) and (q - p) are defined.

For the difference to be defined, p and q must point to elements of the
same array.

You are wrong.

N869
6.5.6 Additive operators

[#7] For the purposes of these operators, a pointer to a
nonarray object behaves the same as a pointer to the first
element of an array of length one with the type of the
object as its element type.

I'm hard pushed to think of a non-contrived case where this would be
meaningful. If p points to an object which isn't an array element, then
it's meaningless to evaluate p-q unless you know that q points to the same
object as p; but if you know that, you already know that p-q==0, so
evaluating p-q is rather pointless.

toys.h:
struct myToy { /* details */ };
intptr_t playWithToys(struct myToy* p, struct myToy* q)

toys.c:
#include "toys.h"

intptr_t playWithToys(struct myToy* p, struct myToy* q)
{
if(p==NULL || q == NULL)
return 0;

intptr_t count = q - p;
for(; p<q; p++)
{
// Do things with *p
}

return count;
}

Use case:
#include "toys.h"
// other details

myToy car;
intptr_t n = playWithToys(&car, 1 + &car);
 

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

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top