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

From: Jonathan Burd (jonathan.burd_at_REMOVEMEgmail.com)
Date: 01/24/05


Date: Mon, 24 Jan 2005 14:44:15 +0530

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


Relevant Pages

  • Re: fields for methods?
    ... but only by a compiler that is allowed to ... struct A {void foo();}; ... int static_instance i = 0; ... Is not possible because foo is the only member of A... ...
    (comp.programming)
  • Re: fields for methods?
    ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
    (comp.programming)
  • Re: Virtual Machine implementation problem, Please help me to spot the bug
    ... This is non-standard (Conceivably your compiler allows it ... tmp1 might not be correctly aligned for u32. ... void change_endian{ ... typedef unsigned int u32; ...
    (comp.lang.c)
  • Re: C Questions
    ... No prototype is given for this function, so the compiler cannot ... To prototype a function with no parameters, use void: ... %d takes an int argument, ...
    (comp.lang.c)
  • Re: The_Sage & void main()
    ... shall have a return type of type int but otherwise in all other respects ... > main may have the return type void then main may indeed have the ... If the compiler accepts void main, then you may use void main ... about whether void mainis conforming or not. ...
    (comp.lang.cpp)