A
arnuld
This program compiles with a warning and of course it is working and
running fine:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
VAL_FALSE = 0,
VAL_TRUE = 1
};
int array_has_two_newlines_together(const char* s);
int main(void)
{
char arr1[] = "This has 2 newlines together\n\n";
char arr2[] = "This has 2 newlines \n but not together\n";
char arr3[] = "This has only one newline\n";
char arr4[] = "This does not have any newlines";
if(VAL_TRUE == array_has_two_newlines_together(arr1))
{
printf("arr1 has 2 newlines together\n");
}
if(VAL_TRUE == array_has_two_newlines_together(arr2))
{
printf("arr2 has 2 newlines together\n");
}
if(VAL_TRUE == array_has_two_newlines_together(arr3))
{
printf("arr3 has 2 newlines together\n");
}
if(VAL_TRUE == array_has_two_newlines_together(arr4))
{
printf("arr4 has 2 newlines together\n");
}
return 0;
}
int array_has_two_newlines_together(const char* s)
{
char* p = s;
for(; *p; ++p)
{
if( ('\n' == *p) && ('\n' == *(p+1)) )
{
return VAL_TRUE;
}
}
return VAL_FALSE;
}
====================== OUTPUT ============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra two-newlines.c
two-newlines.c: In function ‘array_has_two_newlines_together’:
two-newlines.c:51: warning: initialization discards qualifiers from
pointer target type
[arnuld@dune programs]$ ./a.out
arr1 has 2 newlines together
I understand the warning easily. Line 51 is:
char* p = s; (in array_has_two_newlines_together())
I am trying to initialize a non-const pointer from a const one. In what
way I should write my function to stop this warning. The array has to be
const because I am not thinking of modifying it at all. Just reading/
browsing through it comparing elements. I changed the argument
declaration from:
const char* --> char *const
and now it compiles without warning but is it the correct thing to do ?
running fine:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
VAL_FALSE = 0,
VAL_TRUE = 1
};
int array_has_two_newlines_together(const char* s);
int main(void)
{
char arr1[] = "This has 2 newlines together\n\n";
char arr2[] = "This has 2 newlines \n but not together\n";
char arr3[] = "This has only one newline\n";
char arr4[] = "This does not have any newlines";
if(VAL_TRUE == array_has_two_newlines_together(arr1))
{
printf("arr1 has 2 newlines together\n");
}
if(VAL_TRUE == array_has_two_newlines_together(arr2))
{
printf("arr2 has 2 newlines together\n");
}
if(VAL_TRUE == array_has_two_newlines_together(arr3))
{
printf("arr3 has 2 newlines together\n");
}
if(VAL_TRUE == array_has_two_newlines_together(arr4))
{
printf("arr4 has 2 newlines together\n");
}
return 0;
}
int array_has_two_newlines_together(const char* s)
{
char* p = s;
for(; *p; ++p)
{
if( ('\n' == *p) && ('\n' == *(p+1)) )
{
return VAL_TRUE;
}
}
return VAL_FALSE;
}
====================== OUTPUT ============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra two-newlines.c
two-newlines.c: In function ‘array_has_two_newlines_together’:
two-newlines.c:51: warning: initialization discards qualifiers from
pointer target type
[arnuld@dune programs]$ ./a.out
arr1 has 2 newlines together
I understand the warning easily. Line 51 is:
char* p = s; (in array_has_two_newlines_together())
I am trying to initialize a non-const pointer from a const one. In what
way I should write my function to stop this warning. The array has to be
const because I am not thinking of modifying it at all. Just reading/
browsing through it comparing elements. I changed the argument
declaration from:
const char* --> char *const
and now it compiles without warning but is it the correct thing to do ?