Thread: figuring out code

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    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. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    *(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. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Yes. `*(ptr + i)` is equivalent to `ptr[i]`. In fact, latter is just a shorthand for former.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can also do: i[ptr]


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Maz View Post
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by Maz View Post
    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. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Prelude View Post
    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.
    Last edited by anduril462; 09-23-2011 at 11:04 AM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Prelude View Post
    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.
    Quote Originally Posted by Prelude View Post
    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.
    Last edited by quzah; 09-23-2011 at 03:29 PM.
    Hope is the first step on the road to disappointment.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.
    My best code is written with the delete key.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    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. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    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.
    Last edited by quzah; 09-23-2011 at 06:23 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help figuring out error
    By dudeomanodude in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2008, 03:20 PM
  2. Help figuring out equation.
    By gator6688 in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2007, 03:17 PM
  3. Need help figuring out what to learn
    By the dead tree in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-17-2004, 12:02 PM
  4. help figuring out a problem
    By kwm32 in forum Windows Programming
    Replies: 2
    Last Post: 03-20-2004, 10:47 PM
  5. Figuring Algo for assignment
    By axon in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2003, 11:03 AM