473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
6 27744
"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
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 (40°39.22'N, 111°50.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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Richter | last post by:
What does the "." mean in the following sql script stmts? use GO if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table ....
3
by: RobertTG | last post by:
Someone please translate the code below into English... Particularly the indicated line Thanks function attachComment() { var aForms = document.getElementsByTagName("FORM"); for (var i = 0;...
6
by: allenj | last post by:
DB21085I Instance "md" uses "32" bits and DB2 code release "SQL08012" with level identifier "02030106". Informational tokens are "DB2 v8.1.0.16", "s030508", "MI00048", and FixPak "2". Product is...
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
45
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.