|
1樓 180.84.33.*
2015-11-27 10:03
#include <stdio.h> //#include <stdlib.h>
/*int *fun() { //int *p = (int *)malloc(sizeof(int)); int a; int *p = &a; *p = 12; return p; }*/
void invert(int *array, int n) { int *i, *j, temp; i = array; j = array + n - 1; while (i < j) { temp = *i; *i = *j; *j = temp; i++; j--; } }
void main() { /*int a[] = {15, -27, 63, 49, 115, 663, 774}; int *const p = &a[2]; printf("%d\n", (*p)++); printf("%d\n", *p);*/
/* int *p2 = fun(); int *p3; *p2 += 2; p3 = fun(); printf("%d\n", *p3); printf("%d\n", *p2);*/ //free(p2); //free(p3);
int a[] = {2, 4, 7, 3, 6, 9, 11, 14, 8}; int len = sizeof(a) / sizeof(int); int i; for (i = 0; i < len; i++) printf("%-3d", *(a + i)); printf("\n");
invert(a, 4); for (i = 0; i < len; i++) printf("%-3d", *(a + i)); printf("\n"); }
|
|
2樓 180.84.33.*
2015-11-27 10:04
學校機房內的電腦雖然默認情況下不能上網,但是把網卡的IP位址改為自動獲取(DNS不需要改為自動獲取),大概半分鐘後就能上網了。
|
|
3樓 180.84.33.*
2015-11-27 10:05
|
|
4樓 巨大八爪鱼
2015-11-27 10:24
當函數要返回一個指針的時候,這個指針不能指向該函數內部定義的局部變量,但可以指向靜態變量或者malloc函數分配的空間。
如果函數真的返回了指向局部變量的指針的話,那麼在主函數中就不得對指向的空間的值進行任何寫操作,只能讀取其值,否則會出錯。
|
|
5樓 202.115.90.*
2015-11-27 16:26
#include <stdio.h>
void main()
{
int x,y,z;
for(x=0;x<=100;x++)
for(y=0;y<=100;y++)
{
z=100-x-y;
if(x*5+y*3+z/3==100)
printf("x=%d,y=%d,z=%d\n",x,y,z);
}
}
|
|
6樓 202.115.90.*
2015-11-27 16:46
#include <stdio.h>
void main()
{
int a = 10;
printf("%d\n", a = a + 2); //12
printf("%d\n", a += 2); //14
}
|
|
7樓 202.115.90.*
2015-11-27 16:50
#include <stdio.h>
void main()
{
char str[] = "abcdefg";
char *pStr = str;
*pStr++ = 'R';
printf("%s\n", str); //Rbcdefg
printf("%s\n", pStr); //bcdefg
}
|
|
8樓 202.115.90.*
2015-11-27 17:24
// Page 211
#include <stdio.h>
#include <string.h>
void insert(char *s1, const char *s2)
{
int len1 = strlen(s1);
int len2 = strlen(s2);
int pos = -1; // when not found
int i;
/* Find the position */
for (i = 0; i < len1; i++)
{
if (*(s1 + i) == *s2)
{
pos = i; // found
break;
}
}
/* Move */
if (pos != -1)
{
for (i = len1; i >= pos - 1; i--)
{
// At first when i == len1, the character is '\0'
// and then '\0' will be moved to the end
*(s1 + i + len2 - 1) = *(s1 + i);
}
}
/* Copy chars from s2 to s1 */
if (pos == -1)
{
pos = len1;
*(s1 + len1) = *s2; // copy the first character of s2
*(s1 + len1 + len2) = '\0';
}
// copy the rest
for (i = 1; i < len2; i++)
*(s1 + pos + i) = *(s2 + i);
// abcdefghijklmn
// f123
// abcdef ghijklmn
// abcdef123ghijklmn
// abcdef
// d45
// abcd ef
// abcd45ef
// abcdef
// 45
// abcdef45
}
void main()
{
char s1[50] = "abcdef";
char s2[50] = "d45";
insert(s1, s2);
printf("%s\n", s1);
strcpy(s1, "abcdef");
strcpy(s2, "45");
insert(s1, s2);
printf("%s\n", s1);
strcpy(s1, "abcdefghijklmn");
strcpy(s2, "f123");
insert(s1, s2);
printf("%s\n", s1);
}
|
|
9樓 202.115.90.*
2015-11-27 17:26
|
|
10樓 202.115.90.*
2015-11-27 17:29
不得不說書上211頁那個版本的程序比我寫的簡潔得多。
|