Thread: Question about pointers

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    Question about pointers

    First I would want to know why,if I declare the following:
    Code:
    char a[]="abcd";
    and then print:
    Code:
    printf("%s\n",a);
    it;s ok and even
    Code:
    printf("%c\n",a[1]);
    is good
    But when I replace the %c into %s so I get:
    Code:
    printf("%s\n",a[1]);
    that's an error
    I mean , why can;t it print bcd?
    is it some logic point I missed or simply syntax?

    Another question:
    I have teh following declarations:
    Code:
    	char *c[]={"door","me","pointers","balul"};
    	char **cp[]={c+3,c+2,c+1,c};
    	char ***cpp=cp;

    And the following printing lines:
    Code:
        printf("%s\n",**++cpp);
    	printf("%s\n",++**cpp);
    	printf("%s\n",*cpp[-2]+3);
    	printf("%d\n",***cpp);
    	printf("%d\n",**cp);
    Well I understand the 1st and 2ed lines, it;s the 3rd and 4th I dun quite get...
    Why the 3rd line gives trash
    And the 4rth gives "inters"?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,391
    a[1] is a char, so it does not make sense to try and print it with %s. What you want to do is to print a substring starting from index 1, so you should write:
    Code:
    printf("%s\n", a + 1);
    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

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What you could do is:

    Code:
    printf("%s\n", &a[1]);
    But it's redundant really, since a[1] is effectively a de-reference of a at index 1. So, a more common sense way to do, is what laserlight suggested.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Thank you I understood that part

    But what about the other I can;t figure that out.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,391
    For the third, recall that cpp[n] is equivalent to *(cpp + n). Then, trace what cpp points to and you will see the problem.
    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

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Thanks for the help :P.
    after realizing that it was ++cpp...somehow I missed that ._. (I didn;t keep the change but always followed the initiate value...)
    All became clear.

    Okies now all is clear as ice ^^ thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question : Passing Array Arguments as Pointers
    By NEMEAN in forum C Programming
    Replies: 1
    Last Post: 12-04-2010, 02:33 PM
  2. Pointers question
    By JMK in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2010, 07:48 PM
  3. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  4. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM