Thread: strings

  1. #1
    Registered User
    Join Date
    Aug 2014
    Location
    New Delhi, India, India
    Posts
    1

    strings

    Can anyone explain what this program is doing ! (Program is in attachment)
    Code:
    #include<stdio.h>
    int main()
    {
    	char s[]="No two viruses work similarly";
    	int i =0;
    	while(s[i]!='\0');
    	{
    		printf("%c %c\n",s[i],*(s+i));
    		printf("%c %c\n",i[s],*(i+s));
    		i++;
    	}
    	return 0;
    }
    Attached Files Attached Files
    • File Type: c A_c.c (197 Bytes, 70 views)
    Last edited by Salem; 08-29-2014 at 03:05 PM. Reason: Inline the code

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You should post the code here using code tags.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You might want to stare at line 6 some more, until you see a problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Salem View Post
    You might want to stare at line 6 some more, until you see a problem.
    It took me almost two minutes to see it.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Abhishek_Kumar View Post
    Can anyone explain what this program is doing !...
    Code:
    ...
    First, yes line 6's probably should've been picked up with your compiler.
    But after fixing that, to answer your question, basically this program is showing you 4 ways an element of an array can be addressed.
    The second two ways are not recommended though.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    I understand the problem on line 6, but could someone please explain to me line 9's use of
    Code:
     i[s]
    when i is defined as an int ... how is it an array or subject to array notation (i.e. the 's' as a subscript)..???

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by trbennett
    when i is defined as an int ... how is it an array or subject to array notation (i.e. the 's' as a subscript)..?
    To understand this, refer to what the standard has to say about the definition of operator[]
    Quote Originally Posted by C99 Clause 6.5.2.1 Paragraph 2b
    The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).
    i[s] is identical to (*((i)+(s))) which is equivalent to (*((s)+(i))) which is identical to s[i]. However, because most people would expect s[i] (and indeed the standard also states that "E1[E2] designates the E2-th element of E1 (counting from zero)", using i[s] is poor practice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    5
    WOW... That is astounding.
    Thank you for the great description,
    and your description of it as "poor practice" is an understatement...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM