- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
事实上,我已经快完成练习5-13了,但是出现了一个很奇怪的问题,我找不到我犯了什么错误:当我删除代码的第 13 行时,即printf("%d\n",argc);我的程序无法正常运行,并且遇到一个问题:当我输入 ./tail -10
#include <stdio.h>
#define MAXSIZE 1000
/* prints the last linesof its input,and the n's default value is 10 */
int main(int argc, char *argv[])
{
int atoi(char*);
int readlines(char *lineptr[], int maxlines);/* read the input to the lineptr */
int n = 10;
int i;
char *lineptr[MAXSIZE];/* store the the point of input lines */
int lines;
printf("%d\n",argc);
while(--argc > 0){
if((*++argv)[0] == '-') {
n = atoi(++*argv);
printf("%d\n",n);
}
else {
printf("Error input\n");
return -1;
}
}
lines = readlines(lineptr,MAXSIZE);// lines is the the actual lines of input */
if(lines > n) /* print ten the last n lines */
for(i = lines - n;i <= lines -1;i++)
printf("%s\n",lineptr[i]);
else
for(i=0;i < lines;i++) /* print all the input */
printf("%s\n",lineptr[i]);
return 0;
}
readline.c
#include <stdio.h>
#include <string.h>
#define MAXLEN 1000 /* max length of any input line */
int getLine(char *, int);
char *alloc(int);
/* readlines: read input lines */
int readlines(char *lineptr[], int maxlines)
{
int len,nlines;
char *p,line[MAXLEN];
while((len = getLine(line,MAXLEN)) > 0)
if(nlines >= maxlines || (p = alloc(len)) == NULL)
return -1;
else {
line[len-1] = '\0';
strcpy(p,line);
lineptr[nlines++] = p;
}
return nlines;
}
int getLine(char *s, int lim)
{
int i;
int c;
for(i = 0;i<lim-1 && (c=getchar())!=EOF && c != '\n';i++)
*s++ = c;
if(c=='\n') {
*s++ = c;
i++;
}
*s = '\0';
return i;
}
分配.c
#define ALLOCSIZE 10000
static char allocbuf[ALLOCSIZE];
static char *allocp = allocbuf;
char *alloc(int n)
{
if(allocbuf + ALLOCSIZE - allocp >=n) {
allocp += n;
return allocp -n;
}
else
return 0;
}
atoi.c
#include <ctype.h>
int atoi(char *p)
{
int sign,n;
int c;
while(isspace(*p))
p++;
if((c = (*p)) == '+' || c == '-')
p++;
sign = c == '-' ? -1 : 1;
for(n = 0;isdigit(c=*p);p++)
n = n * 10 + (c - '0');
return n * sign;
}
最佳答案
您在 readLines
中有一个未初始化的变量:
int len,nlines; // nlines not initialized
char *p,line[MAXLEN];
while((len = getLine(line,MAXLEN)) > 0)
// nlines used here
if(nlines >= maxlines || (p = alloc(len)) == NULL)
未初始化的局部变量具有未定义的内容,因此尝试使用该值会导致 undefined behavior .
声明时需要设置nlines=0
。
我能够通过 valgrind 快速找到这个问题,这给了我以下输出:
==8219== Conditional jump or move depends on uninitialised value(s)
==8219== at 0x40076A: readlines (x1.c:48)
==8219== by 0x4006CE: main (x1.c:27)
==8219==
==8219== Use of uninitialised value of size 8
==8219== at 0x4007C2: readlines (x1.c:53)
==8219== by 0x4006CE: main (x1.c:27)
==8219==
关于c - 当我用printf删除不相关的句子时,我的程序将无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35509790/
我想知道以下语句在 C 中会打印什么? printf("hello\n") || (printf("goodbye\n") || printf("world\n")); 我通常习惯于使用“cout”在
这是我目前正在学习的系统编程类(class)的幻灯片: catch_child 是 SIGCHLD 处理程序。输出与代码如何对应?为什么没有打印一些“Child #x started”消息? 最佳答案
这有点像拼图……我刚刚又回到了C,打算这次掌握它。所以我一直在阅读 The C Programming Language ,我得到了这个声明: Among others, printf also re
如何使用 printf 在字符串末尾附加空格? 我找不到任何在右侧附加空格的示例。 A similar question我发现使用 printf改为在字符串的左侧添加空格。 最佳答案 使用负数左对齐(
我想通过 usart 从 stm32f405 注销。 在我的 syscall.c 文件中,我实现了通过 usart 打印的功能: int _write(int file, char *ptr, int
我想定义一个记录器函数,比如 myPutStrLn = putStrLn . (++) "log: " main = do myPutStrLn "hello" 这很好。现在我想用 printf 格式
Printf module API详细介绍了类型转换标志,其中: %B: convert a boolean argument to the string true or false %b: conv
@H2CO3 这是我的主要代码: #pragma OPENCL EXTENSION cl_ amd_ printf : enable #define PROGRAM_FILE "matvec.cl"
Printf module API详细介绍了类型转换标志,其中: %B: convert a boolean argument to the string true or false %b: conv
您可以使用 printf 字段宽度说明符截断字符串: printf("%.5s", "abcdefgh"); > abcde 不幸的是,它不适用于数字(将 d 替换为 x 是相同的): printf(
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 4 年前。 Improve th
我遇到了我见过的最奇怪的错误之一。 我有一个简单的程序,可以打印多个整数数组。 对数组进行排序,然后打印...... place_in_buf(n100, 100); insertion(10
我的程序是每隔一段时间获取文件大小并显示它以记录任何更改。由于某种原因,执行下面的代码挂起,只为我提供了一个光标。没有打印或显示任何内容。 代码: #include #include #inclu
printf("It is currently %s's turn.\n", current->name); 我想知道为什么在 %s 之后打印出额外的换行符。我知道C 中的字符串总是以\0 结尾。没有
这个问题已经有答案了: printf anomaly after "fork()" (3 个回答) fork() in c using printf [duplicate] (2 个回答) 已关闭 9
我对编程很陌生。 我正在尝试编写一个程序,从数组中调用水果的价格。但我希望代码在写价格之前也写水果的名称。如果我键入 2,如何使输出为“Orange price : 10”而不仅仅是 price :
这个问题在这里已经有了答案: How do I print a non-null-terminated string using printf? (2 个答案) 关闭 7 年前。 例如,我有一个字符
我有一个 atmel UC3-L0 和罗盘传感器。现在我安装 AtmelStudio 并将一些演示代码下载到电路板中。但是我不知道演示代码中的函数 printf 会在哪里出现数据。我应该如何获取数据?
我有一个 atmel UC3-L0 和罗盘传感器。现在我安装 AtmelStudio 并将一些演示代码下载到电路板中。但是我不知道演示代码中的函数 printf 会在哪里出现数据。我应该如何获取数据?
嗨,我是 C 世界的新手,我的代码确实有些奇怪。目标是创建一个函数,可以在开头和结尾处用空格和/或制表符修剪字符串。我无法使用字符串库。 问题是我的代码中有一个 printf 只是用于测试,它的工作非
我是一名优秀的程序员,十分优秀!