Typedefs and Constness

I have some confusion about pointers,typedefs and const. I have a function which takes a const charptr and prints that value. But when I try to compile I get some errors which I can't figure it out.
Thank you in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <sstream>
using namespace std;
typedef char* charptr;


int foe(const charptr);

int main(){

        string mystring("74");
        foe(mystring.c_str());

        return 0;
}

int  foe(const charptr var){

        int tmp;
        stringstream stream(var);
        stream >> tmp;
        cout << tmp << endl;
        return tmp;
}


ex7.cpp: In function int main():
ex7.cpp:18: error: invalid conversion from const char* to char*
ex7.cpp:18: error: initializing argument 1 of int foe(char*)



Last edited on
Well, int foe(const charptr); resolves to int foe(char *const); because the const applies to the outmost type (which is the pointer) of a typedef while mystring.c_str() returns const char *
Oh! Thats not very intuitive :(
Thank you very much by the way.
Last edited on
Topic archived. No new replies allowed.