pointer

Discussion in 'C Programming' started by vim, May 18, 2006.

  1. vim

    vim Guest

    hi guys

    this is my code
    #include<stdio.h>
    main()
    {
    char *p[]={"hello" ,"world" ,"Nice","show"};
    char **p1;
    }
    I want to print the string "helwornicsho" by using ptr increment or
    decrement.
    How can I do that.
    I tried to print charachter by charachter .
    But for that increment ptr pointing to each charachter .
    I used (**p1)++.But it is giving segmentation fault.
    Plz tell the error.
    vim, May 18, 2006
    #1
    1. Advertising

  2. vim

    vim Guest

    Please another thing is there is statement after
    char **p1;
    p1=p;
    vim, May 18, 2006
    #2
    1. Advertising

  3. vim

    Vladimir Oka Guest

    vim wrote:
    > hi guys
    >
    > this is my code
    > #include<stdio.h>
    > main()
    > {
    > char *p[]={"hello" ,"world" ,"Nice","show"};
    > char **p1;
    > }
    > I want to print the string "helwornicsho" by using ptr increment or
    > decrement.
    > How can I do that.
    > I tried to print charachter by charachter .
    > But for that increment ptr pointing to each charachter .
    > I used (**p1)++.But it is giving segmentation fault.
    > Plz tell the error.


    If you can't explain what exactly you want to achieve, and if you won't
    post the code you tried and failed, there's little chance anyone can
    help.

    I guess you want to print first three characters of each element of
    `p`, possibly with the first one of each being forced to lowercase. In
    which case, this should do you nicely:

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    char *p[]={"hello" ,"world" ,"Nice","show"};
    int i,j;

    for (i = 0; i < (sizeof p / sizeof p[0]); ++i)
    {
    /* no error checking here */
    for (j = 0; j < 3; ++j)
    {
    printf("%c", tolower(p[j]));
    }
    }
    printf("\n");

    return 0;
    }

    Be warned, I did not include checks to see whether any of the array
    elements are long enough (i.e. have at least three characters).
    Vladimir Oka, May 18, 2006
    #3
  4. vim

    Vladimir Oka Guest

    vim wrote:
    > Please another thing is there is statement after
    > char **p1;
    > p1=p;


    Quote context, even when replying to own posts. Read
    <http://cfaj.freeshell.org/google/>.

    I have a sneaky suspicion you've been told this before (and more than
    once). If you want to be taken seriously here, please follow the advice
    in the link above, and also below:

    <http://www.clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    Vladimir Oka, May 18, 2006
    #4
  5. "vim" <> wrote in message
    news:...
    > Please another thing is there is statement after
    > char **p1;
    > p1=p;
    >

    Where is the context? I see two statements here; where is the rest?
    What is your question?
    --
    Fred L. Kleinschmidt
    Boeing Associate Technical Fellow
    Technical Architect, Software Reuse Project
    Fred Kleinschmidt, May 18, 2006
    #5
  6. vim

    Vladimir Oka Guest

    Vladimir Oka wrote:
    > vim wrote:
    > > hi guys
    > >
    > > this is my code
    > > #include<stdio.h>
    > > main()
    > > {
    > > char *p[]={"hello" ,"world" ,"Nice","show"};
    > > char **p1;
    > > }
    > > I want to print the string "helwornicsho" by using ptr increment or
    > > decrement.
    > > How can I do that.
    > > I tried to print charachter by charachter .
    > > But for that increment ptr pointing to each charachter .
    > > I used (**p1)++.But it is giving segmentation fault.
    > > Plz tell the error.

    >
    > If you can't explain what exactly you want to achieve, and if you won't
    > post the code you tried and failed, there's little chance anyone can
    > help.
    >
    > I guess you want to print first three characters of each element of
    > `p`, possibly with the first one of each being forced to lowercase. In
    > which case, this should do you nicely:
    >
    > #include <stdio.h>
    > #include <stdlib.h>


    Sorry, it should have been:

    #include <stdio.h>
    #include <ctype.h>

    > int main(void)
    > {
    > char *p[]={"hello" ,"world" ,"Nice","show"};
    > int i,j;
    >
    > for (i = 0; i < (sizeof p / sizeof p[0]); ++i)
    > {
    > /* no error checking here */
    > for (j = 0; j < 3; ++j)
    > {
    > printf("%c", tolower(p[j]));
    > }
    > }
    > printf("\n");
    >
    > return 0;
    > }
    >
    > Be warned, I did not include checks to see whether any of the array
    > elements are long enough (i.e. have at least three characters).
    Vladimir Oka, May 18, 2006
    #6
  7. vim

    pete Guest

    Vladimir Oka wrote:
    >
    > vim wrote:
    > > hi guys
    > >
    > > this is my code
    > > #include<stdio.h>
    > > main()
    > > {
    > > char *p[]={"hello" ,"world" ,"Nice","show"};
    > > char **p1;
    > > }
    > > I want to print the string
    > > "helwornicsho" by using ptr increment or decrement.
    > > How can I do that.


    > I guess you want to print first three characters of each element of
    > `p`, possibly with the first one of each being forced to lowercase. In
    > which case, this should do you nicely:
    >
    > #include <stdio.h>
    > #include <stdlib.h>


    I think you mean "ctype" instead of "stdlib"

    >
    > int main(void)
    > {
    > char *p[]={"hello" ,"world" ,"Nice","show"};
    > int i,j;
    >
    > for (i = 0; i < (sizeof p / sizeof p[0]); ++i)
    > {
    > /* no error checking here */
    > for (j = 0; j < 3; ++j)
    > {
    > printf("%c", tolower(p[j]));
    > }
    > }
    > printf("\n");
    >
    > return 0;
    > }
    >
    > Be warned, I did not include checks to see whether any of the array
    > elements are long enough (i.e. have at least three characters).


    It could be a little more pointer incremental.

    /* BEGIN new.c */

    #include <stdio.h>
    #include <ctype.h>

    #define THREE 3

    int main(void)
    {
    char *p[] = {"hello" ,"world" ,"Nice","show"};
    char **p1;
    int x;

    for (p1 = p; *p1 != p[sizeof p / sizeof *p]; ++p1) {
    for (x = 0; x != THREE; ++x) {
    putchar(tolower(*(*p1)++));
    }
    }
    putchar('\n');
    return 0;
    }

    /* END new.c */


    --
    pete
    pete, May 18, 2006
    #7
  8. On 2006-05-18, vim <> wrote:
    > hi guys
    >

    You are already annoying me with your lack of capitals.

    > this is my code

    One sec, here's a cleaned up version:

    #include <stdio.h>
    int main(void)
    {
    char *p[]={"hello" ,"world" ,"Nice","show"};
    char **p1;

    p1 = p; /* As per your most recent post */
    /* You should have posted context */

    /* Actual code here */
    return 0;
    }

    > I want to print the string "helwornicsho" by using ptr increment or
    > decrement.

    What is "ptr"? I don't see it in your code.

    > How can I do that.
    > I tried to print charachter by charachter .

    Easier to set the fourth character of each string to '\0' and then
    printf ("%s", p), no?

    > But for that increment ptr pointing to each charachter .
    > I used (**p1)++.But it is giving segmentation fault.


    **p1 points to the letter h, which is part of a string literal. You
    don't need to increment that, and if you could you'd end up with 'i'
    instead of the next letter.

    Use *p1++ to jump to the next word, and p1++ to jump to the next
    letter. The parentheses are unnecessary.

    > Plz tell the error.
    >

    What does "plz" mean? That isn't in your code either.

    --

    Andrew Poelstra < http://www.wpsoftware.net/blog >
    Andrew Poelstra, May 18, 2006
    #8
  9. vim

    pete Guest

    pete wrote:
    >
    > Vladimir Oka wrote:
    > >
    > > vim wrote:
    > > > hi guys


    I forgot to mention,
    anyone who does your homework for you, isn't being your friend.

    > /* BEGIN new.c */
    > /* END new.c */


    But if you ask questions, they will be answered.

    --
    pete
    pete, May 18, 2006
    #9
  10. vim

    Vladimir Oka Guest

    pete wrote:
    > Vladimir Oka wrote:
    > >
    > > vim wrote:
    > > > hi guys
    > > >
    > > > this is my code
    > > > #include<stdio.h>
    > > > main()


    <snip>

    > > I guess you want to print first three characters of each element of
    > > `p`, possibly with the first one of each being forced to lowercase. In
    > > which case, this should do you nicely:
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>

    >
    > I think you mean "ctype" instead of "stdlib"


    Yes. Thanks. (I did notice myself, as well, albeit too late).

    <snip>

    > > Be warned, I did not include checks to see whether any of the array
    > > elements are long enough (i.e. have at least three characters).

    >
    > It could be a little more pointer incremental.


    It was deliberate. I wasn't going to do OP's homework. ;-)
    Vladimir Oka, May 18, 2006
    #10
  11. vim

    Thad Smith Guest

    Andrew Poelstra wrote:
    > On 2006-05-18, vim <> wrote:
    >
    > One sec, here's a cleaned up version:
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > char *p[]={"hello" ,"world" ,"Nice","show"};
    > char **p1;
    >
    > p1 = p; /* As per your most recent post */
    > /* You should have posted context */
    >
    > /* Actual code here */
    > return 0;
    > }
    >
    >
    >>I want to print the string "helwornicsho" by using ptr increment or
    >>decrement.
    >>...
    >>How can I do that.
    >>I tried to print charachter by charachter .

    >
    > Easier to set the fourth character of each string to '\0' and then
    > printf ("%s", p), no?


    Easier, possibly, but resulting in undefined behavior if the strings are
    string literals as defined above.

    --
    Thad
    Thad Smith, May 20, 2006
    #11
  12. On Thu, 18 May 2006 14:42:27 GMT, Andrew Poelstra
    <> wrote:

    > char *p[]={"hello" ,"world" ,"Nice","show"};
    > char **p1;
    >
    > p1 = p; /* As per your most recent post */
    > /* You should have posted context */

    <snip>
    > Use *p1++ to jump to the next word, and p1++ to jump to the next
    > letter. The parentheses are unnecessary.
    >

    p1++ or ++p1 to jump to the next word and
    (*p1)++ (parens necessary) or ++*p1 for next letter.

    - David.Thompson1 at worldnet.att.net
    Dave Thompson, May 29, 2006
    #12
  13. On 2006-05-29, Dave Thompson <> wrote:
    > On Thu, 18 May 2006 14:42:27 GMT, Andrew Poelstra
    ><> wrote:
    >
    >> char *p[]={"hello" ,"world" ,"Nice","show"};
    >> char **p1;
    >>
    >> p1 = p; /* As per your most recent post */
    >> /* You should have posted context */

    ><snip>
    >> Use *p1++ to jump to the next word, and p1++ to jump to the next
    >> letter. The parentheses are unnecessary.
    >>

    > p1++ or ++p1 to jump to the next word and
    > (*p1)++ (parens necessary) or ++*p1 for next letter.
    >
    > - David.Thompson1 at worldnet.att.net


    I tested my method in gcc. I think that you are correct
    about the parentheses (for style reasons, at the very least),
    but when I compiled the OP's code (which may have been
    different from the example code quoted), p1++ moved to the
    next letter, not word.

    --
    Andrew Poelstra < http://www.wpsoftware.net/blog >
    To email me, use "apoelstra" at the above address.
    It's just like stealing teeth from a baby.
    Andrew Poelstra, May 29, 2006
    #13
  14. vim

    Joe Wright Guest

    Andrew Poelstra wrote:
    > On 2006-05-29, Dave Thompson <> wrote:
    >> On Thu, 18 May 2006 14:42:27 GMT, Andrew Poelstra
    >> <> wrote:
    >>
    >>> char *p[]={"hello" ,"world" ,"Nice","show"};
    >>> char **p1;
    >>>
    >>> p1 = p; /* As per your most recent post */
    >>> /* You should have posted context */

    >> <snip>
    >>> Use *p1++ to jump to the next word, and p1++ to jump to the next
    >>> letter. The parentheses are unnecessary.
    >>>

    >> p1++ or ++p1 to jump to the next word and
    >> (*p1)++ (parens necessary) or ++*p1 for next letter.
    >>
    >> - David.Thompson1 at worldnet.att.net

    >
    > I tested my method in gcc. I think that you are correct
    > about the parentheses (for style reasons, at the very least),
    > but when I compiled the OP's code (which may have been
    > different from the example code quoted), p1++ moved to the
    > next letter, not word.
    >

    It's not for style. Remember p1 is a pointer which points to a pointer.
    p1++ will advance p1 to the next pointer.

    puts(*p1++); /* prints hello */
    puts(*p1--); /* prints world */
    puts(++(*p1)); /* prints ello */

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, May 29, 2006
    #14
  15. On 2006-05-29, Joe Wright <> wrote:
    > Andrew Poelstra wrote:
    >> On 2006-05-29, Dave Thompson <> wrote:
    >>> On Thu, 18 May 2006 14:42:27 GMT, Andrew Poelstra
    >>> <> wrote:
    >>>
    >>>> char *p[]={"hello" ,"world" ,"Nice","show"};
    >>>> char **p1;
    >>>>
    >>>> p1 = p; /* As per your most recent post */
    >>>> /* You should have posted context */
    >>> <snip>
    >>>> Use *p1++ to jump to the next word, and p1++ to jump to the next
    >>>> letter. The parentheses are unnecessary.
    >>>>
    >>> p1++ or ++p1 to jump to the next word and
    >>> (*p1)++ (parens necessary) or ++*p1 for next letter.
    >>>
    >>> - David.Thompson1 at worldnet.att.net

    >>
    >> I tested my method in gcc. I think that you are correct
    >> about the parentheses (for style reasons, at the very least),
    >> but when I compiled the OP's code (which may have been
    >> different from the example code quoted), p1++ moved to the
    >> next letter, not word.
    >>

    > It's not for style. Remember p1 is a pointer which points to a pointer.
    > p1++ will advance p1 to the next pointer.
    >
    > puts(*p1++); /* prints hello */
    > puts(*p1--); /* prints world */
    > puts(++(*p1)); /* prints ello */
    >

    That is true for the example code quoted, but the OP's code behaved
    differently. Oh well. I wasn't sure why it worked at the time, and
    it has long since been deleted, so I can't check my work.

    Anyway, I wasn't aware that parentheses were needed, so I learned
    something very important today. (That bug has burned me several times,
    and yet I never remember it). Thanks.

    --
    Andrew Poelstra < http://www.wpsoftware.net/blog >
    To email me, use "apoelstra" at the above address.
    It's just like stealing teeth from a baby.
    Andrew Poelstra, May 29, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    695
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    835
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,248
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    632
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    449
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page