bin554385863
发表于 2019-5-21 23:01:43
======================================================================
2019年5月21日22:56:47
错误代码
/*用冒泡法对20个随机元素的数组排序*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void mppx(int a[])
{
int i, j, t, s;
s = sizeof(a) / sizeof(a);
for (i = 0; i < s; i++)
{
for (j = 0; j < (s - 1); j++)
{
if (a >= a)
{
t = a;
a = a;
a = t;
}
}
}
for (i = 0; i < s; i++)
{
printf("a[%d] = %d\n", i, a);
}
}
void main()
{
int b, i;
srand(time(NULL));
printf("排序之前\n");
for (i = 0; i < 20; i++)
{
b = rand() % 20;
}
for (i = 0; i < 20; i++)
{
printf("b[%d] = %-3d\n", i, b);
}
printf("排序之后\n");
mppx(b);//调用函数对=数组b进行排序
}
----------------------------------------------------------------------------------------------------------------------------
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-cgfzor0y.cvw --stdout=Microsoft-MIEngine-Out-b3zzwf1o.v24 --stderr=Microsoft-MIEngine-Error-0irwvajd.iqa --pid=Microsoft-MIEngine-Pid-dutcx0ls.r0s --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
排序之前
b = 8
b = 15
b = 15
b = 13
b = 0
b = 13
b = 18
b = 14
b = 11
b = 1
b = 5
b = 14
b = 7
b = 14
b = 7
b = 11
b = 0
b = 2
b = 9
b = 19
排序之后
a = 8
-------------------------------------------------------------------------------------------------------------------------------------------------------
正确代码
/*用冒泡法对20个随机元素的数组排序*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void mppx(int a[],int s)
{
int i, j, t;
// printf("s=%d",s);
for (i = 0; i < s; i++)
{
for (j = 0; j < (s - 1); j++)
{
if (a >= a)
{
t = a;
a = a;
a = t;
}
}
}
for (i = 0; i < s; i++)
{
printf("a[%d] = %d\n", i, a);
}
}
int main()
{
int b, i,s;
srand(time(NULL));
printf("排序之前\n");
for (i = 0; i < 20; i++)
{
b = rand() % 20;
}
for (i = 0; i < 20; i++)
{
printf("b[%d] = %-3d\n", i, b);
}
printf("排序之后\n");
s = sizeof(b) / sizeof(b);
mppx(b,s);//调用函数对=数组b进行排序
return 0;
}
----------------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-1jwo4tef.pgo --stdout=Microsoft-MIEngine-Out-lglz1gow.5jn --stderr=Microsoft-MIEngine-Error-u20a5nqm.yfb --pid=Microsoft-MIEngine-Pid-bcdh4jxk.dpt --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
排序之前
b = 11
b = 15
b = 8
b = 4
b = 13
b = 6
b = 7
b = 8
b = 19
b = 13
b = 14
b = 16
b = 14
b = 3
b = 1
b = 8
b = 8
b = 14
b = 16
b = 0
排序之后
a = 0
a = 1
a = 3
a = 4
a = 6
a = 7
a = 8
a = 8
a = 8
a = 8
a = 11
a = 13
a = 13
a = 14
a = 14
a = 14
a = 15
a = 16
a = 16
a = 19
E:\Administrator\Documents\My C>
-----------------------------------------------------------------------
原因错误
当将数组作为实参传递到一个函数中时,就是将数组的首地址作为实参,
函数的形参相当于一个指针,mppx函数里的sizeof(a)得到的是指针的大小为8,
而int大小为4,相除得2,所以你的程序只会排序前两个元素。
感谢:TOP_LK 和 Croper提供的帮助
bin554385863
发表于 2019-5-23 22:35:11
本帖最后由 bin554385863 于 2019-5-24 13:06 编辑
2019年5月23日22:07:12
关于数组的练习
将一个二维数组的行列互换
/*数组的行列互换*/
#include <stdio.h>
void main()
{
int t, barr;
int arr =
{
{3, 6, 5},
{4, 9, 1},
};
printf("行列互换之前\n");
for (size_t i = 0; i < 3; i++)
{
for (size_t j = 0; j < 2; j++)
{
printf("arr[%d][%d] = %d ", j, i, arr);
t = arr;
barr = t;
}
printf("\n");
}
printf("行列互换之后\n");
for (size_t i = 0; i < 2; i++)
{
for (size_t j = 0; j < 3; j++)
{
printf("barr[%d][%d] = %3d ", j, i, barr);
}
printf("\n");
}
}
--------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-sa5aw4kl.t4q --stdout=Microsoft-MIEngine-Out-hrlc5pbu.u2s --stderr=Microsoft-MIEngine-Error-kkqfbvop.r55 --pid=Microsoft-MIEngine-Pid-q3i4ooyd.rng --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
行列互换之前
arr = 3 arr = 4
arr = 6 arr = 9
arr = 5 arr = 1
行列互换之后
barr = 3 barr = 6 barr = 5
barr = 4 barr = 9 barr = 1
E:\Administrator\Documents\My C>
----------------------------------------------------------------------------------------------
2019年5月24日00:39:28
定义一个含有30个偶数的数组并安顺每五个元素求一次平均值放到另一个数组中
/*定义一个含有30个偶数的数组并安顺每五个元素求一次平均值放到另一个数组中*/
#include <stdio.h>
#define N 30
void main()
{
int arr, barr[(N / 5) + 1], d;
printf("原数组arr\n");
for (size_t i = 0; i < N; i++)
{
arr = 2 * i;
printf("arr[%d] = %-3d\n", i, arr);
}
printf("\n");
printf("\n");
printf("每五个元素算其平均值并放在数组Barr中\n");
for (size_t j = 0; j < (N / 5); j++)
{
for (size_t i = 5 * j; i < 5 * (j + 1); i++)
{
barr = (arr + arr + arr + arr + arr) / 5;
}
printf("barr[%d] = %3d ", j, barr);
}
printf("\n");
}
----------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-z0zalo4w.oml --stdout=Microsoft-MIEngine-Out-qbpyg1yo.45v --stderr=Microsoft-MIEngine-Error-oblvt4y4.fjz --pid=Microsoft-MIEngine-Pid-0p5hfamo.wcz --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
原数组arr
arr = 0
arr = 2
arr = 4
arr = 6
arr = 8
arr = 10
arr = 12
arr = 14
arr = 16
arr = 18
arr = 20
arr = 22
arr = 24
arr = 26
arr = 28
arr = 30
arr = 32
arr = 34
arr = 36
arr = 38
arr = 40
arr = 42
arr = 44
arr = 46
arr = 48
arr = 50
arr = 52
arr = 54
arr = 56
arr = 58
每五个元素算其平均值并放在数组Barr中
barr = 4 barr = 6 barr = 8 barr =10 barr =12 barr =14
E:\Administrator\Documents\My C>
平均值输出不正确
-----------------------------------------------------------------------------------------------------------------------------
2019年5月24日13:05:29
正确代码
#include <stdio.h>
#define N 30
int main()
{
int arr, barr;
printf("原数组arr\n");
for (size_t i = 0; i < N; i++)
{
arr = 2 * i;
printf("arr[%d] = %d\n", i, arr);
}
printf("\n");
printf("每五个元素算其平均值并放在数组Barr中\n");
for (size_t j = 0; j < (N / 5); j++)
{
// 在这里不需要第二个循环了
barr = (arr + arr + arr + arr + arr)/5;
printf("barr[%d] = %d ", j, barr);
}
printf("\n");
return 0;
}
--------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-qlsng4kz.mkj --stdout=Microsoft-MIEngine-Out-srgg4bbc.axk --stderr=Microsoft-MIEngine-Error-53xphomf.yiv --pid=Microsoft-MIEngine-Pid-5y15xswl.wf2 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
原数组arr
arr = 0
arr = 2
arr = 4
arr = 6
arr = 8
arr = 10
arr = 12
arr = 14
arr = 16
arr = 18
arr = 20
arr = 22
arr = 24
arr = 26
arr = 28
arr = 30
arr = 32
arr = 34
arr = 36
arr = 38
arr = 40
arr = 42
arr = 44
arr = 46
arr = 48
arr = 50
arr = 52
arr = 54
arr = 56
arr = 58
每五个元素算其平均值并放在数组Barr中
barr = 4 barr = 14 barr = 24 barr = 34 barr = 44 barr = 54
E:\Administrator\Documents\My C>
感谢版主shuofxz提供的帮助{:5_109:}
==========================================================================
bin554385863
发表于 2019-5-25 00:03:08
2019年5月25日00:01:17
定义一个5行7列的数组,找出第一次出现负数的位置
/*定义一个5行7列的数组,找出第一次出现负数的位置*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int m, n, s;
srand(time(NULL));
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 5; j++)
{
m = rand() % 35;
n = rand() % 35;
s = m - (m + n) / 2;
printf("s[%d][%d] = %3d\t\t", j, i, s);
}
printf("\n");
}
for (size_t i = 0; i < 7; i++)
{
for (size_t j = 0; j < 5; j++)
{
if (s < 0)
{
printf("首次出现负数的位置是:\ns[%d][%d] = %2d", j, i, s);
break;
}
}
break;
}
}
-------------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-rd54f5xo.24w --stdout=Microsoft-MIEngine-Out-fjhxc1tx.20d --stderr=Microsoft-MIEngine-Error-wzqvcvka.1qh --pid=Microsoft-MIEngine-Pid-cdalh41e.qyj --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
s = 0 s = 6 s =-6 s =16 s = 3
s =15 s =-1 s =-3 s = 3 s =10
s = -14 s =-1 s = 3 s =-1 s =-1
s =-7 s = 3 s = 1 s =-1 s = 5
s =-5 s =-2 s =-4 s =-4 s = -13
s = 1 s =-4 s = -15 s = 8 s =14
s = 7 s =-1 s =-6 s = -12 s = 1
首次出现负数的位置是:
s = -6
E:\Administrator\Documents\My C>
==========================================================================================================
bin554385863
发表于 2019-5-25 21:44:11
本帖最后由 bin554385863 于 2019-5-25 23:31 编辑
定义数组a,b,c,
如果a=b则c=0;
如果a>b则c=1;
如果a<>b则c=-1;
/*
定义数组a,b,c,
如果a=b则c=0;
如果a>b则c=1;
如果a<>b则c=-1;
*/
#include <stdio.h>
#include <time.h>
int main()
{
int m = 4, n = 6;
int a, b, c;
srand(time(NULL));
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < m; j++)
{
a = rand() % m + n;
printf("a[%d][%d]= %2d\t\t", j, i, a);
b = rand() % n + m;
printf("b[%d][%d]= %2d\t\t", j, i, b);
}
printf("\n");
}
printf("\n");
for (size_t i = 0; i < n; i++)
{
for (size_t j = 0; j < m; j++)
{
if (a == b)
{
c = 0;
}
c = a > b ? 1 : -1;
printf("c[%d][%d]= %2d\t\t", j, i, c);
}
printf("\n");
}
return 0;
}
================================================================================
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ucqwtzta.slt --stdout=Microsoft-MIEngine-Out-xfn4vq1c.dfr --stderr=Microsoft-MIEngine-Error-wnfpu4ev.1pi --pid=Microsoft-MIEngine-Pid-zli5oum2.ua2 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
a=6 b=5 a=6 b=4 a=6 b=6 a=6 b=9
a=6 b=8 a=8 b=9 a=9 b=6 a=8 b=7
a=6 b=8 a=9 b=6 a=7 b=4 a=9 b=6
a=8 b=7 a=8 b=4 a=9 b=7 a=9 b=7
a=8 b=4 a=9 b=9 a=6 b=6 a=7 b=9
a=8 b=4 a=8 b=8 a=7 b=8 a=7 b=4
c=1 c=1 c= -1 c= -1
c= -1 c= -1 c=1 c=1
c= -1 c=1 c=1 c=1
c=1 c=1 c=1 c=1
c=1 c= -1 c= -1 c= -1
c=1 c= -1 c= -1 c=1
E:\Administrator\Documents\My C>
=================================================================================================================
输入两个字符串a,b;
不用strcat()函数将b字符串的前五个字符链接到a字符串
/*输入两个字符串a,b;
不用strcat()函数将b字符串的前五个字符链接到a字符串*/
#include <stdio.h>
#include <time.h>
int main(int argc, char const *argv[])
{
int m, n;
srand(time(NULL));
m = rand() % 10 + 5;
n = rand() % 10;
char a, b;
//字符串a的元素
printf("字符串a:\n\n");
for (size_t i = 0; i < m; i++)
{
a = rand() % 25 + 65;
}
a = '\0'; //必须在后面追加'\0', printf("%s", a);才能正确输出.
printf("%s\n\n", a);
//字符串b的元素
printf("字符串b:\n\n");
for (size_t j = 0; j < n; j++)
{
b = rand() % 25 + 97;
}
b = '\0';
printf("%s\n\n", b); //字符串输出
//改变后的字符串
printf("改变后的字符串a:\n\n");
for (size_t i = 0; i < m + 5; i++)
{
for (size_t j = 0; j < 5; j++)
{
a = b;
}
}
a = '\0';
printf("%s", a);
return 0;
}
------------------------------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-0nklvg1r.5ka --stdout=Microsoft-MIEngine-Out-44sij3ef.guz --stderr=Microsoft-MIEngine-Error-gvrjspxo.ae4 --pid=Microsoft-MIEngine-Pid-apibiaj4.ji1 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
字符串a:
VAXWUITRPGYM
字符串b:
axngqon
改变后的字符串a:
VAXWUITRPGYMaxngq
E:\Administrator\Documents\My C>
===========================================================================================
bin554385863
发表于 2019-5-28 00:16:46
本帖最后由 bin554385863 于 2019-5-29 22:25 编辑
2019年5月28日00:12:54
自定义求数字数组长度的函数
#/*数组长度函数*/
#define el "'e'"
/*用字符e表示一个数字数组的结束,el = endline*/
size_t arrlen(size_t arr[])
{
size_t i = 1;
while (*(arr + i) != el)
{
i++;
}
return i;
}
int main(int argc, char const *argv[])
{
int a[] = {0,0,0,0,0,0,0,000};
int b[] = {9,8,7,6,5,4,3,2,1,0,000};
int c[] = {0,0,0,0,0,5,6,7,8,9,000};
printf("arrlen(a) = %d\n", arrlen(a));
printf("arrlen(b) = %d\n", arrlen(b));
printf("arrlen(c) = %d\n", arrlen(c));
return 0;
}
----------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-mmkvfc0s.o5m --stdout=Microsoft-MIEngine-Out-vajmpcnb.jhu --stderr=Microsoft-MIEngine-Error-yo1jcxxc.c3z --pid=Microsoft-MIEngine-Pid-uaand0zi.ih3 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
arrlen(a) = 7
arrlen(b) = 10
arrlen(c) = 10
E:\Administrator\Documents\My C>
bin554385863
发表于 2019-5-29 17:57:39
本帖最后由 bin554385863 于 2019-5-30 12:36 编辑
2019年5月29日17:58:03
自定义数字数组函数,arrlen(size_t arr)
/*数组长度函数*/
#define el "'e'"
/*用字符e表示一个数字数组的结束,el = endline*/
size_t arrlen(size_t arr[])
{
size_t i = 1;
while (*(arr + i) != el)
{
i++;
}
return i;
}
数字数组拼接函数arrcat()
/*数字数组拼接函数arrcat(arr1, arr2)*/
void arrcat(size_t arr1[], size_t arr2[])
{
size_t temp;
size_t i, j, k;
for (i = 0; i < arrlen(arr1); i++)
{
temp = arr1;
}
for (j = 0; j < arrlen(arr2); j++)
{
temp = arr2;
}
for (k = 0; k < i + j; k++)
{
arr1 = temp;
arr1 = el;
printf("%d ", arr1);
}
}
请确保arr1有足够的空间接受arr2的数据!!!!!
数组冒泡排序函数arrsrt(arr)
/*冒泡排序函数*/
size_t arrsrt(size_t arr[])
{
int t;
for (size_t i = 0; i < arrlen(arr); i++)
{
for (size_t j = 0; j <arrlen(arr) - 1; j++)
{
if (arr >= arr)
{
t = arr;
arr = arr;
arr = t;
}
}
}
-------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include "E:\Administrator\Documents\My C\My C Function\Function.c"
void main()
{
int a = {0, 1, 2, 3, el};
int b[] = {7, 5, 8, 4, el};
int c[] = {6, 9, 10, 11, el};
printf("拼接a[]和b[]\n");
arrcat(a, b);
printf("\n\n");
printf("然后拼接c[]\n");
arrcat(a, c);
printf("\n\n");
printf("对数组排序\n");
arrsrt(a);
printf("\n\n");
printf("打印数组长度\n");
printf("%d", arrlen(a));
}
---------------------------------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-rbvr20ft.zwy --stdout=Microsoft-MIEngine-Out-bh5shobw.1iv --stderr=Microsoft-MIEngine-Error-fj1kw2qv.ntj --pid=Microsoft-MIEngine-Pid-l5zgjw2m.gcu --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
拼接a[]和b[]
0 1 2 3 7 5 8 4
然后拼接c[]
0 1 2 3 7 5 8 4 6 9 10 11
对数组排序
0 1 2 3 4 5 6 7 8 9 10 11
打印数组长度
12
E:\Administrator\Documents\My C>
===================================================================================================
数字数组查找函数arrsch()
/*数组搜索函数*/
void arrsch(size_t arr[], size_t key)
{
size_t temp;
size_t j = 0;
for (size_t i = 0; i < arrlen(arr); i++)
{
if (*(arr + i) == key)
{
temp = i;
j++;
}
}
if (j >= 1)
{
printf("数据:%d\n出现次数:%d\n下标为:", key, j);
for (size_t i = 0; i < j; i++)
{
printf("%02d ", temp);
}
}
else
{
printf("数据: %d\n出现次数: %d\n下标为: NULL", key, 0);
}
}
bin554385863
发表于 2019-5-30 23:09:24
2019年5月30日23:08:34
指针版arr系列函数
#include <stdio.h>
/*用字符e表示一个数字数组的结束,el = endline*/
#define el "'e'"
/*数组长度函数*/
size_t arrlen(size_t arr[])
{
size_t i = 0;
while (*arr != el)
{
arr++;
i++;
}
return i;
}
/*数字数组排序函数*/
void arrsrt(size_t arr[])
{
for (size_t i = 0; i < arrlen(arr); i++)
{
for (size_t j = 0, k = 0; j < arrlen(arr) - 1; j++)
{
if (*(arr + j) > *(arr + j + 1))
{
k = *(arr + j);
*(arr + j) = *(arr + j + 1);
*(arr + j + 1) = k;
}
}
}
return arr;
}
/*数字数组拼接函数arrcat(arr1, arr2)*/
void arrcat(size_t arr1[], size_t arr2[])
{
size_t i = 1;
while (*arr1 != el)
{
arr1++; //跳到arri尾地址
}
while (*arr2 != el)
{
*(arr1++) = *(arr2++); //复制arr2数据到arr1尾部
}
*arr1 = el;
}
bin554385863
发表于 2019-6-1 00:38:35
2019年6月1日00:38:58
/*数字数组替换函数*/
int arrrpl(size_t arr[], size_t key, size_t num)
{
assert(arr != NULL);
size_t i = 0;
for (; *arr != el; arr++)
{
if (*arr == key)
{
i++;
break;
}
}
switch (i)
{
case 0:
return 0;
break;
case 1:
if ((*arr == 0) && (key == 0))
{
*arr = num;
}
else
{
for (; *arr != el; arr++)
{
if (*arr == key)
{
*arr = num;
}
}
}
return arr;
break;
}
}
cato
发表于 2019-6-2 14:39:56
真厉害
多放点香菜
发表于 2019-6-2 18:49:55
我去这么多了,才看见,顶楼主!
bin554385863
发表于 2019-6-2 20:52:00
多放点香菜 发表于 2019-6-2 18:49
我去这么多了,才看见,顶楼主!
{:10_245:}谢谢
bin554385863
发表于 2019-6-2 21:18:34
多放点香菜 发表于 2019-6-2 18:49
我去这么多了,才看见,顶楼主!
谢谢{:10_256:}
多放点香菜
发表于 2019-6-2 21:35:31
bin554385863 发表于 2019-6-2 21:18
谢谢
哈哈我才学,一起加油!{:10_298:}
bin554385863
发表于 2019-6-2 23:20:45
本帖最后由 bin554385863 于 2019-6-2 23:35 编辑
2019年6月2日22:56:04
指针
指针定义: 数据类型 *指针变量名 = &普通变量;
或: 数据类型 *指针变量名 ;
*指针变量名 = &普通变量;
注意:
定义指针时是如果不指向某个变量地址,最好初始化为NULL,以防出现野指针,对程序造成某种bug;
如:
int *p = NULL;(表示指针p不指向任何一个地址)
指针的值:
指针有两个值
一个是其本身的值代表指向内存中的某个地址;
另一个是其指向的地址存储的值.
如:
int a = 1;
int *p = &a;
变量p的值是&a(变量a在内存中的地址);
*p的值是1.(因此我们有两种方式访问变量a,一种是通过变量名a直接访问,一种是通过指针p间接访问).
#include <stdio.h>
#include <assert.h>
#include "E:\Administrator\Documents\My C\My C Function\Function.c"
int main(int argc, char const *argv[])
{
int *p = NULL;//初始化为NULL防止野指针
int a = 100;
p = &a;//也可以直接写作int *p = &a在定义时就直接赋值.
printf("p的值是:%#X\n", p); //以十六进制输出p的值
printf("*p的值是:%d\n", *p);
printf("变量a原值是:%d\n", a);
*p = 200; //通过指针改变变量a得值
printf("改变后变量a的值是:%d\n", a);
return 0;
}
----------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-35uppg2t.wjt --stdout=Microsoft-MIEngine-Out-32uwo45b.pld --stderr=Microsoft-MIEngine-Error-2ufk5rln.hgu --pid=Microsoft-MIEngine-Pid-sg3ydrof.huu --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
p的值是:0X28FF38
*p的值是:100
变量a原值是:100
改变后变量a的值是:200
E:\Administrator\Documents\My C>
=================================================================================
bin554385863
发表于 2019-6-3 23:25:45
本帖最后由 bin554385863 于 2019-6-3 23:31 编辑
2019年6月3日23:15:25
指针和数组
指针和一维数组
如;
int arr = {0,1,2}
int *p = NULL;
p = arr;
*/arr是数组的首地址,数组名arr也可以当做指针来用,然鹅数组名作为指针是不能进行计算的,
因为数组地址是不可改变的.但是指针p可以进行运算(++和--)*/
for (size_t i =0 ; i < 3 ; i++ )
{
print("%d",*(p+1);//等同于print("%d",a )
}
指针和二维数组
如:
#include <stdio.h>
#include <assert.h>
#include <math.h>
#include "E:\Administrator\Documents\My C\My C Function\Function.c"
int main(int argc, char const *argv[])
{
int a = {{0, 1, 2, 3},
{
4,
5,
6,
7,
},
{
8,
9,
10,
11,
}};
for (size_t i = 0; i < 3; i++)
{
printf("%2d ", **(a+i));
}
printf("\n\n");
for (size_t i = 0; i < 12; i++)
{
printf("%2d ", *(*a+i));
}
}
------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-j3ohzhx4.gdo --stdout=Microsoft-MIEngine-Out-pzhlltcc.bnn --stderr=Microsoft-MIEngine-Error-cbkxz01q.ggo --pid=Microsoft-MIEngine-Pid-fe5ad35b.qjs --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
048
0123456789 10 11
E:\Administrator\Documents\My C>
---------------------------------------------------------------
由程序可知
上述代码中:
**(a+i)是指向每一列的首选地址
*(*a+i)代表指向下一个元素的地址.
1291917670
发表于 2019-6-4 03:06:40
厉害
bin554385863
发表于 2019-6-4 08:08:08
谢谢^_^
bin554385863
发表于 2019-6-4 08:10:18
1291917670 发表于 2019-6-4 03:06
厉害
谢谢^_^
bin554385863
发表于 2019-6-5 21:58:42
2019年6月5日21:59:13
闭关苦修指针ing
bin554385863
发表于 2019-6-9 12:35:10
2019年6月9日12:35:37
MMP,接了个大活,每天都没时间学习了