Reputation: 79
Below is the implementation of strlen.c as per "The Standard C Library,
size_t strlen(const char *s){
const char *sc;
for(sc = s; *sc != '\0'; ++sc)
return (sc-s); }
Is my understanding of the legality of sc = s
correct?
sc=s
is a legal assignment because since both variables are declared as const
, both protect the object that is pointed to by s. In this case, it is legal to change where sc or s both point to but any assignment (or reference?) to *s
or sc
would be illegal.
Upvotes: 4
Views: 444
Reputation: 1189
I think what you are asking is what the const keyword means. If not please clarify your question.
The way I like to think of it is any const variable can be stored in ROM (Read Only Memory) and variables that are not declared const can be stored in RAM (Random Access Memory). This kind of depends on the kind of computer you are working with so the const data may not actually be stored in ROM but it could be.
So you can do anything you want with the pointer itself but you can not change the data in the memory it points to.
This means you can reference the pointer and pass that around as much as you like. Also you can assign a different value to the pointer.
Say you have this code
const char* foo = "hello";
const char* bar = "world";
Its perfectly legal to do
foo = bar;
Now both point "world"
Its also legal to do
const char *myPtr = bar;
myPtr = foo;
What you are not allowed to do is change the actual data memory so you are not allowed to do
foo[0] = 'J';
Upvotes: 5
Reputation: 153447
Is my understanding of the legality of sc = s correct?
Yes, only some detail on the last part needed.
... but any assignment (or reference?) to
*s
orsc
would be illegal.
(I suspect OP means "... or *sc
would be illegal.")
Referencing what s
or sc
points to is OK as in char ch = *sc;
Attempting to change the value of *s
or *sc
is undefined behavior (UB), not "illegal" as in *sc = 'x';
(See good additional detail by @rici)
With UB, the assignment may work, it might not on Tuesdays, code may crash, etc. It is not defined by C what happens. Certainty code should not attempt it.
Upvotes: 2
Reputation: 3677
You are correct.
const char * sc
declares a pointer to a const char
. In essence, it means that sc points to a variable of type char
(or in that case, a contiguous array of char
s) and that you cannot use sc
to modify the pointed variable. See it live here.
Note that sc
itself is not a const
variable. The const
applies to the pointed variable, and not to the pointer. You can thus change the value of the pointer, i.e. the variable to which it points.
Follow this answer to have more insight about the different uses of const
and pointers : What is the difference between const int*, const int * const, and int const *?
Upvotes: 2