创客学院9天C语言六

①数组在函数间传参:
全局数组传递方式:
复制传递方式:
实参为数组的指针,形参为数组名(本质是一个指针变量)
地址传递方式:
实参为数组的指针,形参是同类型的指针

▲编写一个函数,计算一个一维整形数组的所有元素的和。
传元素和个数;

int array_sum (int data[],int n);//int *data代替int data[]
int main (int argc,char *argv[])
{
   int a[] = {5,3,9,21,4};
    int sum = 0;
	sum = array_sum(a,sizeof(data)/sizeof(int));
	printf ("sum=%d\n",sum);
	return 0;
}
int array_sum (int data[],int n)//int data[] = a;error int *data =a;
{
   int ret = 0;
   int i;
   //n = sizeof(data)/sizeof(int);
   for (i = 0;i<n;i++)
   {
     printf("%d\n",data[i]);
	 ret += data[i];
   }
   return ret;
}

②编写函数,删除字符串中的空格。

#include <stdio.h>
void del_space(char * s1);
int main (int argc,char *argv[])
{
   char s[] = "   h    a   sdf  g ";
   puts(s);
   del_space(s);
   puts(s);
   return 0;
}
void del_space(char * s1)
{
  char * s2;
  s2 = s1;
  while (*s1)
  {
    if (*s1 == ' ')
	{
	  s1++;
	}
    else 
	{
	  *s2 = *s1;
	  s1++;
	  s2++;
	}
  }
 *s2 = '\0';
}

③指针函数:一个函数的返回值为地址量的函数
<数据类型> * <函数名称> (<参数说明>)
{语句序列;}
返回值:全局变量的地址、static变量的地址、字符串常量的地址

▲错误:

#include <stdio.h>
#include <string.h>
char * getstring();
int main (int argc,char *[])
{
  printf (---%s---\n",getstring());
  return 0;
}
char * getstring ()
{
  char str[20];
  strcpy(str,"hello");
  return str;

}

▲改正:

#include <stdio.h>
#include <string.h>
int main (int argc,char * argv[])
{
  char * r;
  r = getstring();
  printf("---%s---\n",getstring());
  (*r)++;
  puts(r);
  return 0;
  
}

char * getstring()
{
   static char str[20];
   strcpy(str,"hello");
   return str;
}

▲改正2:

int main (int argc,char *argv[])
{
  char * r = "hello";
  r = getstring();
  printf("---%s---\n",getstring());
  puts(r);
  return 0;
}
char * getstring()
{
  char * str = "hello";
}

④编写一个指针函数,删除字符串中的空格

#include <stdio.h>
#include <string.h>
char * del_space(char * s);
int main (int argc,char *argv[])
{
    char * r;
	char str[] = "  how  are  you  ";
	 r = del_space(str);
	 printf("---%s---\n",r);
	 puts(str);
	 return 0;
	
}
char * del_space(char * s)
{
   char * r = s;
   char * p = s;
   while (*s)
   {
     if(*s == ' ')
	 s++;
	 else
	 {
	   *p = *s;
	   s++;
	   p++;
	 }
   }
   *p = '\0';
    return r;
}

⑤字符串拷贝的功能

#include <stdio.h>
#include <string.h>
char * del_space (char * s);
int main (int argc,char *argv[])
{
   char str[]= " how  are  you";
   char s[50],s2[50];
   //strcpy(s,del_space(str));
   strcpy(s2,strcpy(s,del_space(str)));
   puts(str);
   puts(s);
   puts(s2);
   return 0;

}
char * del_space (char * s)
{
   char * r=s;
   char * p=s;
   while (*s)
   {
     if(*s == ' ')
	 s++;
	 else
	 {
	   *p = *s;
	   s++;
	   p++;
	 }
   }
   *p = '\0';
    return r;
}

}

⑥编写一个指针函数,连接字符串

#include <stdio.h>
//#include <string.h>
int main (int argc,char *[])
{
   char dest[50] = "welcome";
   char src[] = "makeru";
   
   puts(mastrcat(dest,src));
   puts(dest);
   return 0;
}
char *mstrcat(char *dest ,const char * src)
{
  char * r = dest;
  while (*dest)
   {
     dest++;
   } 
   while (*src)
   {
       *dest = *src;
	   dest ++;
	   src ++;
   }
   *dest = '\0';
   return r;
}

简洁版:

char *mstrcat(char * dest, const char *src)
{
    char * r = dest;
	while (*dest++);
	dest --;
	while (*dest ++ = *src++);

	return r;
}

⑦指针函数输入数字转为字符

#include <stdio.h>
char *itoa(int n);
int main (int argc,char *argv[])
{
   int n;
   char * s;
   printf("input:");
   scanf ("%d",&n);
   s = itoa(n);
   puts(s);
   return 0;
}
char * itoa (int n)
{
 int r,i=0;
 static char p[50];
 while (n)
 {
    r = n % 10;
	n /= 10;
	p[i] = r + '0';
	i++;
 }
 p[i] = '\0';
 j = i-1;
 i = 0;
 
 while (i<j)
 {
   r = p[i];
   p[i] = p[j];
   p[j] = r;
   i++;
   j--;
 }
 return p;
}

#include <stdio.h>
char *itoa(char * p,int n);
int main (int argc,char *argv[])
{
   int n;
   char s[50],* r;
   printf("input:");
   scanf ("%d",&n);
   r = itoa(n);
   puts(r);
   puts(s);
   return 0;
}
char * itoa (char *p,int n)
{
 int r,i=0,j;
 //static char p[50];
 while (n)
 {
    r = n % 10;
	n /= 10;
	p[i] = r + '0';
	i++;
 }
 p[i] = '\0';
 j = i-1;
 i = 0;
 
 while (i<j)
 {
   r = p[i];
   p[i] = p[j];
   p[j] = r;
   i++;
   j--;
 }
 return p;
}

⑨递归函数
递归函数是指一个函数的函数体中直接或间接调用了该函数自身
递归函数调用的执行过程分为两个阶段:
递推阶段:从原问题出发,按递归公式递推从未知到已知,最终
达到递归终止条件
回归阶段:按递归终止条件求出结果,逆向逐步代入递归公式,回归到
原问题求解;

#include <stdio.h>
int fac(int);
int main (int argc,char *argv[])
{
  int n;
  printf("input:");
  scanf("%d",&n);
  printf("%d\n",fac(n));
  return 0;
}
int fac(int n)
{
   if (n == 0 || n ==1)
      return 1;
	  return n * fac(n-1);
}

菲波那切数列

#include <stdio.h>
int fib(int);
int main ()
{
   int n = 1;
   while (n <=10)
   {
      printf("%d ",fib(n));
	  n++;
   }
   printf ("\n");
   return 0;

}
int fib (int n)
{
   if (n ==1 || n ==2)
      return 1;
   return fib(n-1)+fib(n-2);
}

函数指针:
函数指针用来存放函数的地址,这个地址是一个函数的入口地址
函数名代表了函数的入口地址;

#include <stdio.h>
int add (int a,int b)
{
   return a + b;
} 
int main(int argc,char *argv[])
{
  int m = 10,n = 20;
  p = add;
  printf ("%d\n",(*p)(m,n));
  return 0;
}

返回一个比较长的字符串:

#include <stdio.h>
#include <string.h>
char *strlong(char *str1, char *str2){
    if(strlen(str1) >= strlen(str2)){
        return str1;
    }else{
        return str2;
    }
}
int main(){
    char str1[30], str2[30], *str;
    gets(str1);
    gets(str2);
    str = strlong(str1, str2);
    printf("Longer string: %s\n", str);
    return 0;
}

用指针作为函数返回值时需要注意的一点是,函数运行结束后会销毁在它内部定义的所有局部数据,包括局部变量、局部数组和形式参数,函数返回的指针请尽量不要指向这些数据,C语言没有任何机制来保证这些数据会一直有效,它们在后续使用过程中可能会引发运行时错误。

猜你喜欢

转载自blog.csdn.net/weixin_42068537/article/details/83538060
今日推荐