开发者社区> 问答> 正文

C语言--结构函数,函数返回指针值赋值给同类型指针,结果说表达式有问题,就一个最?400报错

C语言--结构函数,函数返回指针值赋值给同类型指针,结果说表达式有问题,就一个最简单的调用函数的赋值语句? 400 报错 相当奇怪:
我定义了一个结构    struct matrix{..};
定义了一个结构指针    struct matrix *input;
定义了一个结构函数,返回值为结构指针    struct matrix *New(int ,int)    {...}
之后,将结构函数的返回值赋值给结构指针    input=struct matrix *New(row,rank);    //row,rank为int型变量

结果编译器报错,说:    error: expected expression before 'struct'
这个报错指向的就是结构指针赋值的那句,说表达式有误,搞不懂哪里有错,
改用指向指针的指针一样报错,真不知道哪里出错了,求助啊

具体的部分代码如下:

大家可以直接复制到IDE上或其他什么的

#include <stdio.h>
#include <stdlib.h>
#include "main.h"

#define SIZE_STR_MAT sizeof ( struct matrix )
#define SIZE_MAT(SMatrix) ((SMatrix).row)*((SMatrix).rank)*(SIZE_STR_MAT)

struct matrix *input();
struct matrix *NewCreate( int ,int );
void output( struct matrix * );

int main()
{
	struct matrix
	{
		int row,rank;
		int *matrix;
	};
	struct matrix *input();	//这里测试用的
	return 0;
}

/***********************************************
 * 矩阵值的建立(调用函数)与输入
 ***********************************************/
struct matrix *input()
{
	int row,rank,temp,size;
	struct matrix *input;

	printf("请输入当期要输入的矩阵的行数与列数,格式为“行 列”:\n");
	scanf("%d %d",&row,&rank);

/***********************************************
 * 这里是出问题的地方,提示error: expected expression before 'struct'
 ***********************************************/
	input = struct matrix *NewCreate( row ,rank ) ;	//调用函数,将"input"指向新建的结构
	//void *NewCreate( row ,rank ,&input );	//使用指向指针的指针时仍然报一样的错误
/**********************************************************************************/
	size = row * rank;
	for ( temp = 0 ; temp < size ; temp++ )		//矩阵值输入
	{
		if ( temp % rank )
			printf("\n请输入第%3d行矩阵值,各值之间以空格做分隔符:\n",( int )(temp / rank + 1));
		scanf("%d",(*input).matrix++);
	}

	return input;
}

/***********************************************
 * 分配新建矩阵结构的存储空间
 ***********************************************/
struct matrix *NewCreate( int row,int rank )
//void *NewCreate( int row,int rank ,struct matrix **temp )    //使用指向指针的指针
{
	struct matrix *New = NULL;		//初始值设为NULL,以便于malloc失败检测

	for ( ; New == NULL ; )			//for语句防止"空指针"产生,下同,分开是为了不产生垃圾
	{
		New = malloc( SIZE_STR_MAT );	//创建矩阵结构空间,大小为"struct matrix"
		New->row = row;
		New->rank = rank;
	}

	free( New->matrix );
	New->matrix = NULL;			//初始值设为NULL,以便于malloc失败检测
	for ( ; New->matrix == NULL ; )
		New->matrix = malloc( SIZE_MAT( *New ) );	//创建矩阵值空间,大小为"行*列"
       //*temp=New;	//使用指向指针的指针
	return New;
}



求助啊,卡壳了啊,大家帮忙帮忙

展开
收起
爱吃鱼的程序员 2020-06-03 17:08:38 612 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    input = struct matrix *NewCreate( row ,rank ) ; 改为

    input = sNewCreate( row ,rank ) ; 

    ######总算知道是哪里出问题了,多谢啊######是不是改成 input = NewCreate( row ,rank ) ; 啊?
    2020-06-03 20:34:44
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载