Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Printing an array

Reply
Thread Tools

Printing an array

 
 
arnuld
Guest
Posts: n/a
 
      11-13-2009

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.

 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      11-13-2009
> 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.

 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      11-13-2009
On 2009-11-13, arnuld <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-13-2009
On 13 Nov, 07:34, arnuld <(E-Mail Removed)> 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[i] == '\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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 2 11-27-2010 12:12 PM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:52 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:50 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-21-2007 05:28 AM
brochure printing,online yearbook,printing,books printing,publishing elie Computer Support 0 08-18-2007 10:11 AM



Advertisments