| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 533 人关注过本帖
标题:初步涉练c,有请高人指点
只看楼主 加入收藏
hardyvera
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-7-1
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
初步涉练c,有请高人指点
(marks1 = (int**) malloc(2*sizeof(int*)
(marks1[0] = (int*) malloc(5*sizeof(int)题目中截取的两句话,不是很了解,求高人指点是啥意思啊
搜索更多相关主题的帖子: 高人 有请 
2010-07-01 10:46
pbreak
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:83
专家分:558
注 册:2007-5-10
收藏
得分:10 
能把整个题目贴出来不?
2010-07-01 10:57
hardyvera
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-7-1
收藏
得分:0 
回复 2楼 pbreak
/* function InsertStudent
------------------------------------ */
/*This function prompts the user to enter a new student name.
The precise form of the prompt is
  Enter Student Name to be added to Class List:
Then this function reads the name from standard input.
But it must be able to read any name of any size!! Thus you have
to allocate memory (using malloc()) to read the input into, and if
it is not big enough, must use realloc() to extend it. You are not
allowed to allocate more than 10 bytes in one allocation or
extend your segment by more than 10 bytes in one call to realloc().
Do not forget to deallocate the memory before exiting the function!

Once the function has the name to be entered, it traverses the array student
to find out if the name is already there. If so, the function displays
a warning message "student xxx already in the class list" and terminates (
where xxx is the name of the student). If the name is not in the array, the
array students is extended by one item (using realloc()) and a copy
of the string with the name is "placed" there. Also the array marks is
extended by one row (using realloc()) and all five marks in that row are set to
 -1. Then updated arrays students and marks are passed to SortClass()
so they can be sorted alphabetically, as you are required to keep the class
list and the list of marks in alphabetical order.

Note that both students and marks are passed to this function "by reference",
for this function on occasions must be able to modify the value stored in the
pointer student (in main()) - when putting there the very first student or when
realloc() possibly "moves" the array in the memory somewhere else. The same applies
to marks.*/
void InsertStudent(char*** students,int*** marks)
{
char* buf;
int found, i, j, size;
char** students1;
int** marks1;
students1 = *students;
marks1 = *marks;

char *name;
printf("Enter Student Name to be added to Class List:\n");
fflush(stdout);
name = ReadLine();

if (students1 == NULL)
{
  if ((students1 = (char**) malloc(2*sizeof(char*))) == NULL)merror(2);   
  if ((students1[0] = (char*) malloc(strlen(name)+1)) == NULL)merror(3);
  strcpy(students1[0],name);

  students1[1] = NULL;
  if ((marks1 = (int**) malloc(2*sizeof(int*))) == NULL)merror(4);  
  if ((marks1[0] = (int*) malloc(5*sizeof(int))) == NULL)merror(5);   
  marks1[0][0]=marks1[0][1]=marks1[0][2]=marks1[0][3]=marks1[0][4]=-1;
  marks1[1] = NULL;

  *students = students1;
  *marks = marks1;
  return;
}

/* do we have the student yet ? */
for(found = i = 0; students1[i] != NULL; i++)
{
 if (strcmp(students1[i],name) == 0)
 {
   found = 1;
   break;
 }
}

if (found)
{
  printf("student %s already in the class list\n",name);
  return;
}
 

/* so it is not in the class list yet */

students1 = (char**) realloc((void*)students1,(i+2)*sizeof(char*));
if (students1 == NULL)
  merror(6);
if ((students1[i] = (char*) malloc(strlen(name)+1)) == NULL)
  merror(7);
strcpy(students1[i],name);

students1[i+1] = NULL;

free((void*) name);


if ((marks1 = (int**) realloc((void*)marks1,sizeof(int*)*(i+2))) == NULL)
  merror(8);
if ((marks1[i] = (int*) malloc(5*sizeof(int))) == NULL)
  merror(9);
marks1[i][0]=marks1[i][1]=marks1[i][2]=marks1[i][3]=marks1[i][4]=-1;
marks1[i+1] = NULL;

SortClass(students1,marks1);

*students = students1;
*marks = marks1;
}
2010-07-01 11:02
风兮飞扬
该用户已被删除
收藏
得分:10 
提示: 作者被禁止或删除 内容自动屏蔽
2010-07-01 11:05
pbreak
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:83
专家分:558
注 册:2007-5-10
收藏
得分:0 
mark1本质上为一动态数组
mark1[0]开辟了5个int长度的空间
但mark1[1]没有开辟内存空间
2010-07-01 11:24
hardyvera
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-7-1
收藏
得分:0 
回复 5楼 pbreak
请问void InsertStudent(char*** students,int*** marks)为什么会用上了三重指针
2010-07-01 14:12
快速回复:初步涉练c,有请高人指点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017514 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved