显示所有签名的短号码的程序
Program which displays the all signed short numbers
*编辑:我错误地删除了我写的关于使用 short & char 在现代编程中有点过时/效率不高的评论。这个只是为了练习基本的东西。**
此程序从值 0 开始的无符号短整型 "space/world" 中的等价物开始创建并打印一系列有符号短整型值。
**示例:在 short 为 16 位的机器上:
无符号短:0 1 2 .... 65535
=> 签名短:0 1 2 ... 32766 -32767 -32766 -32765 ... -2 -1
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
//Initialize memory pointed by p with values 0 1 ... n
//Assumption : the value of n can be converted to
// short int (without over/under-flow)
unsigned int initArr (short int *p, unsigned int n);
int main (void)
{
const unsigned int lastNumInSeq = USHRT_MAX;
short *p_arr = (short *) malloc ( (lastNumInSeq + 1) * sizeof (short));
short int lastValSet = initArr (p_arr, lastNumInSeq); //returns the "max" val written
// for (unsigned i = 0; i < numOfElem; i++)
// printf ("[%d]=%d \n", i, (*(p_arr + i)));
printf ("lastValSet = %d *(p_arr + lastNumInSeq) = %d ",
lastValSet,*(p_arr + lastNumInSeq ));
return 0;
}
unsigned int initArr (short *p, unsigned int n)
{
unsigned int offset,index = 0;
while (index <= n){
offset = index;
*(p + offset) = ++index -1 ;
}
return offset;
还需要一些其他清理工作。
函数签名应该从
short initArr (short *p, unsigned int n);
至
unsigned int initArr (short *p, unsigned int n);
变量 'lastValSet' 应将其类型更改为 unsigned int。
这条评论也很混乱:
//Assumption : the value of n can be converted to
// short int (without over/under-flow)
应该是这样的:
//Assumption : the value of n which is of type int can be converted to
// short int (without over/under-flow) up to 32767 which is the
// max value for a variable of short type.
*编辑:我错误地删除了我写的关于使用 short & char 在现代编程中有点过时/效率不高的评论。这个只是为了练习基本的东西。**
此程序从值 0 开始的无符号短整型 "space/world" 中的等价物开始创建并打印一系列有符号短整型值。
**示例:在 short 为 16 位的机器上:
无符号短:0 1 2 .... 65535
=> 签名短:0 1 2 ... 32766 -32767 -32766 -32765 ... -2 -1
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
//Initialize memory pointed by p with values 0 1 ... n
//Assumption : the value of n can be converted to
// short int (without over/under-flow)
unsigned int initArr (short int *p, unsigned int n);
int main (void)
{
const unsigned int lastNumInSeq = USHRT_MAX;
short *p_arr = (short *) malloc ( (lastNumInSeq + 1) * sizeof (short));
short int lastValSet = initArr (p_arr, lastNumInSeq); //returns the "max" val written
// for (unsigned i = 0; i < numOfElem; i++)
// printf ("[%d]=%d \n", i, (*(p_arr + i)));
printf ("lastValSet = %d *(p_arr + lastNumInSeq) = %d ",
lastValSet,*(p_arr + lastNumInSeq ));
return 0;
}
unsigned int initArr (short *p, unsigned int n)
{
unsigned int offset,index = 0;
while (index <= n){
offset = index;
*(p + offset) = ++index -1 ;
}
return offset;
还需要一些其他清理工作。
函数签名应该从
short initArr (short *p, unsigned int n);
至
unsigned int initArr (short *p, unsigned int n);
变量 'lastValSet' 应将其类型更改为 unsigned int。
这条评论也很混乱:
//Assumption : the value of n can be converted to
// short int (without over/under-flow)
应该是这样的:
//Assumption : the value of n which is of type int can be converted to
// short int (without over/under-flow) up to 32767 which is the
// max value for a variable of short type.