Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8281303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:58:58+00:00 2026-06-08T09:58:58+00:00

So, I know the differences between char const *, char * const, and char

  • 0

So, I know the differences between char const *, char * const, and char const * const. Those being:

char* the_string : I can change the char to which the_string points,
and I can modify the char at which it points.

const char* the_string : I can change the char to which the_string
points, but I cannot modify the char at which it points.

char* const the_string : I cannot change the char to which the_string
points, but I can modify the char at which it points.

const char* const the_string : I cannot change the char to which
the_string points, nor can I modify the char at which it points.

(from const char * const versus const char *?)

Now, my question is: Let’s say I’m writing a function that would not modify the C string that is passed to it, for example:

int countA(??? string) {
    int count = 0;
    int i;
    for (i=0; i<strlen(string); i++) {
         if (string[i] == 'A') count++;
    }
    return count;
}

Now, what should the header be?

int countA(char const * string); 
int countA(char const * const string);

My feeling is that I should use the second one, because I’m not going to modify the pointer itself, neither the contents of the array. But when I look to the header of standard functions they use the first one. Example

char * strcpy ( char * destination, const char * source );

Why?

(In fact char const * doesn’t really make sense to me, because if you’re thinking about the abstract string, either you are not modifying the string (so, char const * const because you are not modifying the pointer, neither the contents) or you will modify the string (so just char * because, you may modify the contents and you may need to allocate more memory, so you may need to modify the pointer)

I hope someone can make all this clear to me. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:58:59+00:00Added an answer on June 8, 2026 at 9:58 am

    In this case, it does not matter whether the pointer itself is const or not, because it is passed by-value anyway: Whatever strcpy does to source will not affect the caller’s variable, because strcpy will operate on a copy of the caller’s source on the stack, not the original. Note I am talking about the pointer value, not what the pointer points to, which should obviously not be changed since it is the source parameter.

    char dest[10];
    char const * source = "Hello";
    strcpy( dest, source );
    // no matter what strcpy does, the value of source will be unchanged
    

    Within strcpy, you need to iterate pointers over the arrays pointed to by destination and source anyway. Not declaring the parameters as const allows the function to use the values from the stack directly, without copying / casting them first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the differences between IBAction and IBOutlet, but I don't know when I
Can someone please explain the major differences between Scala, Groovy and Clojure. I know
I know the difference between the points-to (->) and dot (.) operator but I
I want to know the differences between SOA and OO, and why SOA is
I know that one of the differences between classes and structs is that struct
In clojure, I'd like to know what are the differences between the three below.
I know BOOL is actually a typedef of signed char, but what about Boolean?
I know the differences between data , newtype and type very well. I am
May I know the differences between application servers and web servers? Please list examples
I would like to know the differences between these two types of URLs: relative

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.