Odko
Odko

Reputation: 151

Save string pointed by pointer to array

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"

Upvotes: 2

Views: 174

Answers (3)

varun
varun

Reputation: 1523

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");

Upvotes: 2

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.

Upvotes: 0

maartenb64
maartenb64

Reputation: 33

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 ...
        }
    }
}

Upvotes: 2

Related Questions