c语言指针

指针与一维数组:

指针存储的是变量的地址.

示例程序:

#include <stdio.h>

 

void main(){

       int arr[] = {12, 15, 16};

       int *p = arr;

       p++;

       printf("%d", *p);

}

 

示例程序:

#include <stdio.h>

 

void main(){

       int arr[] = {12, 15, 16};

       int *p = arr;

       *(p+1) = 17;

       p++;

       printf("%d", *p);

}

 

动态分配内存:

示例程序:

#include <stdio.h>

#include <stdlib.h>

 

int main(){

       int *p;

       p = malloc(sizeof(*p));

       if(p == NULL){

              return -1;

       }

       *p = 100;

       printf("%d", *p);

       free(p);

       return 0;

}

 

示例程序:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

void *fun(char *p, int i){

       p = malloc(i);

       strcpy(p, "hello");

       return p;

}

 

int main(){

       char *p = NULL;

       int i=32;

       p = fun(p, i);

       printf("%s", p);

       free(p);

}

posted @ 2013-02-15 14:55  rorshach  阅读(101)  评论(0编辑  收藏  举报