What does const char *s mean? is s a constant or *s?

G

G Patel

I've been looking at some code for string functions (certain
implementation of them) and the non modified string is usually declared
as a const char *s in the parameter list.

I was wondering, what exactly does that mean. Does that mean the
function is restricted from changing the address (changing s) or
changing what's being pointed to (*s)?
 
C

Chris Torek

I've been looking at some code for string functions (certain
implementation of them) and the non modified string is usually declared
as a const char *s in the parameter list.

I was wondering, what exactly does that mean. Does that mean the
function is restricted from changing the address (changing s) or
changing what's being pointed to (*s)?

C's rule is that "declaration mirrors use", so "const char *s"
says that "*s" has type "const char" -- i.e., *s is a read-only
(but not unchanging) char.

See the comp.lang.c FAQ, question 11.9, et seq.

To see that "*s" does indeed change, compile and run the following
small program:

#include <stdio.h>

char buf[30];

void f(const char *);

int main(void) {
strcpy(buf, "hello world");
f(buf);
return 0;
}

void f(const char *s) {
printf("entered f(\"%s\")\n", s);
buf[0] = 'j';
printf("after changing buf[0], we now have \"%s\"\n", s);
}

Neither you nor the compiler can assume that *s, or indeed any
s, is unmodified between various statements unless you can prove
that that s is not modified by some *other* assignment.
 
M

Mike Wahler

G Patel said:
I've been looking at some code for string functions (certain
implementation of them) and the non modified string is usually declared
as a const char *s in the parameter list.

I was wondering, what exactly does that mean. Does that mean the
function is restricted from changing the address (changing s)
No.

or changing what's being pointed to (*s)?

Yes.

const char *s; /* non-const pointer to const char */
char const *s; /* non-const pointer to const char */
char * const s; /* const pointer to non-const char */
const char * const s; /* const pointer to const char */
char const * const s; /* const pointer to const char */

-Mike
 
M

Michael Mair

G said:
I've been looking at some code for string functions (certain
implementation of them) and the non modified string is usually declared
as a const char *s in the parameter list.

I was wondering, what exactly does that mean. Does that mean the
function is restricted from changing the address (changing s) or
changing what's being pointed to (*s)?

Please read the FAQ 11.9:

11.9: What's the difference between "const char *p" and
"char * const p"?

The comp.lang.c FAQ is available from
http://www.eskimo.com/~scs/C-faq/top.html

Reading more of section 11 will help you to understand what const
means in C.


Cheers
Michael
 
P

pete

G said:
I've been looking at some code for string functions (certain
implementation of them) and the non modified
string is usually declared as a const char *s in the parameter list.

A const char *s in the parameter list means that you should
expect the function not to change the string that s points to,
even though the function still could do that
by casting away the const qualifier.

char *str_cpy(char *s1, const char *s2)
{
char *const p1 = s1;

do {
*s1 = *s2++;
} while (*s1++ != '\0');
return p1;
}
 
J

Jonathan Burd

G said:
I've been looking at some code for string functions (certain
implementation of them) and the non modified string is usually declared
as a const char *s in the parameter list.

I was wondering, what exactly does that mean. Does that mean the
function is restricted from changing the address (changing s) or
changing what's being pointed to (*s)?

See what the compiler tells you when you try to compile it:
(gcc -pedantic -Wall -std=c89 -o complain complain.c)

/* complain.c */
#include <stdio.h>

void
replace_spaces_with_hyphens (/* >> */ const char *, const char *);

void
replace_spaces_with_hyphens (/* >> */ const char *buf, const char *str)
{
int ch;
while ((ch = *str))
{
if (*str == ' ')
ch = '-';
*buf = ch;
++buf;
++str;
}
*buf = '\0';
}

int
main (int argc, char **argv)
{
char buf[100];
const char *str = "foo licious";

replace_spaces_with_hyphens (buf, str);
puts (buf);
return 0;
}

Regards,
Jonathan.
 
K

Karthik Kumar

G said:
I've been looking at some code for string functions (certain
implementation of them) and the non modified string is usually declared
as a const char *s in the parameter list.

I was wondering, what exactly does that mean. Does that mean the
function is restricted from changing the address (changing s) or
changing what's being pointed to (*s)?

If you are working on a *nix-based machine, check if you
have access to the 'cdecl' utility. That should help to
understand these things better.


$ cdecl
Type `help' or `?' for help
cdecl> explain const char * s
declare s as pointer to const char
cdecl>

As a thumb rule, try to interpret by reading them
from right to left.

For that reason, some people find ,

' char const * s; '

much more readable when compared to

' const char * s; ' ,

but then that could be just a style preference.
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top