c语言函数问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言函数问题相关的知识,希望对你有一定的参考价值。

int &get(); 和int get();有什么区别哦?&取地址。大概有什么作用哦?

  一个函数的的声明,要给出 四个信息,举个例子

  void printfMessage (void)

  

      printf("Programming is fun.\\n");

  

    谁可以调用它  who call call it    (注意 static  的应用)

    返回值的类型    The type of value it returns

    函数名 its name

    参数列表  the arguments it takes

  很显然  ,你说的例子中,get作为函数名

  不同的是它们的返回类型,一个返回Int &, 一个返回int.前者是一个指向int 类的指针,后者是整型

 

  下面截至  k & r

  here is the function power and a main program to excuter it , so you can see the whole structure at once.

A function definition has this form:   函数定义具有如下格式

return-type function-name(parameter declarations, if any)

    declarations

    statements

 

返回类型 函数名(参数声明,...)

    声明

    语句

 

Returning Pointers

Although functions tht return pointers are handled just like any other type of function, it is review some key concepts and look at an example. Pointers are neither integers nor unsigned integers, They are the memory address of a certain type of data. One reason for this distinction is that pointer arithmetic is relative to the base type. For example, if an integer

pointer is incremented , it will contain a value that is four greater than its previous value

(assuming 4-byte integers), In general, each time a pointer is incremented(or decremented),

it points to the next(of previous) item of its type. Since the length of different data types may differ, the compiler must know what type of data the pointer is pointing to. For this reason that returns a pointer must declare explicitly what type of pointer it is returning. For example, you should not use a return type of int * to return a char * pointer! In a few case, a function will need to return a generic pointer. In this case, the function return type must be specified as void *.

 

To return a ponter, a function must be declared as having a pointer return type. For example, the following function returns a pointer to the first occurrence of the character c in string s; If no match is found, a pointer to the null terminator is returned.

/* Rerurn pointer of fisrt occurrence of c in s. */

char *match(char c, char *s)

    while (c != *s, && *s) s++;

     return (s);

 

Here is a short program that uses match();

#include <stdio.h>

char *match(char c, char *s);  /* prototype */

 

int main(void)

    char s[80], *p, ch;

 

     gets(s);

    ch = getchar();

    p = match(ch, s);

    if (*p) /* there is a match */

        printf(" %s ", p);

    else

        printf("No match found.");

    return 0;

 

 

 

简单的翻译一下:

         虽然函数返回一个指针和函数返回其他类型的值并没有什么区别,但是通过它回顾一些关键概念还是很有用处的。指针无非就是整型和无符号整型,它们是某些数据类型的内存地址,指针的运算涉及到它的基本类型,比如说,一个指向整型的指针增加1.这个指针变量的的值将会比他的前一个值大4, 一般来讲,指针每加1或者减1,它将指向下一项(这种类型)。由于不同数据类型有着不同的长度,编译器必须知道指针指向了哪种类型,为了解决这个问题,如果一个函数返回一个指针,它必须明确的给出它将返回哪种类型的指针。 比如说,你不能使用 int * 来返回char *. 在某些情况下,一个函数需要返回一个泛型(generic pointer), 在这种情况下,函数必须声明为 void *.

      为了能够返回一个指针,函数必须明确的指出,它将返回哪种指针类型。举个例子,下列函数返回一个指针,指向字符串S中第一次出现的c,如果不能匹配上,返回一个指向NULL的指针

 

 

reference:

[1] Brian W. Kernighan& Dennis M. Ritchie the c programming language

[2] Stephen G.Kochan Pogramming in C Third Edition

[3] Herbert Schildt  C The Complete Reference Fourth Edition

参考技术A int &get()中的&不是取地址,你学了c++才知道,那个是引用运算符,有点像指针,举个例子
int a = 1;
int & ra = a;//定义了一个引用,这里 ra 是 a的别名,其实 ra 就是 a ,对ra操作就是对a进行操作
int get()函数就不用我说了吧,是一个普通的函数
希望对你有帮助追问

引用变量的我懂..不懂int &get()和int get()的区别.比如在两个函数里最后都把返回的值赋值给一个变量,我觉得两者都可以实现。这个&有什么其他作用吗?
例如: int &get()..... return x; int get().... return x; 在主函数中int num = get() 。

参考技术B …… 求解释 int &get()
是定义函数吗追问

