By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,034 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

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

P: n/a
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)?

Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"G Patel" <ga********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Nov 14 '05 #2

P: n/a
In article <11**********************@z14g2000cwz.googlegroups .com>
G Patel <ga********@gmail.com> wrote:
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[i], is unmodified between various statements unless you can prove
that that s[i] is not modified by some *other* assignment.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #3

P: n/a
G Patel wrote:
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
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #4

P: n/a
G Patel wrote:

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;
}

--
pete
Nov 14 '05 #5

P: n/a
G Patel wrote:
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.

--
"Women should come with documentation." - Dave
Nov 14 '05 #6

P: n/a
G Patel wrote:
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.

--
Karthik.
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.