-2

I am trying to learn how to use pointers in C and I'm trying to get the equivalents for &input[71] and &input[i];

I have tried if (*(input+i) - arrayEnd) == 0) and it tells me I have an int vs char * comparison.

char input[72];
char *arrayEnd = &input[71];
if((&input[i] - arrayEnd) == 0)
user2946437
  • 23
  • 2
  • 6
  • 2
    As a starter array index 72 is invalid. Indexes in C start from zero. So an array with 72 elements will have index from 0-71. – benipalj Nov 24 '13 at 22:10
  • 2
    Read This.. Seriously: [What are the barriers to understanding pointers and what can be done to overcome them?](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) – WhozCraig Nov 24 '13 at 22:11
  • 5
    @NedStark Nevertheless, the index 72 is valid as long as it's not dereferenced. (Pointers pointing one past the last element of the array can be used for comparison.) –  Nov 24 '13 at 22:14

1 Answers1

3

I have tried if (*(input+i) - arrayEnd) == 0) and it tells me I have an int vs char * comparison

Yes. You are doing an int vs char * comparison. *(input+i) means you are dereferencing the value at the address (input+i).

How to use pointers for array instead of brackets

Try this instead

if( ((input + i) - arrayEnd) == 0 )
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    @H2CO3; Read the tiltle: **How to use pointers for array instead of brackets**. I answered for this title. :) – haccks Nov 24 '13 at 22:15