本文介绍了指针数组(第5.6章)例如,在C程序设计语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的理解,一旦指针被初始化为一个字符串常量,字符串不能被修改。我试着进行修改和程序崩溃。

it is my understanding that once a pointer is initialized to a string constant, the string cannot be modified. I've tried performing the modification and the program crashes.

这个理论是由Brian W. Kernighan和丹尼斯·里奇先生C程序设计语言的第5.5章字符指针和功能给出。

This theory is given at Chapter 5.5 "Character Pointers and Functions" in The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie.

然而有存储指针在阵列(第5.6章),其中指针的内容被修改的例子。如下面给出的方案:

However there is an example of storing pointers in arrays (Chapter 5.6) where the contents of the pointer is modified. The program is as given below:

#include <stdio.h>
#include <string.h>
#define MAXLINES 5000 /* max #lines to be sorted */
#define MAXLEN 1000

char *lineptr[MAXLINES]; /* pointers to text lines */
char *alloc(int);
int readlines(char *lineptr[], int nlines);
int getline(char *, int);
void writelines(char *lineptr[], int nlines);

main()
{
    int nlines; /* number of input lines read */
    if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
        writelines(lineptr, nlines);
        return 0;
    } else {
        printf("error: input too big to sort\n");
        return 1;
    }
}

/* readlines: read input lines */
int readlines(char *lineptr[], int maxlines)
{
    int len, nlines;
    char *p, line[MAXLEN];
    nlines = 0;
    while ((len = getline(line, MAXLEN)) > 0)
    if (nlines >= maxlines)
    return -1;
    else {
        line[len-1] = '\0'; /* delete newline */
        strcpy(p, line);
        lineptr[nlines++] = p;
    }
    return nlines;
}

/* writelines: write output lines */
void writelines(char *lineptr[], int nlines)
{
    int i;
    for (i = 0; i < nlines; i++)
    printf("%s\n", lineptr[i]);
}

int getline(char s[],int lim)
{
    int c, i;
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
    s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}

编译它们只是在第一行之后崩溃时程序不显示任何警告或错误。

The program does not show any warning or error when compiled but crashes after feeding the first line.

能否请您确认这是否是由于字符串常量初始化为一个指针修改?有问题的指针谢谢@ 35行上执行@ 28行的char * p和strcpy的。

Can you please confirm if this is due to modification of a string constant initialized to a pointer? The pointer in question is @ line 28 "char *p" and strcpy performed on it @ line 35. Thank you.

推荐答案

据崩溃此处

 strcpy(p, line);

由于 P 不是指向分配的内存;
你应该添加这样的事情

because p not points to allocated memory;you should add something like this

p = (char*)malloc(sizeof(char)* MAXLEN);

更新:

另外这里

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)

在字符串长度MAXLEN的情况下,用的'\\ n'你的端I 可能达到 LIM 值。然后

in case of string with length MAXLEN ended with '\n' your i could reach lim value. And then

if (c == '\n') {
    s[i] = c;
    ++i;
}

您将分配的'\\ n'到S [MAXLEN](LIM = MAXLEN),你会走出界外。 (指标应该是介于0和 MAXLEN - 1

you will assign '\n' to s[MAXLEN] (lim = MAXLEN) and you will go out-of-bounds. (indexes should be between 0 and MAXLEN - 1.

我建议修改

for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)

for (i=0; i < lim-2 && (c=getchar())!=EOF && c!='\n'; ++i)

这篇关于指针数组(第5.6章)例如,在C程序设计语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 08:45