C语言中函数调用的问题?

Python013

C语言中函数调用的问题?,第1张

关于c语言中函数调用问题解答如下:

首先如果是编译的这个代码那这个函数肯定被调用了,让你觉得没有调用是因为while循环没有被执行。

图中红色框内代码p1和p2经过赋值p1是肯定大于p2所以不满足while.的条件直接退出了函数。

你需要看看要实现啥在改一下。

我给你改正了一份完整的代码

#include <stdio.h>

#include <stdlib.h>

#include <conio.h>

#define MAXSIZE 100

typedef char datatype

typedef struct { 

datatype data[MAXSIZE]

int last

} sequenlist

//函数声明写到这里 

int INSERT(sequenlist *L,datatype x,int i)

int DELETE(sequenlist *L,int i)

void print (sequenlist *L, int n) // 方便观察,这里额外加个打印,题主按需保留或删除 

//void main ()

int main (void) { //main函数最好这样写 

//int INSERT(sequenlist *L,datatype x,int i)

//int DELETE(sequenlist *L,int i)

//sequenlist *L //定义sequenlist型指针,但未指向有效对象 

sequenlist L //定义一个sequenlist型变量

int m,n,i

datatype x //定义一个datatype型变量

printf ("请输入元素个数")

//scanf ("%c", &n)

scanf ("%d", &n) //n是int型%d

for (m=0 m<=n-1 m++) {

printf ("请输入%d个元素", m+1)

//scanf ("%c", &(*L).data[m])

fflush (stdin) //清空输入缓冲区,否则【scanf ("%d", &n)】执行时输入的回车将会写入【L.data[m]】

scanf ("%c", &L.data[m])

}

//(*L).last = n-1

L.last = n-1

print (&L, L.last) //方便观察,打印一下

//插入、删除元素,写的太乱了。。。以下重写

printf ("输入要插入的元素")

fflush (stdin) //清空输入缓冲区

scanf ("%c", &x) // 如果要输入char型,最好先清空输入缓冲区

printf ("输入插入位置")

scanf ("%d", &i)

INSERT (&L, x, i) //插入

print (&L, L.last) //方便观察,打印一下

printf ("输入要删除第几个元素")

scanf ("%d", &i)

DELETE(&L , i) //删除

print (&L, L.last) //方便观察,打印一下

getch () //屏幕暂留 

return 0

}

int INSERT (sequenlist *L, datatype x, int i) {

int j

if (((*L).last) >= MAXSIZE-1)

return 0

if ((i<1)||(i>=((*L).last)+2))

return 0

for (j=(*L).last j>=i-1 j--)

//(*L).data[j-1] = (*L).data[j] //在i处插入新元素,i之后的元素均向后移动一位

(*L).data[j+1] = (*L).data[j]

(*L).data[i-1] = x

(*L).last++

return (1)

}

int DELETE (sequenlist *L, int i) {

int j

if ((i<1)||(i>(*L).last+1))

return 0

for (j=i-1 j<=(*L).last j++)

//(*L).data[j-1]=(*L).data[j] //删除i处的元素,i之后的元素均向前移动一位

(*L).data[j]=(*L).data[j+1]

(*L).last--

return(1)

}

// 方便观察,这里额外加个打印,题主按需保留或删除 

void print (sequenlist *L, int n) {

int i

for (i=0 i<=n i++)

printf ("%c ", (*L).data[i])

putchar ('\n')

}

运行结果

==========以下是原回答==========

main函数里调用INSERT、DELETE这两个函数时的参数错了。

int INSERT(sequenlist *L,datatype x,int i) //第一个参数是sequenlist指针类型

INSERT(*L,n,z) //main函数里,你是如是调用的,变量L是sequenlist指针类型,*L代表取指针指向的数据,所以参数类型不符

INSERT(L,n,z) //改成这样就好 

int DELETE(sequenlist *L,int i) //同理

DELETE(*L,z) //main函数中的调用

DELETE(L,z) //改成这样

先举个例子:

#include <stdio.h>

int max(int,int)//这是函数max的申明,最后要加分号

int main()

{

int a=1,b=2,c

c = max(a,b)//这是函数max的使用

printf("%d",c)

return 0

}

int max(int n,int m)//这是函数max的定义部分,最后不加分号

{

if (n>m)

return n

else

return m

}

不能在函数体内定义函数是什么意思?

【就是说函数在程序中不能嵌套定义,这个和Pascal语言不同,例如上面程序中,max函数不能定义在main函数中间】

定义是声明还是使用?

【定义既不是申明,也不是使用,函数具体功能的实现代码叫做函数的定义,如上面程序最后7行就是定义】

函数不是应该先声明在使用吗?

【是的,但如果函数定义在函数使用之前,就可以不用申明(如下面程序)。函数定义在函数使用后的情况下(如上面程序),先声明,能使编译器在编译在编译过程中能够识别使用的函数】

#include <stdio.h>

int max(int n,int m)//这是函数max的定义部分,最后不加分号

{

if (n>m)

return n

else

return m

}

int main()

{

int a=1,b=2,c

c = max(a,b)//这是函数max的使用

printf("%d",c)

return 0

}

定义如何解释?

【见第二个问题】