#include "stdio.h"
#include "stdlib.h"
#define SIZE 2
#include "string.h"
void myprint(int *p) {
printf("array[1]= %d\n",*p);
p++;
printf("array[1]= %d\n",*p);
}
main () {
//using multi array
//multi array will allocate memory directly in stack
printf("****************************\n");
char president[SIZE][10] = {0};
char (*p)[10];
p = president;
strcpy((char *)p,"aa");
p++;
strcpy((char *)p,"bb");
int x,index;
for(x=0;x<SIZE;x++)
{
index = 0;
while(president[x][index] != '\0')
{
putchar(president[x][index]);
index++;
}
putchar('\n');
}
//-----using array which contain pointer, you still need malloc memory for the pointer's content
printf("****************************\n");
char *pointarray[10];
char *q;
q=(char* )malloc(20);
strcpy(q,"aaaaaa");
pointarray[1] = q;
printf("pointarray[1]= %s\n",pointarray[1]);
printf("size of char * %lud\n", sizeof(char *));
printf("size of point %lud\n", sizeof(int *));
//-------using double ** pointer, you need first malloc memeory for pointer self!!---
//allocate two pointer's space, each pointer's content is still pointer
printf("****************************\n");
char **doublestar= (char **)malloc(2*sizeof(char *));
char **tmp = doublestar;
char *tmpp;
char *tmpq;
tmpp=(char* )malloc(20);
tmpq=(char* )malloc(20);
strcpy(tmpp,"abcbbb");
strcpy(tmpq,"abcaaa");
*tmp= tmpp;
tmp++;
*tmp= tmpq;
printf("*doublestar is %s\n",*doublestar);
doublestar++;
printf("*doublestar is %s\n",*doublestar);
//---------using array name as function parameter
printf("****************************\n");
int intarray[2];
intarray[0] = 2;
intarray[1] = 4;
myprint(intarray);
return(0);
}
Result:
gcc -g -c aa.c
gcc -g -o aa aa.o
./aa
****************************
aa
bb
****************************
pointarray[1]= aaaaaa
size of char * 8d
size of point 8d
****************************
*doublestar is abcbbb
*doublestar is abcaaa
****************************
array[1]= 2
array[1]= 4
#include "stdio.h"
#include "stdlib.h"
#define SIZE 2
#include "string.h"
main () {
int i = 5;
int *p =NULL;
printf("addr of i = %p\n", (void *)&i);
printf("addr of p = %p\n", &p);
printf("value of p = %p\n", p);
printf("size of p = %ld\n", sizeof(p));
p = &i;
printf("addr of i = %p\n", (void *)&i);
printf("addr of p = %p\n", &p);
printf("value of p = %p\n", p);
return(0);
}
~
gcc -g -c aa.c
gcc -g -o aa aa.o
./aa
addr of i = 0x7fff7e04c044
addr of p = 0x7fff7e04c048
value of p = (nil)
size of p = 8
addr of i = 0x7fff7e04c044
addr of p = 0x7fff7e04c048
value of p = 0x7fff7e04c044