difference between char [] and const char[] as function param

Hello everyone!

I have here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void func( char l[] )
{

}

void func( const char l[] )
{

}

// just in case if it does make difference
void func( const char *l )
{

}


Now my question is what does the const really do?
I know that if it is const it means that variable can't be change.
I assume that in const case the variable what i pass into function, in function
i would not have that same function but it's copy, am i right?

Basically if i do:
func( "asd" );
without using the const then that char [] created by "" ... where does that
go when function is called and where it stays while im in function?

I just want to learn very basics before i start recoding my program again.
( dont want to recode it to wrong way )


Basically i want to prevent the memory leak and im worried when instead of
1
2
3
4
5
6
7
void main()
{
    char something[] = "asd"; // i know that this variable will be 
    // destroyed after main() function ends.

   func(something);
}


doing this:
1
2
3
4
void main()
{
   func("asd");
}

or perhaps they are exactly the same thing after compiler compiles?
Thanks!
Last edited on
In first case type of passed parameter is char* (actually it is char[4], but it is not important here). It can be passed to both types of func. It can be modified without any fear

In second case passed parameter is string literal of type const char*. It can only be received by const char* /const char[] functions. It cannot be changed and any attempt to do so will lead to UB and possible crash.

If your compiler is adequate and properly configured, it won't let you do that (however I suppose, your compiler is not, as it allows you to use illegal void main())

Basically if i do:
func( "asd" );
without using the const then that char [] created by "" ... where does that
go when function is called and where it stays while im in function?
String literals are stored with program image in read only memory.

I assume that in const case the variable what i pass into function, in function
i would not have that same function but it's copy, am i right?
Depends on how you pass it to the function:
1
2
3
4
5
6
some_function(      int  foo) // foo is a copy      of passed variable you can    change
some_function(const int  foo) // foo is a copy      of passed variable you cannot change

//Reference to variables are essentually variables themselves
some_function(      int& foo) // foo is a reference to passed variable you can    change
some_function(const int&  foo) // foo is a reference to passed variable you cannot change 
When you pass something to function it creeates new variable of type indicated in function argument list and initializes it with passed value.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
With pointers (your case) it is different. What const in different places means:
//You can change x (can point it to another variable)
//You can change value it points to. 
int* x; 

//You can change x (can point it to another variable)
//You cannot change value it points to.
const int* x; 

//You cannot change x (it always points to same variable)
//You can change value it points to. 
int* const x; 

//You cannot change x (it always points to same variable)
//You cannot change value it points to. 
const int* const x; 
read only memory will be always freed after the function is done, am i right?
Last edited on
read only memory will be always freed after the function is done
String literals are a part of the program. They are stored inside executable, loaded with programm startup and destroyed on program termination.

func("asd"); ← passes pointer to string literal stored somewhere to the function. String literal is exist before and after function call
char something[] = "asd"; ← creates character array and copies character from string literal to it. String literal exist before and after variable initialization.
Makes sense. Thank you!
Topic archived. No new replies allowed.