0
#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;
}

さて、私は後でソートするために学生情報を取り込むプログラムを作成しようとしています。一番下の関数で入力を取り下げました。私は、学生情報を分類するための 3 つの異なるポインターに分解しようとしています。私が抱えている問題は、情報を保存するのに十分なメモリを割り当てようとしていることです。次に、実際にそのポインターの場所にメモリを格納します。

簡単な入力は

John Smith 80
^fname ^lname ^score

私が持っていた for ループは理論的には機能すると思っていましたが、そうではありませんでした (エラー: ConsoleApplication3.exe の 0x0F3CFA50 (msvcr110d.dll) で未処理の例外: 0xC0000005: アクセス違反の読み取り場所 0xFFFFFF8)。動作するループを教えてください)?

4

2 に答える 2