简体   繁体   English

保存指针指向数组的字符串

[英]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. 此处: matched.timelog="10:24:12"类的字符串。 And i want to save it to timelog[matchescount] . 我想将其保存到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] 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 ii store like this ? ii可以这样存储吗?

[0][0]="10:24:12" [0][1]="10:24:13" [0] [0] =“ 10:24:12” [0] [1] =“ 10:24:13”

[1][0]="10:24:14" [1][1]="10:24:15" [1] [0] =“ 10:24:14” [1] [1] =“ 10:24:15”

Assuming your timelog string always look like "hh:mm:ss", you can do 假设您的时间日志字符串始终看起来像“ hh:mm:ss”,则可以

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

just make your array 2D. 只需将阵列设为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 创建一个二维char数组,因为String是一个char数组,所以您只能在二维char数组中存储si1D字符串数组

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]; 这表明您在timelog [0]处有一个字符串“ abcde”;

to store 2d array of strings you need a 3D char array 要存储2D字符串数组,您需要3D char数组

timelog[maxline][noOfStrings][maxLengthOfEachString];

now you can store 2D array of strings. 现在您可以存储2D字符串数组。

strcpy(timelog[0][0],"abcd");
strcpy(timelog[0][1],"efgh");
strcpy(timelog[1][0],"ijkl");
strcpy(timelog[1][1],"xyz");

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. 通过在需要数组的任何地方执行此操作,可以避免许多等待发生的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM