zoukankan      html  css  js  c++  java
  • 指针与数组的异同

    指 针与数组的异同

    http://www.javaeye.com/wiki/c/785-%E6%8C%87%E9%92%88%E4%B8%8E%E6%95%B0%E7%BB%84%E7%9A%84%E5%BC%82%E5%90%8C 

    原创作者: simohayha   阅读:1010次   评论:0条   更新时间:2008-12-15    
    指针是c的灵魂,俺这里只能抛砖引玉了.
     
    1  首先,数组名不能当作赋值对象,比如下面的代码:
    C代码
    1. char *s="abc";  
    2.     char *s1="bcd";  
    3.     s1=s;  
    4.     printf("%c\n",s1[0]);  
    char *s="abc"; 	char *s1="bcd"; 	s1=s; 	printf("%c\n",s1[0]); 
    可以正常运行,如果把 这边的指针变为数组就会出错。

    2 下面阐述一下,指针和数组各自是如何访问的:
    C代码
    1. char s[]="abc"; c=s[i];  


    编译器符号表有一个符号 s 地址为 1234,然后首先取i的值,把i和1234相加,然后取出(i+1234)的内容付给c.

    C代码
    1. char *s="abc"; c=s[i];  

    编译器符号表有一个符号s,他的地址为1234,然后取地址1234的内容,就是'5678',然后把i和5678相加,然后取出 (i+5678)的内容付给c.

    大家可以看下下面的这个程序:
    C代码
    1. #include <stdio.h>  
    2. void main()  
    3. {  
    4.     char *s="abc";  
    5.     char s2[]="789";  
    6.     printf("%d\n",&s);  
    7.     printf("%d\n",&s[0]);  
    8.     printf("%d\n",&s2);  
    9.     printf("%d\n",&s2[0]);  
    10. }  
    #include <stdio.h> void main() { 	char *s="abc"; 	char s2[]="789"; 	printf("%d\n",&s); 	printf("%d\n",&s[0]); 	printf("%d\n",&s2); 	printf("%d\n",&s2[0]); }

    呵呵,s和s[0] 的地址竟然不一样。

    3 定义指针时编译器并不为指针所指向的内容分配空间,它只分配指针本身的空间,除非在声明的同时付给指针一个字符串常量初始化。比如:
    C代码
    1. char *s="abc";  
    char *s="abc"; 

    可是只有对字符串常量才是如此,其他的类型都会出错。

    4 数组和指针的相同点。
      。表达式中的数组名(不同于声明)被编译器当做一个指向数组第一个元素的指针。
      。下标总是和指针偏移量相同,a[i]总是被编译器改写成*(a+i)这种形式来访问。(比如:a[6]和6[a]是一样的)
      。在函数参数的声明中,数组名被编译器当做一个指向数组第一个元素的指针
    可以看下下面的代码的输出。
    C代码
    1. #include <stdio.h>  
    2. void f(char s[]);  
    3. void g(char *s);  
    4. char s2[4]="789";  
    5. void main()  
    6. {     
    7.     printf("%d\n",&s2);  
    8.     printf("%d\n",&(s2[0]));  
    9.     f(s2);  
    10.     g(s2);  
    11. }  
    12. void f(char s[4])  
    13. {  
    14.     printf("%d\n",&s);  
    15.     printf("%d\n",&(s[0]));  
    16. }  
    17. void g(char *s)  
    18. {  
    19.     printf("%d\n",&s);  
    20.     printf("%d\n",&s[0]);  
    21. }  
    #include <stdio.h> void f(char s[]); void g(char *s); char s2[4]="789"; void main() {	 	printf("%d\n",&s2); 	printf("%d\n",&(s2[0])); 	f(s2); 	g(s2); } void f(char s[4]) { 	printf("%d\n",&s); 	printf("%d\n",&(s[0])); } void g(char *s) { 	printf("%d\n",&s); 	printf("%d\n",&s[0]); }

    为什么c要做成这种呢,其实很简单,就是在c中调用函数的时候会把实参进行拷贝,而如果实参是数组的话,拷贝的开销太大,所以不如指针方便.

    呵呵,这边多维数组没有涉及到,不过多维数组只要紧记不过是数组的数组罢了.
  • 相关阅读:
    nodejs 的序列化与反序列化
    Visual Studio 监视与快速监视即时窗口没有智能提示
    mysql 备份数据语句
    mysql 导入sql 2006
    MySql.Data.MySqlClient.MySqlException (0x80004005): Unable to connect to any of the specified MySQL hosts.
    怎么查看mysql的安装目录
    【支付宝】退款接口 报 “缺少签名参数”
    【支付宝】"验签出错,sign值与sign_type参数指定的签名类型不一致:sign_type参数值为RSA,您实际用的签名类型可能是RSA2"
    【支付宝】支付 系统繁忙,请稍后再试(ALIN10146)
    php插入日志到数据库,对象转json
  • 原文地址:https://www.cnblogs.com/madfrog/p/1773363.html
Copyright © 2011-2022 走看看