簡體   English   中英

嘗試訪問C中的字符串數組的第一個字符時出現段錯誤

[英]Seg fault when trying to access first charecter of an array of strings in C

p是類似的東西

[["test"], ["lest"]]

它打印p [j]表示打印“測試”

char **p;
p = explode[i] = split(eachLineOfLsInArray[i]);
for(j=0;p[j];++j)
    puts(p[j]);

但是在嘗試打印p [j] [0]時給出段錯誤,這意味着試圖打印“ t”

char **p;
p = explode[i] = split(eachLineOfLsInArray[i]);
for(j=0;p[j];++j)
    puts(p[j][0]);

編輯:

這是整個代碼。 Lorem ipsum dolor坐下,一直保持着安靜的狀態。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char **split(const char *s);

int main (int argc, char *argv[]){
    char virtualLs[100];
    char eachLineOfLsInArray[100][100];
    scanf("%[^\t]", virtualLs);
    char *eachLineOfLs;
    eachLineOfLs = strtok(virtualLs, "\n");
    int loopCounterForStuffing;
    loopCounterForStuffing = 0;
    while (eachLineOfLs != NULL)
    {
        strcpy(eachLineOfLsInArray[loopCounterForStuffing], eachLineOfLs);
        eachLineOfLs = strtok(NULL, "\n");
        ++loopCounterForStuffing;
    }

    char **explode[sizeof(eachLineOfLsInArray)/sizeof(*eachLineOfLsInArray)];
    int i,j, n = sizeof(eachLineOfLsInArray)/sizeof(*eachLineOfLsInArray);
    int maximum = 0;
    int maximumindex = 0;
    int maximumname = 0;
    for(i=0;i < n; ++i){
        char **p;
        p = explode[i] = split(eachLineOfLsInArray[i]);
        for(j=0;p[j];++j)
        {
            putchar(p[j][0]);
            printf("%c", p[j][0]);
        }

    }
    return 0;
}

static int wordCount(const char *s){
    char prev = ' ';
    int wc = 0;
    while(*s){
        if(isspace(prev) && !isspace(*s)){
            ++wc;
        }
        prev = *s++;
    }
    return wc;
}

char **split(const char *s){
    int i, wc = wordCount(s);
    char *word, **result = calloc(wc+1, sizeof(char*));
    char *clone = strdup(s);//Note that you are copying a whole
    for(word=strtok(clone, " \t\n"); word; word=strtok(NULL, " \t\n")){
        result[i++] = word;//or strdup(word); and free(clone); before return
    }
    return result;
}

該代碼在split()初始化i並將打印循環限制為已讀取的數據(否則包括一些調試I / O,主要是為了展示另一種調試技術)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char **split(const char *s);

int main(void)
{
    char virtualLs[100];
    char eachLineOfLsInArray[100][100];
    scanf("%[^\t]", virtualLs);
    char *eachLineOfLs;
    eachLineOfLs = strtok(virtualLs, "\n");
    int loopCounterForStuffing;
    loopCounterForStuffing = 0;
    while (eachLineOfLs != NULL)
    {
        printf("each: <<%s>>\n", eachLineOfLs);
        strcpy(eachLineOfLsInArray[loopCounterForStuffing], eachLineOfLs);
        eachLineOfLs = strtok(NULL, "\n");
        ++loopCounterForStuffing;
    }
    printf("LoopC = %d\n", loopCounterForStuffing);

    char **explode[sizeof(eachLineOfLsInArray) / sizeof(*eachLineOfLsInArray)];
    int i, j, n = sizeof(eachLineOfLsInArray) / sizeof(*eachLineOfLsInArray);
    printf("n = %d\n", n);
    for (i = 0; i < loopCounterForStuffing; ++i)
    {
        char **p;
        p = explode[i] = split(eachLineOfLsInArray[i]);
        for (j = 0; p[j]; ++j)
        {
            putchar(p[j][0]);
            printf("%c", p[j][0]);
        }
        putchar('\n');
    }
    return 0;
}

static int wordCount(const char *s)
{
    char prev = ' ';
    int wc = 0;
    while (*s)
    {
        if (isspace(prev) && !isspace(*s))
        {
            ++wc;
        }
        prev = *s++;
    }
    return wc;
}

char **split(const char *s)
{
    int i = 0;
    int wc = wordCount(s) + 1;
    printf("Word count = %d\n", wc);
    char *word;
    char **result = calloc(wc + 1, sizeof(char *));
    char *clone = strdup(s);// Note that you are copying a whole
    for (word = strtok(clone, " \t\n"); word; word = strtok(NULL, " \t\n"))
    {
        printf("Word: <<%s>>\n", word);
        result[i++] = word;// or strdup(word); and free(clone); before return
    }
    result[i] = 0;
    return result;
}

樣本輸出:

$ ./split <<< "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
each: <<Lorem ipsum dolor sit amet, consectetur adipiscing elit.>>
LoopC = 1
n = 100
Word count = 9
Word: <<Lorem>>
Word: <<ipsum>>
Word: <<dolor>>
Word: <<sit>>
Word: <<amet,>>
Word: <<consectetur>>
Word: <<adipiscing>>
Word: <<elit.>>
LLiiddssaaccaaee
$ 

輸出是每個單詞的第一個字符,在結束的一行上打印兩次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM