Problem with char** typed function

Hi!

I got the task where I must get first and middle name of authors from a .txt file and sort them into a char array, one column for first and the other for middle names, using this function: char** wrs_lst(char *filename) (wrs_lst -> writers list).

However, there is a problem that I just can't get over: whenever I try to run the .cpp file in cmd, I receive this message:

error: cannot convert 'char*' to 'char**' in initialization
char** wrs_lst(arr);

Here's my code so far - it's not complete, cause being stuck at this char** problem drives me crazy :S

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<iostream>
#include<string>
#include<cstring>
#include<fstream>

char** wrs_lst(char *fn);
char** createMtx(int n, int m);
void deleteMtx(char** mtx, int n, int m);
int findNthOccur(std::string str, char ch, int N);

int main(){
	
	std::string s = "authors.txt";
	int l = s.length();
	char arr[l + 1];
	
	strcpy(arr, s.c_str());

	char** wrs_lst(arr);
	
	return 0;
}

char** wrs_lst(char *fn){
	
	int m = 2; //2 columns for first and middle name
	std::string filename = fn;
	int wnum = 0;
	int zero = 0;
	std::string str;
	int counter = 0;
	
	std::ifstream file(filename);
	if(file.is_open()){
		while(!file.eof()){
			while(counter < 1){
				std::getline(file, str);
				wnum = std::stoi(str) + zero;
			}
			
			std::getline(file, str);
			char** list = createMtx(wnum, m);
			
			int space = findNthOccur(str, ' ', 1);
			int shossz = str.length();
			for(int i = 0; i < wnum; i++){
				list[i][0] = str.substr(0, space);
				
				list[i][1] = str.substr(space+1, shossz-space);
			}
			
		}
		file.close();
	}else{
		std::cout << "Unable to open file!\n";
	}
	
	
}

char** createMtx(int n, int m){

	char** mtx = new char*[n];
	if(mtx){
		for(int i = 0; i<n; i++){
			mtx[i] = new char[m];
			if(!mtx[i]){
				exit(1);
			}
		}
	}else{
		exit(1);
	}
	
	return mtx;
}

void deleteMtx(char** mtx, int n, int m){
	
	for(int i = n-1; i != 0; i--){
		delete[] mtx[i];
	}
	delete[] mtx;
	
}

int findNthOccur(std::string str, char ch, int N){
    int occur = 0;

    for (unsigned int i = 0; i < str.length(); i++) {
        if (str[i] == ch) {
            occur += 1;
        }
        if (occur == N)
            return i;
    }
    return -1;
}
Last edited on
Look at line 19:

char** wrs_lst(arr);

Here, you're declaring a char** variable called wrs_lst, and trying to initialise it to the value of arr. However, arr is a char*, not a char**, which is not a compatible type.

I'm guessing what you meant to do there was call the function wrs_lst(), but that's not the correct syntax for calling a function.

By the way, when you're telling us about compiler errors, it's really helpful if you actually tell us what lines are causing those errors, rather than editing that information out of the message.
Last edited on
Aaa I'm such an idiot! And yeah, I wanted to call the function, I don't know why I wrote char** in front of it... thanks for the help, it means a lot!!

Also, alright, my bad, I'll next time tell what lines are causing the error :D

Thanks again!!
You're welcome - glad I could help! It's an easy mistake to make. In fact, you're lucky there was something for the compiler to complain about; if the types had been compatible, your code would have compiled without errors, and finding the bug would have been harder :)
Topic archived. No new replies allowed.