Like Tree2Likes
  • 1 Post By brewbuck
  • 1 Post By Click_here

A constant question

This is a discussion on A constant question within the C Programming forums, part of the General Programming Boards category; The declaration for fopen is Code: FILE *fopen(const char *filename, const char *mode); Is filename a pointer to a memory ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    19

    Question A constant question

    The declaration for fopen is

    Code:
    FILE *fopen(const char *filename, const char *mode);
    Is filename a pointer to a memory location which is a constant char, but the memory address in filename can change? Or is filename a constant pointer to a fixed memory location which contains a character, which can change?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,821
    O_o

    You need to learn to read declarations.

    As declared, `filename' is a pointer to a constant character, but to be honest I'm not even sure what "but the memory address in filename can change" really means to you. The pointer to a constant character bit just says to you, as a client, that the `fopen' function will not change any values at the address. You can think of it as a promise.

    That said, the `fopen' function expects `filename' to be a pointer to a null terminated string of character values which, as above, it will not modify. So, really, neither of the things you think it says is correct.

    Soma

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    I think the question is about the difference between a pointer to a const, and a pointer which is const.

    In the example given, it is a pointer to a const. A pointer which is const would be written "char * const filename", but this is rarely used.

    Confusingly, people (including myself) often refer to a pointer to const as a "const pointer."
    uncreativename likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    Quote Originally Posted by brewbuck View Post
    I think the question is about the difference between a pointer to a const, and a pointer which is const.

    In the example given, it is a pointer to a const. A pointer which is const would be written "char * const filename", but this is rarely used.

    Confusingly, people (including myself) often refer to a pointer to const as a "const pointer."
    You understand the question right. Thanks.

    Just to be completely clear that I have it right: "const char" is a constant character. That is, it's a spot in memory which contains some character which and it will not change. Then filename is a pointer to that memory location. filename could be changed to point to another location that is declared for a char. Of course, based on the previous poster, that seems unlikely to occur).

    C is full of lots of strange syntax and phrases, which makes it difficult to learn.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by uncreativename View Post
    "const char" is a constant character.
    IMHO it's better to think of "const" meaning "read-only" instead of "constant" (that's a little design glitch in the language) because you can't use a const object as a constant expression.
    Consider:
    Code:
    void function(int n)
    {
        const int x = n * 2;
        enum values { alpha = x, beta = 2 * x, gamma = 3 * x } test;    // This doesn't work
        ...
        switch (...)
        {
               case x: ....      // neither does this 
    }
    Although "x" is declared as const, you can't use it for example as a case label (only "real" constant expressions are allowed like integer constants or macros). The reason is that "x" is only constant during its lifetime which is shorter than the program execution time.

    Bye, Andreas

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,266
    Quote Originally Posted by AndiPersti View Post
    IMHO it's better to think of "const" meaning "read-only" instead of "constant" (that's a little design glitch in the language) because you can't use a const object as a constant expression.
    Or even more importantly, because the value can actually change. i.e.
    Code:
    void foobar(const int *c, int *nc)
    {
        printf("before %d\n", *c);
        ++(*nc);
        printf("after %d\n", *c);
    }
    
    int main(void)
    {
        int x = 3;
        foobar(&x, &x);
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,304
    I think the question is about the difference between a pointer to a const, and a pointer which is const
    This link has helped a lot of people of the years

    http://untroubled.org/articles/cdecls.txt
    uncreativename likes this.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    Quote Originally Posted by Click_here View Post
    This link has helped a lot of people of the years

    http://untroubled.org/articles/cdecls.txt
    That's a kick ass document (that's my slang for "good - really good")!
    The title of it is Reading C Declarations: A Guide for the Mystified. The author gives a procedure, and examples (which are rare in the linux/c world), for how to interpret declarations.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    5,871
    Quote Originally Posted by uncreativename View Post
    Just to be completely clear that I have it right: "const char" is a constant character.
    Uhhh .... sort of.

    const does not mean "constant" (as in forever invariable, unchanging, immutable, etc). It means that there are constraints in the language that prevent or discourage accidental change (for example, by triggering a compilation error). constness is a constraint on what is allowed, not an inviolable law.

    In general terms, the const keyword in a declaration applies to whatever is on its left. Unless nothing is to its left, in which case, the const applies to whatever is to its right. So "const char *filename" is equivalent to "char const *filename" and means that filename is a "pointer to const" or (more precisely) "pointer to const char". Whereas, char * const filename means that filename is a "const pointer to char".

    Quote Originally Posted by uncreativename View Post
    C is full of lots of strange syntax and phrases, which makes it difficult to learn.
    A lot of people say that about English too, usually in comparison with other languages.

    The C language is a complex one, no doubt. But, with effort to understand, there is logic to it. In programming language design there is often a trade-off between language complexity and expressive power.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constant
    By UserName112 in forum C++ Programming
    Replies: 12
    Last Post: 10-08-2011, 10:06 AM
  2. question about constant variable in c/c++
    By fatshaw in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2010, 09:16 PM
  3. Why make a constant a constant again?
    By Overworked_PhD in forum C Programming
    Replies: 3
    Last Post: 11-03-2007, 06:57 PM
  4. Constant value not known
    By franchan in forum C Programming
    Replies: 6
    Last Post: 06-12-2007, 11:48 PM
  5. Constant pointer to constant value
    By tretton in forum C Programming
    Replies: 10
    Last Post: 12-23-2005, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21