2

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"

6
  • 2
    timelog is an array of chars not an array of strings. Use strncpy to avoid buffer overruns. Tag either C or C++ (they're different languages). Commented Dec 21, 2014 at 5:09
  • Which is it, C or C++? If it's C++, use std::vector<std::string> Commented Dec 21, 2014 at 5:11
  • Sorry it's C. So how should i declare timelog ? Commented Dec 21, 2014 at 5:13
  • Try char timelog[maxline][20];. Your current timelog can hold 1 string not many strings. Commented Dec 21, 2014 at 5:26
  • 1
    @CoolGuy That worked ! :) You're really are Cool Guy :) Commented Dec 21, 2014 at 5:33

3 Answers 3

2

just make your array 2D. The error was because of your array is 1D so you ccan store only one string int that array.To Store multiple make the following changes.

 char timelog[maxline][10];
  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++;
    }
  }

UPDATE:

char timelog[maxline][255]

creates a 2d array of char.As String is an array of chars you can store only si1D array of string in 2d array of char

timelog[0][0]='a';
timelog[0][1]='b';
timelog[0][2]='c';
timelog[0][3]='d';
timelog[0][4]='e';

this indicates you have a string "abcde" at timelog[0];

to store 2d array of strings you need a 3D char array

timelog[maxline][noOfStrings][maxLengthOfEachString];

now you can store 2D array of strings.

strcpy(timelog[0][0],"abcd");
strcpy(timelog[0][1],"efgh");
strcpy(timelog[1][0],"ijkl");
strcpy(timelog[1][1],"xyz");
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming your timelog string always look like "hh:mm:ss", you can do

#define MAXTIMELOG 9
#define MAXENTRIES 1000


char timelog[MAXENTRIES][MAXTIMELOG];
matchescount = 0;
while ((read = getline(&line, &len, fp)) != -1) {
    struct matches matched = check_match(line, lgd, grd);
    if (matched.status==1)
    {
        strncpy(timelog[matchescount], matched.timelog, MAXTIMELOG);
        timelog[matchescount][MAXTIMELOG-1] = 0;
        if (++matchescount == MAXENTRIES) {
            ... deal with full array ...
        }
    }
}

Comments

0

Actually, you shouldn't copy the strings to a preallocated array. Use dynamic arrays instead:

size_t timelogSize = 8, matchescount = 0;
char** timelog = malloc(timelogSize*sizeof(*timelog));
while ((read = getline(&line, &len, fp)) != -1) {
    struct matches matched = check_match(line,lgd,grd);
    if (matched.status==1) {
        if(matchescount == timelogSize) {
            timelogSize *= 2;
            timelog = realloc(timelog, timelogSize*sizeof(*timelog));
        }
        timelog[matchescount++] = strdup(matched.timelog);
    }
}

This has the big advantage that you can handle input strings of arbitrary size and count. By doing this everywhere you need an array, you avoid a lot of bugs that are just waiting to happen.

8 Comments

Instead of multiplying timelogSize with 2,shouldnt you be adding 8 to it? timelogSize += 8;
Thanks ;). It only work when matched.timelog's size is 8 right ?. What if it's size always changes ?
@varun: No, it should not be timelogSize += 8;. A fixed increment would give the algorithm a quadratic order, a fixed factor ensures that the algorithm stays O(N).
@akiD I have not looked at struct matches, so I didn't change how it's used. However, if matched.timelog is an array (which I suspect), I would want to replace it with a char* as well.
@cmaster How do i free timelog ? when i try free(timelog) it shows double free or corruption (!prev)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.