C ch13重點整理-Pointer

範例一:
#include <stdio.h>
int main()
{
  int a=4;
  int b=3;
  int *a_ptr;
  int *b_ptr;
  b_ptr=&b;
  a_ptr=&a;
  printf("%x %d\n",a_ptr,*a_ptr);
    printf("%x %d\n",b_ptr,*b_ptr); 
    return 0;
}
範例二:
#include <stdio.h>
void inc_count(int *count_ptr)
{
    (*count_ptr)++;

}

int main()
{
    int count=0;
while(count<10)
{
    inc_count(&count);
    printf("%d\n",count);
}
    return 0;
}
一、當你使用以下的語法時:
const char *a_ptr = "bitch";

以上的code 其實是告訴 C 被 a_ptr指到的資料是一個 constant,不能被改變;而
a_ptr 指向的位置,其實還是能改變的,如:
#include <stdio.h>
#include <string.h>
int main()
{
    const char *a_ptr="bitch";             //此代表的是 ptr指向一個 const char array,
    printf("%x, %s\n",&a_ptr,a_ptr);        //ptr仍可assign其它的值,但是 *a_ptr則不能再assign
    a_ptr="whore";                      
    printf("%x, %s\n",&a_ptr,a_ptr);
    return 0;
}
另一范例:
main()
{
    char *ptr="hello";
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);    // ptr 是 "hello" 首位址,*ptr及为第一个位址的内容
    *ptr='v';                                                             // 错误! “hello” 所在之内存位置为文字常量区,不能更改其值 
                                                                             // ref: http://blog.csdn.net/chenke1988/article/details/7273375
                                                                             // ref: http://zhidao.baidu.com/question/498837142.html
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);         

}
经修改:
main()
{
    char a[]="hello",*ptr;
    ptr=a;
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);
    *ptr='j';
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);

}<span style="font-family: Arial; background-color: rgb(255, 255, 255);"> </span>
  图片

在加了 const 变成 const *char ptr; 後
 main()
{
    char a[]="hello";
    const char *ptr=a;                                                  //ptr的指向的位置可改,其指向的内容不可改
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);
    *ptr='j';
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);
}
图片
二、但是如果將 const 放到 * 之後,則代表 pointer 是個 const 如:
ex1: 
#include <stdio.h>
#include <string.h>
int main()
{
    char *const a_ptr="bitch"; // 代表 a_ptr是一個constant的pointer,不能被改變
                                                  // 而被指的資料,則可以被改變
                                                  //a_ptr="new";     非法
    *a_ptr="whore";                     //合法,但直接出現錯誤訊息後關掉程式 囧
     return 0;
}
ex2:
main()
{
    char a[]="hello";
    char *const ptr=a;                              //ptr的指向的位置为const,其指向内容可改
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);
    *ptr='j';
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);
}
  图片
而 ptr指向的地址不能改
main()
{
    char a[]="hello";
    char b[]="jello";
    char *const ptr=a;                             
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);
    ptr=b;
    printf("%p--%p--%s--%c\n",&ptr,ptr,ptr,*ptr);

}
图片 
三、而如果將const 放在二個地方的話,則產生了一個pointer 固定指向一個constant 的data,如:
const char *const a_ptr="adventure";
note: char const  *ptr 和 const char *ptr 意思是一样的,就是其所指的内容为常数,不可更动。
#include <stdio.h>
#include <string.h>
int main()
{
    char array[5]="four";
    char *array_ptr=&array[0];
    printf("%-10p,%c\n",array_ptr,*array_ptr);
    array_ptr++;
    printf("%-10p,%c\n",array_ptr,*array_ptr);
    array_ptr++;
    printf("%-10p,%c\n",array_ptr,*array_ptr);
    array_ptr++;
    printf("%-10p,%c\n",array_ptr,*array_ptr);
    printf("%-10p,is address varable's address\n",&array_ptr);
    return 0;
}
图片
當print pointer的時候,要使用 %p,详参  printf() 涵数使用方式
#include <stdio.h>
swap(int *a, int *b)
{
    int tmp;
    tmp= *a;
    *a = *b;
    *b = tmp;
}

// int *a, *b 都是形参,所以在涵数结束後,就消失了,
// 因此不能达到 swap 的效果,所以我们该做的是改变指针指向
// 的内容,而不是指针的位置 
swap_bad(int *a,int *b)
{
    int *tmp;
    tmp =a;
    a=b;
    b=tmp;
}

main()
{
    int a=2,b=3;
    printf("a =%d, b=%d\n",a,b);
    swap_bad(&a,&b);
    printf("a =%d, b=%d\n",a,b);
}
但为啥没有初始化 ps,便会产生错误?
图片
main(){
    char st[20],*ps;
    int i;
    printf("input a string:\n");
    ps=st;
    scanf("%s",ps);
    for(i=0;ps[i]!='\0';i++)
        if(ps[i]=='k'){
            printf("there is a 'k' in the string\n");
            break;
        }
    if(ps[i]=='\0') printf("There is no 'k' in the string\n");
}<span style="font-family: Arial; background-color: rgb(255, 255, 255);"> </span>
因为没有给ps指针分配内存空间,导致出现段错误。
ps=st不会报错,是因为之前ps指向了st,有了 20个字节空间
如果不想段错误一种是和st一样
栈(stack)分配空间
C/C++ code
?
1
char  ps[20];
也可以堆(heap)分配
C/C++ code
?
1
2
#include <unistd.h>
char  *ps = ( char  *) malloc (20);
ref:  http://bbs.csdn.net/topics/390599138

copy String with pointer
#include <stdio.h>
cpystr(char *src,char *dst)
{
    //在此処将来源(src)的值复制到目的地(dst)并加以判断 
    while((*dst=*src)!='\0')
    {
         src++;
         dst++;
    }
    // 可以简化为 cprstr(char *pss,char*pds) {while ((*pds++=*pss++)!=`\0');}
    // 再进一步简化为: cprstr (char *pss,char *pds) {while (*pdss++=*pss++);}
}

main()
{
    char *source="china", *destination,a[10];
    // 对 pointer 进行复始化,没了这一行,会有 Segmentation Fault 报错,why? ANS: 
    destination=a;
    cpystr(source,destination);
    printf("origin: %s, after: %s\n",source,destination);
}
int argc指的是執行程式有argument數,如:
#hello -v 
其 argc 即為 2 而char *argv[ ]是指向argument的char array pointer ,在
#hello -v -h -o 中,
"hello" 在 argv[0] ;
"-v" 在argv[1];
"-h" 在argv[2]; 
"-o" 在argv[3] 
argv[1][0]是 "-" 而argv[1][1] 是 "v"
argv[2][0]是 "-" 而argv[2][1] 是 "h"
等等

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

char *out_file="print.out";
char *program_name;

void first_function(void)
{
    printf("this is the first function\n");
}

void program_description(void)
{
    fprintf(stderr,"%s is a program for blabla use\n",program_name);
}

int main(int argc, char *argv[])
{
    program_name=argv[0];
while ((argc>1)&&(argv[1][0]=='-'))
{
   switch (argv[1][1])
{
   case 'h':
   program_description();
   break;
case 'e1':
       first_function();
break;
}
++argv;
--argc;
}
if(argc==1)
{
   first_function();
}
else
{
   while (argc>1)
{
   first_function();
++argv;
--argc;
}
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值