是啊、不懂int &get()和int get()的区别.比如在两个函数里最后都把返回的值赋值给一个变量,我觉得两者都可以实现。这个&有什么其他作用吗?
例如: int &get()..... return x; int get().... return x; 在主函数中int num = get() 。

关于c语言中数组作为函数参数的函数之间调用问题

比如,在main函数中定义了一个int array[3]; 不赋初值
再写两个子函数,假如在第一个子函数中给数组赋值,在第二个子函数中用这个数组中的值做计算,就假设是在这里定义另一个数组 做 s[i]=array[i] *i;的操作好了。
……fun1 (……)

……

……fun2 (……)

for(i=0,i<n;i++)
s[i]=array[i] *i;
……

main()

……//fun1调用
……//fun2调用


我假设的问题有没有意义不重要,也不是要其他什么简便方法,我就是想知道这样关于数组在函数之间的调用是怎么处理的,返回值是怎么样的。可以的话给我一个完整的参考代码,谢谢~~~

1、新建一个数组作为参数项目,如图所示:

2、添加一个array.c文件,如图所示:

3、包含stdio.h和stdlib.h头文件,如图所示:

4、输入main函数主体及返回值,如图所示:

5、定义一个数组arr,如图所示:

6、定义一个function函数,如图所示:

7、将数组作为参数传递给function函数,如图所示:

8、运行程序,输出结果,如图所示:

参考技术A 这问题说明你对指针的运用还是不熟练的,既然是数组,作为函数参数传递的是数组首地址.
#include <stdio.h>
#include <stdlib.h>
void func1(int *array, int size)
int i,temp;
for (i=0;i<size;i++)
scanf("%d",&temp); //利用scanf输入赋值
array[i]=temp;


//数组array_b根据数组array_a的元素乘以项数获得元素值
void func2(int *array_a,int *array_b, int size)
int i;
for (i=0;i<size;i++)
array_b[i]=array_a[i] * i;

//输出数组元素
void print(int *array, int size)
int i;
for (i=0;i<size;i++)
printf("%d ",array[i]);
printf("\n");


int main(void)
int size; //数组大小
int i;
int *array_a, *array_b; //声明数组array_a,array_b
scanf("%d", &size);
array_a=malloc(sizeof(int) * size);
array_b=malloc(sizeof(int) * size);
func1(array_a, size);
func2(array_a, array_b, size);
print(array_a,size);
print(array_b,size);
return 0;
本回答被提问者采纳
参考技术B 这问题说明你对指针的运用还是不熟练的,既然是数组,作为函数参数传递的是数组首地址.
#include
<stdio.h>
#include
<stdlib.h>
void
func1(int
*array,
int
size)

int
i,temp;
for
(i=0;i<size;i++)

scanf("%d",&temp);
//利用scanf输入赋值
array[i]=temp;


//数组array_b根据数组array_a的元素乘以项数获得元素值
void
func2(int
*array_a,int
*array_b,
int
size)

int
i;
for
(i=0;i<size;i++)
array_b[i]=array_a[i]
*
i;

//输出数组元素
void
print(int
*array,
int
size)

int
i;
for
(i=0;i<size;i++)
printf("%d
",array[i]);
printf("\n");

int
main(void)

int
size;
//数组大小
int
i;
int
*array_a,
*array_b;
//声明数组array_a,array_b
scanf("%d",
&size);
array_a=malloc(sizeof(int)
*
size);
array_b=malloc(sizeof(int)
*
size);
func1(array_a,
size);
func2(array_a,
array_b,
size);
print(array_a,size);
print(array_b,size);
return
0;
参考技术C 函数在数组中的调用是以数组名为实参调用的,因为数组名就是数组的首地址,所以说对数组的调用是不需要返回值的,主调函数里面的数组值会随被调函数的形参数组改变而改变,当然这些都是在你以数组名作为实参的前提下 参考技术D 如果传递值是指针(地址),也就是数组名,虽然在子函数里面,但复制过去的是地址,所以可以对地址里面指向的内容进行操作,也就是一个子函数中可以实现对多个值的操作,如果传递的是内容,则至多通过return 进行一个返回值操作,总之,要看你传什么

以上是关于c语言函数问题的主要内容,如果未能解决你的问题,请参考以下文章

c语言静态函数调用问题

C语言绘图函数问题(超简单)

C语言中不能在函数里面声明函数吗

C语言中 多个源文件之间函数如何调用问题

c语言的基本组成单位是啥啊

简单的C语言问题?