Printing an array

Discussion in 'C Programming' started by arnuld, Nov 13, 2009.

  1. arnuld

    arnuld Guest

    WANTED: To print an array elements 1 by 1 and stop on on the basis of
    last NULL element.

    GOT: print2_char() function Segfaults





    #include <stdio.h>
    #include <string.h>


    void print1_arr(char*);
    void print2_arr(char*);


    int main(void)
    {
    char arrc[] = "arnuld";
    print1_arr(arrc);
    print2_arr(arrc);

    return 0;
    }


    void print1_arr(char* p)
    {
    int i;
    printf("Inside %s:\n", __func__);

    for( i = 0; i < 10; ++i, ++p)
    {
    printf("%c", *p);
    }

    printf("-------------\n\n");
    }



    void print2_arr(char* q)
    {
    printf("Inside %s:\n", __func__);

    for( ; q; ++q)
    {
    printf("%c", *q);
    }


    printf("-------------\n\n");
    }



    Using %s to print the array inside any of the the functions works fine.
    What I did not get is when I defined an array using a string constant
    then it automatically adds NULl to the end. If it is so then why the
    condition in for loop of print2_char() does not work ? It should stop
    printing when p is NULL but it Segfaults.





    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
     
    arnuld, Nov 13, 2009
    #1
    1. Advertising

  2. arnuld

    arnuld Guest

    > On Fri, 13 Nov 2009 07:34:02 +0000, arnuld wrote:


    > ...SNIP...


    > void print2_arr(char* q)
    > {
    > printf("Inside %s:\n", __func__);
    >
    > for( ; q; ++q)



    it should be: for( ; *q; ++q)

    Eh.. this is the source of whole issue. How can a pointer be NULL when a
    value is assigned to it. Silly me.




    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
     
    arnuld, Nov 13, 2009
    #2
    1. Advertising

  3. arnuld

    Seebs Guest

    On 2009-11-13, arnuld <> wrote:
    > for( ; q; ++q)


    You are testing whether or not q is a null pointer. You probably want to test
    whether the thing POINTED TO by q is a null CHARACTER.

    Maybe:
    for (; *q; ++q)

    > printing when p is NULL but it Segfaults.


    Pointers don't become null at the end of a string, they become pointers
    to a null byte. Not the same thing.

    Also, you used q, not p. C is a fussy language, and you have to get things
    right; you can't do something approximately like what you mean and assume
    it'll guess.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Nov 13, 2009
    #3
  4. On 13 Nov, 07:34, arnuld <> wrote:

    > WANTED: To print an array elements 1 by 1 and stop on on the basis of
    > last NULL element.


    this is the root of your problem (or one of the roots anyway!). You
    need to distinguish the null-pointer-constant, the NULL macro (the
    NULL macro is a null-pointer-constant) and the null character
    (sometimes spelled nul or '\0').

    You are less likely to get into trouble if you use explicit tests

    if (p == NULL)
    /* p is a null pointer */

    if (*p == '\0')
    /* p points to a nul character */

    if (p == '\0')
    /* the ith element of p is nul */

    > GOT:   print2_char() function Segfaults
    >
    > #include <stdio.h>
    > #include <string.h>
    >
    > void print1_arr(char*);
    > void print2_arr(char*);


    if you define print1_arr() and print2_arr() before main() then you
    don't need the prototypes.

    > int main(void)
    > {
    >   char arrc[] = "arnuld";
    >   print1_arr(arrc);
    >   print2_arr(arrc);
    >
    >   return 0;
    >
    > }
    >
    > void print1_arr(char* p)


    const char* would be better

    > {
    >   int i;
    >   printf("Inside %s:\n", __func__);
    >
    >   for( i = 0; i < 10; ++i, ++p)


    how many printable characters are there in p? How many characters does
    this loop print?

    >     {
    >       printf("%c", *p);
    >     }
    >
    >   printf("-------------\n\n");
    >
    > }
    >
    > void print2_arr(char* q)
    > {
    >   printf("Inside %s:\n", __func__);
    >
    >   for( ; q; ++q)


    you've already worked out what is wrong here...

    >     {
    >       printf("%c", *q);
    >     }
    >
    >   printf("-------------\n\n");
    >
    > }
    >
    > Using %s to print the array inside any of the the functions works fine.
    > What I did not get is when I defined an array using a string constant
    > then it automatically adds NULl to the end.


    no. It places a nul character at the end


    > If it is so then why the
    > condition in for loop of print2_char() does not work ?  It should stop
    > printing when p is NULL but it Segfaults.


    there is no p in print2_arr() and q is never NULL
     
    Nick Keighley, Nov 13, 2009
    #4
    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. Jon B
    Replies:
    7
    Views:
    7,555
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. JustSomeGuy
    Replies:
    13
    Views:
    519
    msalters
    Dec 9, 2004
  3. iffy agbim
    Replies:
    1
    Views:
    114
    Mark Andrews
    May 24, 2004
  4. iffy agbim
    Replies:
    0
    Views:
    113
    iffy agbim
    May 21, 2004
Loading...

Share This Page