【问题标题】:Breaking down a pointer str to smaller pointers str将指针 str 分解为较小的指针 str
【发布时间】:2013-11-16 16:51:48
【问题描述】:
#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)任何人都可以指出我正确的方向(不只是给我一个有效的循环)?

【问题讨论】:

    标签: c arrays sorting pointers input


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      在您的实施中,您会遇到访问冲突。您正试图触及内存的脏区。这是解决方案,下面有解释

      #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 循环搞砸了。特别是,您混淆了您传递的数据类型。请记住,您已经声明了一个 POINTERS 数组。

       temp=strtok(studentinfo, " ");
      

      这就是说,嘿,让我们标记我的数组指针。你不想要这个。您想标记该数组中的 yth 元素!所以数组中的元素 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 也有两个指针,一个指向目标,一个指向源,所以你的临时变量需要是指向你的字符串的指针。

      如果您对我的解释感到困惑,希望这有助于让我知道。

      【讨论】:

        猜你喜欢
        • 2014-08-15
        • 1970-01-01
        • 2019-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-16
        • 2014-07-22
        相关资源
        最近更新 更多