1. 程式人生 > >C語言 通過指標和二級指標遙控資料

C語言 通過指標和二級指標遙控資料

// PointerArray.cpp : 定義控制檯應用程式的入口點。
//vs2015

#include “stdafx.h”
#include <stdlib.h>
void test1(int* p)
{
*p = 1;
}
void test2(int *p,int n)
{
int i = 0;
while (i<n)
{
*p = 10 * i + 15;
p++;
i++;
}
}
int main()
{
int n = 10;
test1(&n);
printf(“n=10通過指標改變為n=%d\n”, n);//1
int ar[10] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = ar;
test2(p,_countof(ar));
int i=0;
while (i<10)
{
printf(“ar[%d]的資料通過指標變為%d\n”,i, ar[i]);
i++;
}
return 0;
}


// PointerArray.cpp : 定義控制檯應用程式的入口點。
//vs2015

#include “stdafx.h”
#include <stdlib.h>
void test1(char* p)
{
*p = ‘b’;
}
void test2(char *p,int n)
{
int i = 0;
while (i<n)
{
*p = *p - 32;
p++;
i++;
}
}

int main()
{
char c = ‘a’;
test1(&c);
printf(“c=a通過指標改變為c=%c\n”,c);//b
char ar[10] = {‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’,‘h’,‘i’,‘j’};
char *p = ar;
test2(p,_countof(ar));
int i=0;
while (i<10)
{
printf(“ar[%d]的資料通過指標變為%c\n”,i, ar[i]);//A B C D E F G H I J
i++;
}
return 0;
}


// PointerArray.cpp : 定義控制檯應用程式的入口點。
//vs2015

#include “stdafx.h”
#include <stdlib.h>

char s[] = “bbb”;
void test1(char** pp)
{
*pp = “bbb”;
}
void test2(char **pp,int n)
{
char *arr[10] = { “aaa”,“bbb”,“ccc”,“ddd”,“eee”,“fff”,“ggg”,“hhh”,“iii”,“kkk” };
int i = 0;
while (i<n)
{
pp = arr[i];
pp++;
i++;
}
}
int main()
{
char

c = “abbb”;
char **pp = &c;
test1(pp);
printf(“c=abbb通過指標改變為c=%s\n”,c);//bbb
char *ar[10] = {“afdf”,“bdfs”,“csfs”,“dwe”,“dfse”,“fggf”,“cvvg”,“wrh”,“gfdi”,“fdfdj”};
char **pp1 = ar;
//test2(pp1,_countof(ar));
test2(ar, _countof(ar));
int i=0;
while (i<10)
{
printf(“ar[%d]的資料通過指標變為%s\n”,i, ar[i]);//執行結果"aaa",“bbb”,“ccc”,“ddd”,"
i++; ///eee",“fff”,“ggg”,“hhh”,“iii”,“kkk”

}
return 0;

}
在這裡插入圖片描述