I'm trying to save string from pointer to array. But my code outputs segmentation fault. Here's my code:
char timelog[maxline];
matchescount = 0;
while ((read = getline(&line, &len, fp)) != -1) {
struct matches matched = check_match(line,lgd,grd);
if (matched.status==1)
{
strcpy(timelog[matchescount],matched.timelog);
matchescount++;
}
}
Here: matched.timelog="10:24:12" like string. And i want to save it to timelog[matchescount] . So i want this from timelog array:
timelog[0]="10:24:12"
timelog[1]="10:24:13"
UPDATE: Can i store 2d array of strings ?
char timelog[maxline][255]
creates
[0][0]="1" [0][1]="0" [0][2]=":" [0][3]="2" [0][4]="4" [0][5]=":" [0][6]="1" [0][7]="2"
[1][0]="1" .......
right ?
Can i i store like this ?
[0][0]="10:24:12" [0][1]="10:24:13"
[1][0]="10:24:14" [1][1]="10:24:15"
timelogis an array ofchars not an array of strings. Usestrncpyto avoid buffer overruns. Tag either C or C++ (they're different languages).std::vector<std::string>char timelog[maxline][20];. Your currenttimelogcan hold 1 string not many strings.