Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > const problem

Reply
Thread Tools

const problem

 
 
arnuld
Guest
Posts: n/a
 
      08-25-2010
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 ?



--
www.lispmachine.wordpress.com
my email is @ the above blog.

 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      08-25-2010
arnuld wrote:
>
> 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?

--
Best regards.
Andrey Tarasevich
 
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
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments