萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 846|回复: 3

[计算机] c语言扫盲

[复制链接]
发表于 2009-1-1 14:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x
c语言中的for循环
基本结构
for (initial;condition;increment)
        statement
常用形式
1 /* 递减 1*/
  for ( count = 100;count > 0;count --)

2 /* 递增 5*/
  for ( count = 0; count < 1000;count +=5)

3 /* 在循环体外初始化 */
   count =1;
  for (printf("Now the array is sorted");count < 1000;count ++)
        /* here is the sort action */

4 /* 在循环体内递增 */
  for (count=0;count <100;)  
        printf("%d",count++);
5 /* 在条件中使用逻辑运算符 */
  for ( count =0;count <1000 && array [count]!=0;count ++)
        printf("%d,array[count]");
  /*或者简化为:*/  
  for ( count =0;count <1000 && array [count];count ++)
        printf("%d,array[count]");
6 /*初始化数组的每个元素为50*/
  for (count =0;count <1000;array [count++]=50)
        ;
  /*或者清楚一点,这样写*/
  for (count =0;count <1000;array [count++]=50)
  {
  ;
  }
7 /*多个条件*/
  for (i =0,j=999;i<1000;i++,j--)
        b[j]=a;

8 for循环与while循环的互换
        while (condition)
                statements
        =
        for (;condition;)
                statements

[ 本帖最后由 cn.de 于 2009-1-1 17:31 编辑 ]
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
 楼主| 发表于 2009-1-1 17:32 | 显示全部楼层

malloc()函数

malloc()函数
malloc()是内存分配函数,使用时需要将字节数传递给它.malloc()找到并保留一个所需大小的内存块,并返回第一个字节的地址.但是函数的返回类型为void指针.void指针能与所有数据类型兼容.如果mallloc()无法分配所需数量的内存,则返回NULL.
语法如下:
#include <stdlib.h>
void *malloc(size_t size);
范丽如下:
1.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *str;
str=(char*) malloc(100);
if(str==NULL)
{
   printf("Not enough memory to allocat!\n");
   exit(1);
  }
  printf("String was allocated\n");
  return 0;
}
2.
int *numbers;
numbers=(int *)malloc(50*sizeof(int));
3.
float *numbers;
numbers=(float *)malloc(10*sizeof(float));
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2009-1-1 20:39 | 显示全部楼层
C最郁闷的时指针问题啊~经常一不小心就程序就跑不出来了~
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
 楼主| 发表于 2009-1-2 10:03 | 显示全部楼层
指针第一要检查是否初始化然后再使用
第二如果用malloc或者其他方式分配内存了使用要及时free()
第三就是边界
然后注意指针指向的内容一定一定要确定才行:D
都是些简单的原则,但是能时刻记住不容易;)
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网

GMT+1, 2025-2-1 03:41 , Processed in 0.068453 second(s), 19 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表