Printing an array

A

arnuld

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.
 
S

Seebs

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
 
N

Nick Keighley

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top