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