1. ## figuring out code

hey guys,

I was going through some code and I was just wondering what is going on it.

Code:
```char *s1,*sE;

//*s was a char passed in

for (i=0;i<=len;i++)
{
*(s1+i)='\0';
*(sE+i)='\0';
};

for (i=0;i<=len;i++)
{
*(s1+i)=*(s+i);
};

for (i=strlen(s1);i<=len;i++)
{
*(s1+i)='\0';
};

strcpy(sEdit,s1);```
I have never seen strings like this before. For example the *(s1+i)='\0';. I have only seem them like s1[0]='\0';. Is this something equivalent to that?

2. *(s1+i)='\0'; is equavalent to s1[i] = '\0'; so yes that's just an alternative (less readable) way of referring to array indices.

Mind you the code itself is basically nonsense... nothing you're likely to see in real world progamming... or, at least, one would hope not.

3. Yes. `*(ptr + i)` is equivalent to `ptr[i]`. In fact, latter is just a shorthand for former.

4. You can also do: i[ptr]

Quzah.

5. Quzah, I had never ever thought of that, but thats indeed true :] Adding "location" <index> the start address, will indeed result same as adding index to start address.

In some strange way I do actually like that notation

6. Originally Posted by Maz
Quzah, I had never ever thought of that, but thats indeed true :] Adding "location" <index> the start address, will indeed result same as adding index to start address.

In some strange way I do actually like that notation
It's in the C FAQ: Question 6.11

Quzah.

7. Originally Posted by Maz
In some strange way I do actually like that notation
If you ever intend on sharing your code with someone else you better stick to the standard way of ptr[i] though, or there is guaranteed to be a lot of confusion later on...

8. You can also do: i[ptr]
You can, but if you do, you're stupid. Sometimes I wonder[1], why every time "array notation" vs. "pointer notation" comes up, somebody feels the need to mention this utterly useless point of trivia. The frequency with which it's brought up is significantly greater than its value.

[1] I always wonder it, actually. But I also think I know the reason: it's a way to show off one's knowledge of the dark areas of C without any effort. Even wannabe experts can appear more knowledgeable than they are by quoting trivia.

9. Originally Posted by Prelude
You can, but if you do, you're stupid. Sometimes I wonder[1], why every time "array notation" vs. "pointer notation" comes up, somebody feels the need to mention this utterly useless point of trivia. The frequency with which it's brought up is significantly greater than its value.
Agreed.

[1] I always wonder it, actually. But I also think I know the reason: it's a way to show off one's knowledge of the dark areas of C without any effort. Even wannabe experts can appear more knowledgeable than they are by quoting trivia.
Not so agreed. I've only been here 10 months, but I've seen hundreds, if not thousands of posts on arrays, and this is actually the first time I recall seeing this particular piece of trivia posted. It certainly isn't "every time", and I would hardly even say "often". Most of the time, it's just about *(p + i) being equivalent to p[i] and discouraging the use of the less clear pointer syntax. Maybe that trivia is more popular on the C++ forum, where I don't hang out. And perhaps it is to show off, but I know from personal experience that questions like that are far too common on job interviews and interview tests. Taking advantage of the commutative property of the [] operator, Duffs device, ++*p++, etc are totally useless in real world programming, but can be of great use for getting a job. Employers seem to think knowing that crap makes you a 1337 h4x0r and worthy of hiring. It's sad, but true. So letting a newbie know of useless crap like that actually can be beneficial. At least they should recognize it as valid C, and be able to comment on how stupid it is.

EDIT: And anybody wishing to mention the i[p] version should be kind enough to tell the OP never to use it.

10. Originally Posted by Prelude
You can, but if you do, you're stupid.
I know you're trying to be all wise now, since I called you out on being an idiot earlier, but this isn't helping you any in your quest.
Originally Posted by Prelude
Sometimes I wonder[1], why every time "array notation" vs. "pointer notation" comes up, somebody feels the need to mention this utterly useless point of trivia. The frequency with which it's brought up is significantly greater than its value.

[1] I always wonder it, actually. But I also think I know the reason: it's a way to show off one's knowledge of the dark areas of C without any effort. Even wannabe experts can appear more knowledgeable than they are by quoting trivia.
It's not a dark area. It's basic math. A pointer is a value. Dereferencing that value gives you whatever is at that address.

X + Y can be an address just as easy as Y + X can. The order you do your addition doesn't matter. It's the same thing. That's why I posted it, because it's the same thing. [] dereferences and that X[ Y] is addition. That's all.

That's all arrays are: Addition and dereferencing. That had been covered already. All I pointed out was that the order of addition is irrelevant. If you can't understand basic math, then I question who the real idiot is in this thread.

Furthermore, it's not undefined behavior, like the "how to swap without a temp" question.

Now run along and be pretentious elsewhere.

edit - Let me submit a whole bunch of "idiots". But I guess that's black programming which leaves you eternally confusled.

Quzah.

11. I know you're trying to be all wise now, since I called you out on being an idiot earlier, but this isn't helping you any in your quest.
Get over yourself, my reply would be the same regardless of who brought it up.

12. The bulk of mine would as well. It's not a dark area. It's basic math. It's also standard compliant. C doesn't care if you add X to Y or Y to X. It's the same thing. Using value[array] is no different than using array[value], and understanding that can only help you.

Quzah.

13. Well, I've learned something I didn't know before. If I have seen this index[ptr] format before, I probably thought it was an error from the poster being distracted at that moment.

I don't see anything wrong with it - but on the other hand, I see no reason to prefer it. Confusing for those not aware of it, of course. We could waste a zillion posts explaining it over and over, if we were to use it regularly.

I don't see that using it should get you labeled as "stupid" or "genius", either one.

I don't see that using it should get you labeled as "stupid" or "genius", either one.
Just because you CAN do something does not mean you SHOULD.

And I think the word you're looking for is "Silly"...

15. Originally Posted by CommonTater
Just because you CAN do something does not mean you SHOULD.

And I think the word you're looking for is "Silly"...
It is sort of like this discussion.

Putting "if( 0 == var )" compared to "if( var == 0 )" is really no different than saying "value[array]" compared to "array[value]".

There's no difference at all.

The only reason it's "silly", or "stupid" is because you were first taught that "array[value]" was "right". Damn, have some quotation marks!

But as far as one being better, it's just simply not so. It's purely personal preference. They're identical. There's nothing in the language to indicate one is better than the other. It's just because that's how you learned it, so that must be right.

If you want to be silly, you could say: 0[X + Y] == *(X + Y)
Of course, you'd still be right; they'd be equally correct, and equally as easy to understand.

Quzah.