const problem

Discussion in 'C Programming' started by arnuld, Aug 25, 2010.

  1. arnuld

    arnuld Guest

    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.
     
    arnuld, Aug 25, 2010
    #1
    1. Advertising

  2. 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
     
    Andrey Tarasevich, Aug 25, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,108
  2. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,122
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    542
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page