C point and array practice

#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
                                                                                                                                



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值