const problem

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 ?
 
A

Andrey Tarasevich

arnuld said:
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 ?

No.

What you should change is not the parameter declaration.

What you should change is your local variable declaration from

char* p = s;

to

const char* p = s;

I'm surprised you even have to ask this question. You declared the
parameter correctly. How come you can't declare the local variable in
_exactly_ the same way? Is this your code?
 

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

Similar Threads

Const Issue 2
Parsing a string 44
Fibonacci 0
Finding a word inside a string 35
K&R2, exercise 5-4, strend(s,t) 15
wcstombs() problem 16
Stack using Array 16
pointer array problem? 7

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top