- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINES 25
int get_lines(char *studentinfo[]);
int main()
{
int onswitch=0;
char *studentinfo[100];
char *fname[100];
char *lname[100];
char *score[100];
int counter;
int x,y;
char temp,temp2,temp3;
counter=get_lines(studentinfo);
for (y=0; y<counter; y++)
{
temp=strtok(studentinfo, " ");
fname[y]=malloc(strlen(temp));
strcpy(fname[y],temp);
temp2=strtok(NULL, " ");
lname[y]=malloc(strlen(temp2));
strcpy(lname[y],temp2);
temp3=strtok(NULL," ");
score[y]=malloc(strlen(temp3));
strcpy(score[y],temp3);
int get_lines(char *studentinfo[])
{
int n=0;
char buffer[80];
puts("Enter one line at a time; enter a blank when done.");
while ((n<MAXLINES) && (gets(buffer) !=0) && (buffer[0] != '\0'))
{
if ((studentinfo[n]=(char*)malloc(strlen(buffer)+1))==NULL)
return -1;
strcpy(studentinfo[n++],buffer);
}
return n;
}
好吧,伙计们,我正在尝试制作一个程序,该程序可以接收学生信息以便稍后进行排序。我已使用底部的功能将输入记录下来。我试图将学生信息分解为三个不同的指针进行排序。我遇到的问题是尝试分配足够的内存来存储信息。然后实际将内存存储在该指针位置。
一个简单的输入是
John Smith 80
^fname ^lname ^score
我认为我的 for 循环理论上可以工作,但事实并非如此(错误:ConsoleApplication3.exe 中 0x0F3CFA50 (msvcr110d.dll) 处未处理的异常:0xC0000005:访问冲突读取位置 0xFFFFFF8)任何人都可以指出我正确的方向(不仅仅是给我一个有效的循环)?
最佳答案
通过您的实现,您会遇到访问冲突。您正试图触及内存的脏区域。这是解决方案,下面有解释
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLINES 25
int get_lines(char *studentinfo[]);
int main()
{
int onswitch=0;
char *studentinfo[100];
char *fname[100];
char *lname[100];
char *score[100];
int counter;
int x,y;
char *temp,*temp2,*temp3;
counter=get_lines(studentinfo);
for (y=0; y<counter; y++)
{
temp=strtok(studentinfo[y], " ");
fname[y]=malloc(strlen(temp));
strcpy(fname[y],temp);
temp2=strtok(NULL, " ");
lname[y]=malloc(strlen(temp2));
strcpy(lname[y],temp2);
temp3=strtok(NULL," ");
score[y]=malloc(strlen(temp3));
strcpy(score[y],temp3);
printf("%s %s %s", fname[y], lname[y], score[y]);
}
}
int get_lines(char *studentinfo[])
{
int n=0;
char buffer[80];
puts("Enter one line at a time; enter a blank when done.");
while ((n<MAXLINES) && (gets(buffer) !=0) && (buffer[0] != '\0'))
{
if ((studentinfo[n]=(char*)malloc(strlen(buffer)+1))==NULL)
return -1;
strcpy(studentinfo[n++],buffer);
}
return n;
}
首先,您的 for 循环和 main 函数缺少结束括号 } 。因此添加这些。
你的 getlines 功能很好。
你的for循环搞砸了。特别是,您混淆了传递的数据类型。请记住,您已经声明了一个指针数组。
temp=strtok(studentinfo, " ");
这就是说,嘿,让我们标记我的数组指针。你不想要这个。您想要标记该数组中的第 y 个元素!因此数组中的元素 0 是指向字符串“JOHN SMITH 80”的指针。这就是我们想要标记化的内容。否则,您将尝试按照 0xabcdabcd 或分配的数组的内存地址来标记某些内容。
temp=strtok(studentinfo[y], " ");
这才是正确的做法。它说标记第一个元素,它是指向我们字符串的指针。
你的下一个问题是你的临时变量。您正在调用 strlen(temp)。 strlen 需要一个指向字符串的指针。您正在传递 char 本身的数据。实际上,您正在传递存储在 char 字节中的 strtok 函数的返回指针(可能为 null)。
char temp,temp2,temp3;
您为 char 类型声明了三个字节。您想要的是三个 char * 来保存指向字符串标记的指针。
char *temp,*temp2,*temp3;
这样,strlen 接收这些指针,为您的 fname 元素分配一些空间,然后您继续使用 strcpy 复制到该元素中。
注意:strcpy 还需要两个指针,一个用于目标,一个用于源,因此您的临时变量再次需要是指向字符串的指针。
希望这有助于让我知道您是否对我的解释感到困惑。
关于c - 将指针 str 分解为更小的指针 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19795353/
我正在尝试在 R 中计算任意 N x J 矩阵 S 的投影矩阵 P: P = S (S'S) ^ -1 S' 我一直在尝试使用以下函数来执行此操作: P 概述 solve 基于一般方阵的 LU 分解
所以我有一个包含数千行的非常旧的文件(我猜是手工生成的),我正试图将它们移动到一个 rdb 中,但是这些行没有转换为列的格式/模式。例如,文件中的行如下所示: blah blahsdfas
这实际上只是一个“最佳实践”问题...... 我发现在开发应用程序时,我经常会得到很多 View 。 将这些 View 分解为几个 View 文件是常见的做法吗?换句话说......而不只是有view
使用以下函数foo()作为简单示例,如果可能的话,我想将...中给出的值分配给两个不同的函数。 foo args(mapply) function (FUN, ..., MoreArgs = NUL
正面案例:可以进入列表 groovy> println GroovySystem.version groovy> final data1 = [[99,2] , [100,4]] groovy> d
省略素数计算方法和因式分解方法的详细信息。 为什么要进行因式分解? 它的应用是什么? 最佳答案 哇,这个线程里有这么多争斗。 具有讽刺意味的是,这个问题有一个主要的有效答案。 因式分解实际上在加密/解
术语“分解不良”和“重构”程序是什么意思?你能举一个简单的例子来理解基本的区别吗? 最佳答案 重构是一种通用技术,可以指代许多任务。它通常意味着清理代码、去除冗余、提高代码质量和可读性。 分解不良代码
我以前有,here ,表明 C++ 函数不容易在汇编中表示。现在我有兴趣以一种或另一种方式阅读它们,因为 Callgrind 是 Valgrind 的一部分,在组装时显示它们已损坏。 所以我想要么破坏
最初,我一直在打开并同时阅读两个文件,内容如下: with open(file1, 'r') as R1: with open(file2, 'r') as R2: ### m
我正在尝试摆脱 标签和标签内的内容使用 beatifulsoup。我去看了文档,似乎是一个非常简单的调用函数。有关该功能的更多信息是 here .这是我到目前为止解析的 html 页面的内容...
给定一个 float ,我想将它分成几个部分的总和,每个部分都有给定的位数。例如,给定 3.1415926535 并要求将其分成以 10 为基数的部分,每部分 4 位数字,它将返回 3.141 + 5
我的 JSF 项目被部署为一个 EAR 文件。它还包括一些 war 文件。我需要 EAR 的分解版本(包括分解的内部 WAR)。 有什么工具可以做到吗? 最佳答案 以编程方式还是手动? EAR 和 W
以下函数不使用行透视进行 LU 分解。 R 中是否有一个现有的函数可以使用行数据进行 LU 分解? > require(Matrix) > expand(lu(matrix(rnorm(16),4,4
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 提供事实和引用来回答它. 7年前关闭。 Improve this
我正在使用登记数据进行病假研究。从登记册上,我只得到了每个人的病假开始日期和结束日期。但日期并没有逐年分割。例如,对于人 A,只有开始日期 (1-may-2016) 和结束日期 (14-feb-201
我发现以下 R 代码使用 qr 因式分解无法恢复原始矩阵。我不明白为什么。 a <- matrix(runif(180),ncol=6) a[,c(2,4)] <- 0 b <- qr(a) d <-
我正在尝试检测气候数据时间序列中的异常值,其中一些缺失的观测值。在网上搜索我发现了许多可用的方法。其中,STL 分解似乎很有吸引力,因为它去除了趋势和季节性成分并研究了其余部分。阅读 STL: A S
我想使用 javascript 分解数组中的 VIN,可能使用正则表达式,然后使用某种循环... 以下是读取 VIN 的方法: http://forum.cardekho.com/topic/600-
我正在研究 Databricks 示例。数据框的架构如下所示: > parquetDF.printSchema root |-- department: struct (nullable = true
我正在尝试简化我的代码并将其分解为多个文件。例如,我设法做到了: socket.once("disconnect", disconnectSocket); 然后有一个名为 disconnectSock
我是一名优秀的程序员,十分优秀!