Thread: Arrays vs pointers

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    9

    Arrays vs pointers

    Hello everyone,im a beginner in C programming.I've read a lot about the differences between arrays and pointers and I'm pretty confused.
    First of all what is the name of an array???
    Is it a label (or an alias) to the memory address or constant pointer?
    Code:
    #include <stdio.h>
    int main()
    {
        char arr[] = "string";
        char* ptr = arr;
    
        printf("%c %c\n", arr[4], ptr[4]);
        printf("%c %c\n", *(arr+2), *(ptr+2));
    
        return 0;
    }
    What is the difference between *(arr+2) and *(ptr+2)?Does this have the same meaning to the compiler?
    Thanks in advance for your answer.



  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The name of an array by itself is basically the same as "&array[0]": in other words, in most contexts it's treated as a pointer to the first element of the array. There are some cases when this is different (e.g. sizeof(array) will tell you the total bytes the array takes up, while sizeof(&array[0]) will tell you the size of pointers on your machine), but it's a useful simplification to think about. We sometimes talk about an array "degrading" to a pointer, since an with an array you have more information (e.g. the size) than you do with a pointer; this happens when, for example, you pass an array to a function and it becomes a pointer. Or, in your case, you assign a pointer to point at an array.

    An array is basically a contiguous chunk of memory somewhere. When you say array[1], array[i], the compiler knows you mean to find that chunk of memory and grab that element. But a pointer is slightly different: there is a location somewhere in memory that is storing *an address*. So "char *ptr" itself must be given some space. When you say ptr[2] or *(ptr+2), the compiler does some math to figure out where you mean, based on the address stored in the pointer. Another way of looking at it is that if you had an array of zero elements (which is probably invalid), it wouldn't actually take up any space at all. Whereas every time you add a pointer, a little bit of space must be used to store the "value" of the pointer (which is itself an address).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays and pointers
    By bobknows in forum C++ Programming
    Replies: 6
    Last Post: 02-13-2012, 08:02 AM
  2. Pointers to pointers with arrays
    By Nurumla in forum C Programming
    Replies: 3
    Last Post: 07-18-2011, 11:53 PM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. Arrays and Pointers
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-08-2002, 10:27 AM