将指针str分解为较小的指针str

时间:2013-11-05 17:43:54

标签: c arrays sorting pointers input

#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)任何人都可以指向正确的方向(不只是给我一个有效的循环)?

2 个答案:

答案 0 :(得分:1)

通过实施,您将获得访问冲突。你正试图触摸一个肮脏的记忆区域。这是解决方案,下面有解释

#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循环和主要功能缺少一个结束括号}。所以加上那些。

你的getlines功能都很好。

你的for循环搞砸了。特别是,您混淆了传递的数据类型。请记住,您已经声明了一个POINTERS数组。

 temp=strtok(studentinfo, " ");

这就是说,嘿,让我们对我的数组指针进行标记。你不想要这个。您想要标记该数组中的y元素!因此,数组中的元素0是指向字符串&#34; JOHN SMITH 80&#34;的指针。这就是我们想要标记的内容。否则你所拥有的是试图沿着0xabcdabcd的行或任何分配的数组的内存地址标记的东西。

temp=strtok(studentinfo[y], " ");

这是正确的方法。它表示标记第一个元素,它是指向我们字符串的指针。

你的下一个问题是你的临时变量。你正在调用strlen(temp)。 strlen期望指向字符串的指针。您正在传递char本身的数据。实际上,您正在传递存储在char字节中的strtok函数的返回指针(可能为null)。

char temp,temp2,temp3;

您为char类型声明了三个字节。你想要的是三个char *来保存指向你的字符串标记的指针。

  char *temp,*temp2,*temp3;

有了这个,strlen接受这些指针,为你的fname元素mallocs一些空间,然后你继续使用strcpy复制到这个元素。

注意:strcpy也有两个指针,一个用于目标,一个用于源,所以你的临时变量也需要指向你的字符串。

希望这有助于让我知道你是否对我的解释感到困惑。

答案 1 :(得分:0)

strcpy接受字符,直到达到\ 0字符。您想要检查strncpy或memcpy函数,然后手动添加null终止符。

相关问题