Hi guys,
I am kinda confused with extern const and const.
Please help to explain the usage of both.
I am having compilation/linking error when using extern const and const.
The code are written in c but I am using c++ compiler as the project contains both c and c++ codes.
In svc_tone.h
Code:
/* Function prototype */
void svcRequestTone(int, int *);
....
In test.c
Code:
#include "svc_tone.h"
....
const int tone_table[] ={1, 2, 3};
In test_user_defs.h
Code:
#include "svc_tone.h"
....
/* Basic Data Types */
extern const int tone_table[];
In test.h
Code:
#include "test_user_defs.h"
....
....
/* Function prototype */
void functionABC(int TaskID);
...
....
In test_function.c
Code:
#include "test.h"
....
....
...
...
void functionABC(int TaskID)
{
svcRequestTone(TaskID, tone_table); // line 200
}
When I compiled it, the error I got in test_function.c, at line 200:-
"argument of type "const int *" is incompatible with parameter of type "int *" tone_table "
I tried putting a "const" at the function "svcRequestTone" argument list, hence the function is now becomes:-
In svc_tone.h
Code:
/* Function prototype */
void svcRequestTone(int, const int *);
....
and it works...this managed to solve the error!!
I just wonder why the "const" keyword is required??Can anyone please tell me?
Next, after solving the above compilation error, I had a linking error that says undefined symbol for "tone_table".
In order to solve it, I modified the test.c by adding "extern" keyword to the declaration.
In test.c
Code:
#include "svc_tone.h"
....
extern const int tone_table[] ={1, 2, 3};
And guess what, it builds sucessfully!!
Can anyone tell me, would it cause any problems by adding the "extern" to its declarartion?
Why is it an "extern const" is needed in the declaration instead of just the "const"?
(why both .c and .h need to be "extern const"?).
Please help!!
Thanks guys!!!!