数组训练2
TeX中的引号转换
Fgetc(fin)读取一个打开的文件fin,读取一个字符,然后返回一个int值
//为什么返回一个int而不是char?
//如果文件结束,返回特殊标记EOF,它并不是一个char。如果返回char,将无法把EOF与普通字符区分
//如果要用标准输入读取一个字符,可以用getchar,等价于fetc(stdin),
fgets(buf, maxn, fin) 用于读取完整的一行,其中buf的声明意为char buf [maxn], 此函数读取不超过maxn-1个字符,然后在末尾添加“\0". 此函数读取到回车符\n即停止,
TEX中的引号,逻辑取反的妙用
#include
int main()
{
int urn[5] = { 100, 200, 300, 400, 500};
int * ptr1, *ptr2, *ptr3;
ptr1 = urn;
ptr2 = &urn[2];
printf("pointer value, dereferenced poniter, pointer address:\n");
printf("ptrl = %p, *ptrl = %d, &ptrl = %p\n", ptr1, *ptr1, &ptr1);
//指针加法
ptr3 = ptr1 + 4;
printf("\nadding an int to a pointer: \n");
printf("ptr1 + 4 = %p, *(ptr1 + 4) = %d\n", ptr1 + 4, *(ptr1 + 4));
printf("\nvalue after ptr1++\n");
ptr1++;
printf("ptr1 = %p, *ptr1 = %d, &ptr = %p", ptr1, *ptr1, &ptr1);
ptr2--;
printf("\nvalue after ptr2--");
printf("ptr2 = %p, *ptr2 = %d, &ptr2 = %p", ptr2, *ptr2, &ptr2);
--ptr1;
++ptr2;
printf("\nPointers reset to original values:");
printf("ptr1 = %p, ptr2 = %p\n", ptr1, ptr2);
//指针之间相减
printf("\nSubtrating one pointer from another:\n");
printf("ptr2 = %p. ptr1 = %p, ptr2 - ptr1 = %td\n", ptr2, ptr1, ptr2 - ptr1);
//指针减去整数
printf("\nSubtrating an int from a pointer:\n");
printf("ptr3 = %p, ptr3 - 2 = %p\n", ptr3, ptr3 - 2);
return 0;
}
注意:数组和指针的区别,其地址是不同的,如指针型变量tr1(“tr1”,%p)指的是数组tr1[0]的地址,而(“&tr1”, %p)才是指针地址
#include
#define SIZE 5
void show_array (const double ar[], int n);
void mult_array (double ar[], int n, double mult);
int main(void)
{
double dip[SIZE] = { 20.0, 17.66, 8.2, 15.3, 22.22};
printf("The original dip array:\n");
show_array(dip, SIZE);
mult_array(dip, SIZE, 2.5);
printf("The dip array after calling mult_array():");
show_array(dip, SIZE);
return 0;
}
void show_array(const double ar[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%8.3f ", ar[i]);
putchar('\n');
}
void mult_array(double ar[], int n, double mult)
{
int i;
for(i = 0; i < n; i++)
ar[i] *= mult;
}