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中调用函数的时候会把实参进行拷贝,而如果实参是数组的话,拷贝的开销太大,所以不如指针方便.

    呵呵,这边多维数组没有涉及到,不过多维数组只要紧记不过是数组的数组罢了.
  • 相关阅读:
    ubuntu安装jdk的两种方法
    LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)
    LeetCode 617. Merge Two Binary Tree (合并两个二叉树)
    LeetCode 476. Number Complement (数的补数)
    LeetCode 575. Distribute Candies (发糖果)
    LeetCode 461. Hamming Distance (汉明距离)
    LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)
    LeetCode 594. Longest Harmonious Subsequence (最长的协调子序列)
    LeetCode 371. Sum of Two Integers (两数之和)
    LeetCode 342. Power of Four (4的次方)
  • 原文地址:https://www.cnblogs.com/madfrog/p/1773363.html
Copyright © 2011-2022 走看看